Krinkle has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/350109 )

Change subject: tests: Complete code coverage to 100%
......................................................................

tests: Complete code coverage to 100%

Add missing cases:
* __construct:
  - Contruct with DateTime.
* setTimestamp:
  - Parse timestamp=0 to mean now.
  - Parse timestamp in 'RFC 850' format.
  - Parse timestamp in 'asctime' format.
  - 'catch' branch from DateTime construction.
* convert:
  - Fix broken test. It was passing the invalid timestamps as $format
    instead of $ts parameter, thus it was a duplicate test for
    invalid format, which covers a different code path.
* now:
  - New test for uncovered method.
* setTimezone:
  - New test for uncovered method.
* getTimezone:
  - New test for uncovered method.
* format:
  - New test for uncovered method.

Also:
* Add 'composer run cover' command.
* Ignore unreachable '$final === false' case. Added FIXME.
* Ignore ./src/defines.php.
* Change comment 'TS_RFC850' which looks like a constant, but
  is in fact just one of two formats that are parse-only
  (together with 'asctime').

Change-Id: I687546adb4f499308bdba9d7a8d752a5365ef157
---
M composer.json
M phpunit.xml.dist
M src/ConvertibleTimestamp.php
M tests/TimestampTest.php
4 files changed, 120 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/libs/Timestamp 
refs/changes/09/350109/1

diff --git a/composer.json b/composer.json
index 714a560..9dd2b11 100644
--- a/composer.json
+++ b/composer.json
@@ -27,6 +27,7 @@
                        "phpunit",
                        "phpcs -p -s"
                ],
+               "cover": "phpunit --coverage-html coverage/",
                "fix": "phpcbf"
        }
 }
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 21fd54c..6ec4091 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -9,6 +9,9 @@
        <filter>
                <whitelist addUncoveredFilesFromWhitelist="true">
                        <directory suffix=".php">./src</directory>
+                       <exclude>
+                               <file>./src/defines.php</file>
+                       </exclude>
                </whitelist>
        </filter>
 </phpunit>
diff --git a/src/ConvertibleTimestamp.php b/src/ConvertibleTimestamp.php
index 5ed934a..5dfd477 100644
--- a/src/ConvertibleTimestamp.php
+++ b/src/ConvertibleTimestamp.php
@@ -125,7 +125,7 @@
                ) ) {
                        # TS_POSTGRES
                } elseif ( preg_match(
-               # Day of week
+                       # Day of week
                        '/^[ \t\r\n]*([A-Z][a-z]{2},[ \t\r\n]*)?' .
                        # dd Mon yyyy
                        '\d\d?[ \t\r\n]*[A-Z][a-z]{2}[ \t\r\n]*\d{2}(?:\d{2})?' 
.
@@ -138,7 +138,7 @@
                        # The regex is a superset of rfc2822 for readability
                        $strtime = strtok( $ts, ';' );
                } elseif ( preg_match( '/^[A-Z][a-z]{5,8}, 
\d\d-[A-Z][a-z]{2}-\d{2} \d\d:\d\d:\d\d/', $ts ) ) {
-                       # TS_RFC850
+                       # RFC 850
                        $strtime = $ts;
                } elseif ( preg_match( '/^[A-Z][a-z]{2} [A-Z][a-z]{2} +\d{1,2} 
\d\d:\d\d:\d\d \d{4}/', $ts ) ) {
                        # asctime
@@ -160,7 +160,10 @@
                }
 
                if ( $final === false ) {
+                       // FIXME: DateTime never returns false?
+                       // @codeCoverageIgnoreStart
                        throw new TimestampException( __METHOD__ . ': Invalid 
timestamp format.' );
+                       // @codeCoverageIgnoreEnd
                }
 
                $this->timestamp = $final;
diff --git a/tests/TimestampTest.php b/tests/TimestampTest.php
index 858ebc2..0bc33b3 100644
--- a/tests/TimestampTest.php
+++ b/tests/TimestampTest.php
@@ -37,6 +37,16 @@
        }
 
        /**
+        * @covers \Wikimedia\Timestamp\ConvertibleTimestamp::__construct
+        */
+       public function testConstructWithDateTime() {
+               $input = '1343761268';
+               $dt = new \DateTime( "@$input", new \DateTimeZone( 'GMT' ) );
+               $timestamp = new ConvertibleTimestamp( $dt );
+               $this->assertSame( $input, $timestamp->getTimestamp() );
+       }
+
+       /**
         * @covers \Wikimedia\Timestamp\ConvertibleTimestamp::__toString
         */
        public function testToString() {
@@ -68,6 +78,7 @@
         * Parse valid timestamps and output in MW format.
         *
         * @dataProvider provideValidTimestamps
+        * @covers \Wikimedia\Timestamp\ConvertibleTimestamp::setTimestamp
         * @covers \Wikimedia\Timestamp\ConvertibleTimestamp::getTimestamp
         */
        public function testValidParse( $format, $original, $expected ) {
@@ -75,12 +86,56 @@
                $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW 
) );
        }
 
+       public static function provideParseOnly() {
+               // Formats supported only by setTimestamp(), but do not
+               // have a constant for getTimestamp()
+               return [
+                       'RFC 850' => [ 'Tuesday, 31-Jul-12 19:01:08 UTC', 
'20120731190108' ],
+                       'asctime' => [ 'Tue Jul 31 19:01:08 2012', 
'20120731190108' ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideParseOnly
+        * @covers \Wikimedia\Timestamp\ConvertibleTimestamp::setTimestamp
+        */
+       public function testValidParseOnly( $original, $expected ) {
+               $timestamp = new ConvertibleTimestamp( $original );
+               $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW 
) );
+       }
+
+       /**
+        * @covers \Wikimedia\Timestamp\ConvertibleTimestamp::setTimestamp
+        */
+       public function testValidParseZero() {
+               $now = time();
+               $timestamp = new ConvertibleTimestamp( 0 );
+               $this->assertEquals(
+                       $now,
+                       $timestamp->getTimestamp( TS_UNIX ),
+                       'now',
+                       10.0 // acceptable delta in seconds
+               );
+       }
+
+       /**
+        * @covers \Wikimedia\Timestamp\ConvertibleTimestamp::now
+        */
+       public function testNow() {
+               $this->assertEquals(
+                       time(),
+                       ConvertibleTimestamp::now( TS_UNIX ),
+                       'now',
+                       10.0 // acceptable delta in seconds
+               );
+       }
+
        /**
         * Parse invalid timestamps.
         *
         * @dataProvider provideInvalidTimestamps
         * @expectedException \Wikimedia\Timestamp\TimestampException
-        * @covers \Wikimedia\Timestamp\ConvertibleTimestamp
+        * @covers \Wikimedia\Timestamp\ConvertibleTimestamp::setTimestamp
         */
        public function testInvalidParse( $input ) {
                new ConvertibleTimestamp( $input );
@@ -112,7 +167,7 @@
         * @covers \Wikimedia\Timestamp\ConvertibleTimestamp::convert
         */
        public function testConvertInvalid( $input ) {
-               $this->assertSame( false, ConvertibleTimestamp::convert( 
$input, 0 ) );
+               $this->assertSame( false, ConvertibleTimestamp::convert( 
TS_UNIX, $input ) );
        }
 
        /**
@@ -127,14 +182,58 @@
                $timestamp->getTimestamp( $format );
        }
 
+       public static function provideInvalidFormats() {
+               return [
+                       [ 'Not a format' ],
+                       [ 98 ],
+               ];
+       }
+
        /**
-        * Test requesting an invalid output format.
+        * @dataProvider provideInvalidFormats
         * @covers \Wikimedia\Timestamp\ConvertibleTimestamp::getTimestamp
         * @expectedException \Wikimedia\Timestamp\TimestampException
         */
-       public function testInvalidOutput() {
+       public function testInvalidFormat( $format ) {
                $timestamp = new ConvertibleTimestamp( '1343761268' );
-               $timestamp->getTimestamp( 98 );
+               $timestamp->getTimestamp( $format );
+       }
+
+       /**
+        * @covers \Wikimedia\Timestamp\ConvertibleTimestamp::setTimezone
+        */
+       public function testSetTimezone() {
+               $timestamp = new ConvertibleTimestamp( 0 );
+               $this->assertSame( null, $timestamp->setTimezone( 'GMT' ) );
+       }
+
+       /**
+        * @covers \Wikimedia\Timestamp\ConvertibleTimestamp::setTimezone
+        * @expectedException \Wikimedia\Timestamp\TimestampException
+        */
+       public function testSetTimezoneInvalid() {
+               $timestamp = new ConvertibleTimestamp( 0 );
+               $timestamp->setTimezone( 'Invalid' );
+       }
+
+       /**
+        * @covers \Wikimedia\Timestamp\ConvertibleTimestamp::getTimezone
+        */
+       public function testGetTimezone() {
+               $timestamp = new ConvertibleTimestamp( 0 );
+               $this->assertInstanceOf(
+                       \DateTimeZone::class,
+                       $timestamp->getTimezone()
+               );
+       }
+
+       /**
+        * @covers \Wikimedia\Timestamp\ConvertibleTimestamp::format
+        */
+       public function testFormat() {
+               $timestamp = new ConvertibleTimestamp( '1343761268' );
+               $this->assertSame( '1343761268', $timestamp->format( 'U' ) );
+               $this->assertSame( '20120731190108', $timestamp->format( 
'YmdHis' ) );
        }
 
        /**
@@ -143,7 +242,7 @@
         */
        public static function provideValidTimestamps() {
                return [
-                       // Various formats
+                       // Formats supported in both directions
                        [ TS_UNIX, '1343761268', '20120731190108' ],
                        [ TS_MW, '20120731190108', '20120731190108' ],
                        [ TS_DB, '2012-07-31 19:01:08', '20120731190108' ],
@@ -164,8 +263,13 @@
         */
        public static function provideInvalidTimestamps() {
                return [
+                       // Not matching any known patterns
+                       // (throws from main 'else' branch in setTimestamp)
                        [ 'Not a timestamp' ],
-                       [ '1971:01:01 06:19:385' ]
+                       [ '1971:01:01 06:19:385' ],
+                       // Invalid values for known patterns
+                       // (throws from DateTime construction)
+                       [ 'Zed, 40 Mud 2012 99:99:99 GMT' ],
                ];
        }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I687546adb4f499308bdba9d7a8d752a5365ef157
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/libs/Timestamp
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to