test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / CdbTest.php
blobe3d9da7c84c58cbc7661e3125e2267c383107f12
1 <?php
3 /**
4 * Test the CDB reader/writer
5 */
6 class CdbTest extends MediaWikiTestCase {
8 protected function setUp() {
9 parent::setUp();
10 if ( !CdbReader::haveExtension() ) {
11 $this->markTestSkipped( 'Native CDB support is not available' );
15 /**
16 * @group medium
18 public function testCdb() {
19 $dir = wfTempDir();
20 if ( !is_writable( $dir ) ) {
21 $this->markTestSkipped( "Temp dir isn't writable" );
24 $phpcdbfile = $this->getNewTempFile();
25 $dbacdbfile = $this->getNewTempFile();
27 $w1 = new CdbWriter_PHP( $phpcdbfile );
28 $w2 = new CdbWriter_DBA( $dbacdbfile );
30 $data = array();
31 for ( $i = 0; $i < 1000; $i++ ) {
32 $key = $this->randomString();
33 $value = $this->randomString();
34 $w1->set( $key, $value );
35 $w2->set( $key, $value );
37 if ( !isset( $data[$key] ) ) {
38 $data[$key] = $value;
42 $w1->close();
43 $w2->close();
45 $this->assertEquals(
46 md5_file( $phpcdbfile ),
47 md5_file( $dbacdbfile ),
48 'same hash'
51 $r1 = new CdbReader_PHP( $phpcdbfile );
52 $r2 = new CdbReader_DBA( $dbacdbfile );
54 foreach ( $data as $key => $value ) {
55 if ( $key === '' ) {
56 // Known bug
57 continue;
59 $v1 = $r1->get( $key );
60 $v2 = $r2->get( $key );
62 $v1 = $v1 === false ? '(not found)' : $v1;
63 $v2 = $v2 === false ? '(not found)' : $v2;
65 # cdbAssert( 'Mismatch', $key, $v1, $v2 );
66 $this->cdbAssert( "PHP error", $key, $v1, $value );
67 $this->cdbAssert( "DBA error", $key, $v2, $value );
71 private function randomString() {
72 $len = mt_rand( 0, 10 );
73 $s = '';
74 for ( $j = 0; $j < $len; $j++ ) {
75 $s .= chr( mt_rand( 0, 255 ) );
78 return $s;
81 private function cdbAssert( $msg, $key, $v1, $v2 ) {
82 $this->assertEquals(
83 $v2,
84 $v1,
85 $msg . ', k=' . bin2hex( $key )