jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/348662 )

Change subject: tests: Improve code coverage
......................................................................


tests: Improve code coverage

* Update PHPUnit to latest 4.x.
* Remove 5.3.3 from Travis CI matrix.
* Add 'composer run cover' command.
* Call parent::tearDown() after local logic, not before.
* Add missing @covers.
* Remove @group (leftover from MediaWiki).

* Add tests for
  - Reader::open()
  - Writer::open()
  - Writer::__destruct()
  - Reader\DBA::__construct() - file open error
  - Reader\PHP::__construct() - file open error
  - Reader\PHP::__construct() - file size error
  - Reader\DBA::get() - key not found
  - Reader\PHP::get() - key not found
  - Writer\PHP::finish() - empty case (0 keys)
    Also fixed array_fill warning.
* Ignore coverage:
  - Reader::haveExtension() - mocked environment check

Change-Id: I42a8adf747c7baa3675c97e43b5f642f66b14e66
---
M .travis.yml
M composer.json
M phpunit.xml.dist
M src/Reader.php
M src/Writer.php
M src/Writer/PHP.php
M tests/CdbTest.php
A tests/Reader/DBATest.php
M tests/Reader/HashTest.php
A tests/Reader/PHPTest.php
10 files changed, 131 insertions(+), 21 deletions(-)

Approvals:
  Aaron Schulz: Looks good to me, approved
  jenkins-bot: Verified
  Jforrester: Looks good to me, but someone else must approve



diff --git a/.travis.yml b/.travis.yml
index 5b4fcc5..19a0ad5 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,5 @@
 language: php
 php:
-  - "5.3.3"
   - "5.3"
   - "5.4"
   - "5.5"
@@ -10,8 +9,6 @@
 env:
   global:
     - COMPOSER_DISABLE_XDEBUG_WARN=1
-before_install:
-  - if [ "$TRAVIS_PHP_VERSION" = "5.3.3" ]; then composer config disable-tls 
true; composer config secure-http false; fi
 install:
   - composer install
 script:
diff --git a/composer.json b/composer.json
index bf54635..2c17e96 100644
--- a/composer.json
+++ b/composer.json
@@ -30,14 +30,15 @@
        },
        "require-dev": {
                "jakub-onderka/php-parallel-lint": "0.9",
-               "phpunit/phpunit": "4.6.*",
+               "phpunit/phpunit": "^4.8",
                "mediawiki/mediawiki-codesniffer": "0.5.0"
        },
        "scripts": {
                "test": [
                        "parallel-lint . --exclude vendor",
                        "phpunit $PHPUNIT_ARGS",
-                       "phpcs -p"
-               ]
+                       "phpcs -p -s"
+               ],
+               "cover": "phpunit --coverage-html coverage"
        }
 }
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 8cffd80..d4c95c0 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -1,6 +1,7 @@
 <phpunit colors="true"
        beStrictAboutTestsThatDoNotTestAnything="true"
-       beStrictAboutOutputDuringTests="true">
+       beStrictAboutOutputDuringTests="true"
+       verbose="true">
        <testsuites>
                <testsuite name="CDB Tests">
                        <directory>./tests</directory>
diff --git a/src/Reader.php b/src/Reader.php
index b1aaa02..565676f 100644
--- a/src/Reader.php
+++ b/src/Reader.php
@@ -38,7 +38,6 @@
         * Open a file and return a subclass instance
         *
         * @param string $fileName
-        *
         * @return Reader
         */
        public static function open( $fileName ) {
@@ -51,6 +50,7 @@
         * Returns true if the native extension is available
         *
         * @return bool
+        * @codeCoverageIgnore
         */
        public static function haveExtension() {
                if ( !function_exists( 'dba_handlers' ) ) {
diff --git a/src/Writer.php b/src/Writer.php
index 5321604..9e4e24d 100644
--- a/src/Writer.php
+++ b/src/Writer.php
@@ -51,7 +51,6 @@
         * The user must have write access to the directory, for temporary file 
creation.
         *
         * @param string $fileName
-        *
         * @return Writer
         */
        public static function open( $fileName ) {
diff --git a/src/Writer/PHP.php b/src/Writer/PHP.php
index 4676b5e..2fd5405 100644
--- a/src/Writer/PHP.php
+++ b/src/Writer/PHP.php
@@ -173,7 +173,12 @@
                // Excessively clever and indulgent code to simultaneously fill 
$packedTables
                // with the packed hashtables, and adjust the elements of 
$starts
                // to actually point to the starts instead of the ends.
-               $packedTables = array_fill( 0, $this->numentries, false );
+               if ( $this->numentries > 0 ) {
+                       $packedTables = array_fill( 0, $this->numentries, false 
);
+               } else {
+                       // array_fill(): Number of elements must be positive
+                       $packedTables = array();
+               }
                foreach ( $this->hplist as $item ) {
                        $packedTables[--$starts[255 & $item['h']]] = $item;
                }
diff --git a/tests/CdbTest.php b/tests/CdbTest.php
index 6ef4655..2ed655e 100644
--- a/tests/CdbTest.php
+++ b/tests/CdbTest.php
@@ -7,9 +7,6 @@
 
 /**
  * Test the CDB reader/writer
- * @group Cdb
- * @covers Cdb\Writer\PHP
- * @covers Cdb\Writer\DBA
  */
 class CdbTest extends \PHPUnit_Framework_TestCase {
        /** @var string */
@@ -29,9 +26,9 @@
        }
 
        protected function tearDown() {
-               parent::tearDown();
                unlink( $this->phpCdbFile );
                unlink( $this->dbaCdbFile );
+               parent::tearDown();
        }
 
        /**
@@ -48,11 +45,43 @@
                return $s;
        }
 
+       private function cdbAssert( $msg, $key, $expected, $actual ) {
+               $this->assertSame(
+                       $expected,
+                       $actual,
+                       $msg . ', k=' . bin2hex( $key )
+               );
+       }
+
        /**
+        * @covers Cdb\Reader::open
+        */
+       public function testReaderOpen() {
+               $this->assertInstanceOf(
+                       Reader::class,
+                       Reader::open( $this->phpCdbFile )
+               );
+       }
+
+       /**
+        * @covers Cdb\Writer::open
+        */
+       public function testWriterOpen() {
+               $this->assertInstanceOf(
+                       Writer::class,
+                       Writer::open( $this->phpCdbFile )
+               );
+       }
+
+       /**
+        * @covers Cdb\Util
+        * @covers Cdb\Writer
+        * @covers Cdb\Writer\PHP
+        * @covers Cdb\Writer\DBA
         * @covers Cdb\Reader\PHP
         * @covers Cdb\Reader\DBA
         */
-       public function testCdbWrite() {
+       public function testReadWrite() {
                $w1 = new Writer\PHP( $this->phpCdbFile );
                $w2 = new Writer\DBA( $this->dbaCdbFile );
 
@@ -104,6 +133,8 @@
                $this->assertTrue( $r2->exists( $firstKey ), 'DBA entry exists' 
);
                $this->assertFalse( $r1->exists( -1 ), 'PHP entry doesn\'t 
exists' );
                $this->assertFalse( $r2->exists( -1 ), 'DBA entry doesn\'t 
exists' );
+               $this->assertFalse( $r1->get( -1 ), 'PHP entry not found' );
+               $this->assertFalse( $r2->get( -1 ), 'DBA entry not found' );
 
                $firstKey1 = $r1->firstkey();
                $firstKey2 = $r2->firstkey();
@@ -120,11 +151,23 @@
                $this->assertFalse( $r2->nextkey() );
        }
 
-       private function cdbAssert( $msg, $key, $expected, $actual ) {
-               $this->assertSame(
-                       $expected,
-                       $actual,
-                       $msg . ', k=' . bin2hex( $key )
+       /**
+        * @covers Cdb\Writer\PHP::finish
+        */
+       public function testEmpty() {
+               $w = new Writer\PHP( $this->phpCdbFile );
+               $this->assertSame( null, $w->close() );
+       }
+
+       /**
+        * @covers Cdb\Writer::__destruct
+        */
+       public function testDestruct() {
+               $w = new Writer\PHP( $this->phpCdbFile );
+               $this->assertInstanceOf(
+                       Writer\PHP::class,
+                       $w
                );
+               $w = null;
        }
 }
diff --git a/tests/Reader/DBATest.php b/tests/Reader/DBATest.php
new file mode 100644
index 0000000..11a32ad
--- /dev/null
+++ b/tests/Reader/DBATest.php
@@ -0,0 +1,25 @@
+<?php
+namespace Cdb\Test;
+
+use Cdb\Reader;
+use Cdb\Reader\DBA;
+
+/**
+ * @covers Cdb\Reader\DBA
+ */
+class DBATest extends \PHPUnit_Framework_TestCase {
+
+       protected function setUp() {
+               parent::setUp();
+               if ( !Reader::haveExtension() ) {
+                       $this->markTestSkipped( 'Native CDB support is not 
available.' );
+               }
+       }
+
+       public function testConstructor() {
+               $this->setExpectedException( 'Cdb\Exception' );
+               // Silence native error from dba_open()
+               // @codingStandardsIgnoreLine Generic.PHP.NoSilencedErrors
+               @new DBA( '/tmp/non-exist' );
+       }
+}
diff --git a/tests/Reader/HashTest.php b/tests/Reader/HashTest.php
index 70401fa..78919a2 100644
--- a/tests/Reader/HashTest.php
+++ b/tests/Reader/HashTest.php
@@ -4,7 +4,6 @@
 use Cdb\Reader\Hash;
 
 /**
- * @group Cdb
  * @covers Cdb\Reader\Hash
  */
 class HashTest extends \PHPUnit_Framework_TestCase {
diff --git a/tests/Reader/PHPTest.php b/tests/Reader/PHPTest.php
new file mode 100644
index 0000000..3a5bab0
--- /dev/null
+++ b/tests/Reader/PHPTest.php
@@ -0,0 +1,40 @@
+<?php
+namespace Cdb\Test;
+
+use Cdb\Reader\PHP;
+
+/**
+ * @covers Cdb\Reader\PHP
+ */
+class PHPTest extends \PHPUnit_Framework_TestCase {
+       /** @var string */
+       private $cdbFile;
+
+       protected function setUp() {
+               parent::setUp();
+               $temp = sys_get_temp_dir();
+               if ( !is_writable( $temp ) ) {
+                       $this->markTestSkipped( "Temp dir [$temp] isn't 
writable." );
+               }
+               $this->cdbFile = tempnam( $temp, get_class( $this ) . '_' );
+       }
+
+       protected function tearDown() {
+               unlink( $this->cdbFile );
+               parent::tearDown();
+       }
+
+       // File can't be opened
+       public function testConstructorOpen() {
+               $this->setExpectedException( 'Cdb\Exception' );
+               // Ignore native error from fopen()
+               // @codingStandardsIgnoreLine Generic.PHP.NoSilencedErrors
+               @new PHP( '/tmp/non-exist' );
+       }
+
+       // File contains fewer than 2048 bytes
+       public function testConstructorRead() {
+               $this->setExpectedException( 'Cdb\Exception' );
+               new PHP( $this->cdbFile );
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/348662
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I42a8adf747c7baa3675c97e43b5f642f66b14e66
Gerrit-PatchSet: 5
Gerrit-Project: cdb
Gerrit-Branch: master
Gerrit-Owner: Krinkle <krinklem...@gmail.com>
Gerrit-Reviewer: Aaron Schulz <asch...@wikimedia.org>
Gerrit-Reviewer: Jforrester <jforres...@wikimedia.org>
Gerrit-Reviewer: Krinkle <krinklem...@gmail.com>
Gerrit-Reviewer: Ori.livneh <o...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to