From: Antoine Musso Date: Wed, 2 Feb 2011 21:04:21 +0000 (+0000) Subject: Test suite for revisions and date related magic variables. X-Git-Tag: 1.31.0-rc.0~32245 X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=596956c928878cc432e57b27a83453f219df888a;p=lhc%2Fweb%2Fwiklou.git Test suite for revisions and date related magic variables. Tests should be carefully reviewed, please note there is are exceptions for 'revisionday' and 'revisionmonth1' magics which are hardcoded in the parser to return an integer. Follow up r66200 TESTS: $ php phpunit.php -c suite.xml --filter MagicVariable --testdox PHPUnit 3.5.10 by Sebastian Bergmann. MagicVariable [x] Currentday is un padded [x] Currentdaytwo is zero padded [x] Localday is un padded [x] Localdaytwo is zero padded [x] Currentmonth is zero padded [x] Currentmonthone is un padded [x] Localmonth is zero padded [x] Localmonthone is un padded [x] Revisionday is un padded [x] Revisiondaytwo is zero padded [x] Revisionmonth is zero padded [x] Revisionmonthone is un padded $ --- diff --git a/tests/phpunit/includes/parser/MagicVariableTest.php b/tests/phpunit/includes/parser/MagicVariableTest.php new file mode 100644 index 0000000000..bd5500139c --- /dev/null +++ b/tests/phpunit/includes/parser/MagicVariableTest.php @@ -0,0 +1,199 @@ +testParser = new Parser(); + $this->testParser->Options( new ParserOptions() ); + + # initialize parser output + $this->testParser->clearState(); + } + + /** destroy parser (TODO: is it really neded?)*/ + function tearDown() { + unset( $this->testParser ); + } + + ############### TESTS ############################################# + # FIXME: + # - those got copy pasted, we can probably make them cleaner + # - tests are lacking useful messages + + # day + + /** @dataProvider provideDays */ + function testCurrentdayIsUnPadded( $day ) { + $this->assertUnPadded( 'currentday', $day ); + } + /** @dataProvider provideDays */ + function testCurrentdaytwoIsZeroPadded( $day ) { + $this->assertZeroPadded( 'currentday2', $day ); + } + /** @dataProvider provideDays */ + function testLocaldayIsUnPadded( $day ) { + $this->assertUnPadded( 'localday', $day ); + } + /** @dataProvider provideDays */ + function testLocaldaytwoIsZeroPadded( $day ) { + $this->assertZeroPadded( 'localday2', $day ); + } + + # month + + /** @dataProvider provideMonths */ + function testCurrentmonthIsZeroPadded( $month ) { + $this->assertZeroPadded( 'currentmonth', $month ); + } + /** @dataProvider provideMonths */ + function testCurrentmonthoneIsUnPadded( $month ) { + $this->assertUnPadded( 'currentmonth1', $month ); + } + /** @dataProvider provideMonths */ + function testLocalmonthIsZeroPadded( $month ) { + $this->assertZeroPadded( 'localmonth', $month ); + } + /** @dataProvider provideMonths */ + function testLocalmonthoneIsUnPadded( $month ) { + $this->assertUnPadded( 'localmonth1', $month ); + } + + + # revision day + + /** @dataProvider provideDays */ + function testRevisiondayIsUnPadded( $day ) { + $this->assertUnPadded( 'revisionday', $day ); + } + /** @dataProvider provideDays */ + function testRevisiondaytwoIsZeroPadded( $day ) { + $this->assertZeroPadded( 'revisionday2', $day ); + } + + # revision month + + /** @dataProvider provideMonths */ + function testRevisionmonthIsZeroPadded( $month ) { + $this->assertZeroPadded( 'revisionmonth', $month ); + } + /** @dataProvider provideMonths */ + function testRevisionmonthoneIsUnPadded( $month ) { + $this->assertUnPadded( 'revisionmonth1', $month ); + } + + ############### HELPERS ############################################ + + /** assertion helper expecting a magic output which is zero padded */ + PUBLIC function assertZeroPadded( $magic, $value ) { + $this->assertMagicPadding( $magic, $value, '%02d' ); + } + + /** assertion helper expecting a magic output which is unpadded */ + PUBLIC function assertUnPadded( $magic, $value ) { + $this->assertMagicPadding( $magic, $value, '%d' ); + } + + /** + * Main assertion helper for magic variables padding + * @param $magic string Magic variable name + * @param $value mixed Month or day + * @param $format string sprintf format for $value + */ + private function assertMagicPadding( $magic, $value, $format ) { + # Initialize parser timestamp as year 2010 at 12h34 56s. + # month and day are given by the caller ($value). Month < 12! + if( $value > 12 ) { $month = $value % 12; } + else { $month = $value; } + + $this->setParserTS( + sprintf( '2010%02d%02d123456', $month, $value ) + ); + + # please keep the following commented line of code. It helps debugging. + //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n"; + + # format expectation and test it + $expected = sprintf( $format, $value ); + $this->assertMagic( $expected, $magic ); + } + + /** helper to set the parser timestamp and revision timestamp */ + private function setParserTS( $ts ) { + $this->testParser->Options()->setTimestamp( $ts ); + $this->testParser->mRevisionTimestamp = $ts; + } + + /** + * Assertion helper to test a magic variable output + */ + private function assertMagic( $expected, $magic ) { + if( in_array( $magic, $this->expectedAsInteger ) ) { + $expected = (int) $expected; + } + + # Generate a message for the assertion + $msg = sprintf( "Magic %s should be <%s:%s>", + $magic, + $expected, + gettype( $expected ) + ); + + $this->assertSame( + $expected, + $this->testParser->getVariableValue( $magic ), + $msg + ); + } + + + ############## PROVIDERS ########################################## + + /* provide an array of numbers from 1 up to @param $num */ + private function createProviderUpTo( $num ) { + $ret = array(); + for( $i=1; $i<=$num;$i++ ) { + $ret[] = array( $i ); + } + return $ret; + } + + /* array of months numbers (as an integer) */ + public function provideMonths() { + return $this->createProviderUpTo( 12 ); + } + + /* array of days numbers (as an integer) */ + public function provideDays() { + return $this->createProviderUpTo( 31 ); + } +}