| Index: trunk/phase3/tests/phpunit/includes/parser/MagicVariableTest.php |
| — | — | @@ -0,0 +1,199 @@ |
| | 2 | +<?php |
| | 3 | +/** |
| | 4 | + * This file is intended to test magic variables in the parser |
| | 5 | + * It was inspired by Raymond & Matěj Grabovský commenting about r66200 |
| | 6 | + * |
| | 7 | + * As of february 2011, it only tests some revisions and date related |
| | 8 | + * magic variables. |
| | 9 | + * |
| | 10 | + * @author Ashar Voultoiz |
| | 11 | + * @copyright Copyright © 2011, Ashar Voultoiz |
| | 12 | + * @file |
| | 13 | + */ |
| | 14 | + |
| | 15 | +/** */ |
| | 16 | +class MagicVariableTest extends PHPUnit_Framework_TestCase { |
| | 17 | + /** Will contains a parser object*/ |
| | 18 | + private $testParser = null; |
| | 19 | + |
| | 20 | + /** |
| | 21 | + * An array of magicword returned as type integer by the parser |
| | 22 | + * They are usually returned as a string for i18n since we support |
| | 23 | + * persan numbers for example, but some magic explicitly return |
| | 24 | + * them as integer. |
| | 25 | + * @see MagicVariableTest::assertMagic() |
| | 26 | + */ |
| | 27 | + private $expectedAsInteger = array( |
| | 28 | + 'revisionday', |
| | 29 | + 'revisionmonth1', |
| | 30 | + ); |
| | 31 | + |
| | 32 | + /** setup a basic parser object */ |
| | 33 | + function setUp() { |
| | 34 | + global $wgContLang; |
| | 35 | + $wgContLang = Language::factory( 'en' ); |
| | 36 | + |
| | 37 | + $this->testParser = new Parser(); |
| | 38 | + $this->testParser->Options( new ParserOptions() ); |
| | 39 | + |
| | 40 | + # initialize parser output |
| | 41 | + $this->testParser->clearState(); |
| | 42 | + } |
| | 43 | + |
| | 44 | + /** destroy parser (TODO: is it really neded?)*/ |
| | 45 | + function tearDown() { |
| | 46 | + unset( $this->testParser ); |
| | 47 | + } |
| | 48 | + |
| | 49 | + ############### TESTS ############################################# |
| | 50 | + # FIXME: |
| | 51 | + # - those got copy pasted, we can probably make them cleaner |
| | 52 | + # - tests are lacking useful messages |
| | 53 | + |
| | 54 | + # day |
| | 55 | + |
| | 56 | + /** @dataProvider provideDays */ |
| | 57 | + function testCurrentdayIsUnPadded( $day ) { |
| | 58 | + $this->assertUnPadded( 'currentday', $day ); |
| | 59 | + } |
| | 60 | + /** @dataProvider provideDays */ |
| | 61 | + function testCurrentdaytwoIsZeroPadded( $day ) { |
| | 62 | + $this->assertZeroPadded( 'currentday2', $day ); |
| | 63 | + } |
| | 64 | + /** @dataProvider provideDays */ |
| | 65 | + function testLocaldayIsUnPadded( $day ) { |
| | 66 | + $this->assertUnPadded( 'localday', $day ); |
| | 67 | + } |
| | 68 | + /** @dataProvider provideDays */ |
| | 69 | + function testLocaldaytwoIsZeroPadded( $day ) { |
| | 70 | + $this->assertZeroPadded( 'localday2', $day ); |
| | 71 | + } |
| | 72 | + |
| | 73 | + # month |
| | 74 | + |
| | 75 | + /** @dataProvider provideMonths */ |
| | 76 | + function testCurrentmonthIsZeroPadded( $month ) { |
| | 77 | + $this->assertZeroPadded( 'currentmonth', $month ); |
| | 78 | + } |
| | 79 | + /** @dataProvider provideMonths */ |
| | 80 | + function testCurrentmonthoneIsUnPadded( $month ) { |
| | 81 | + $this->assertUnPadded( 'currentmonth1', $month ); |
| | 82 | + } |
| | 83 | + /** @dataProvider provideMonths */ |
| | 84 | + function testLocalmonthIsZeroPadded( $month ) { |
| | 85 | + $this->assertZeroPadded( 'localmonth', $month ); |
| | 86 | + } |
| | 87 | + /** @dataProvider provideMonths */ |
| | 88 | + function testLocalmonthoneIsUnPadded( $month ) { |
| | 89 | + $this->assertUnPadded( 'localmonth1', $month ); |
| | 90 | + } |
| | 91 | + |
| | 92 | + |
| | 93 | + # revision day |
| | 94 | + |
| | 95 | + /** @dataProvider provideDays */ |
| | 96 | + function testRevisiondayIsUnPadded( $day ) { |
| | 97 | + $this->assertUnPadded( 'revisionday', $day ); |
| | 98 | + } |
| | 99 | + /** @dataProvider provideDays */ |
| | 100 | + function testRevisiondaytwoIsZeroPadded( $day ) { |
| | 101 | + $this->assertZeroPadded( 'revisionday2', $day ); |
| | 102 | + } |
| | 103 | + |
| | 104 | + # revision month |
| | 105 | + |
| | 106 | + /** @dataProvider provideMonths */ |
| | 107 | + function testRevisionmonthIsZeroPadded( $month ) { |
| | 108 | + $this->assertZeroPadded( 'revisionmonth', $month ); |
| | 109 | + } |
| | 110 | + /** @dataProvider provideMonths */ |
| | 111 | + function testRevisionmonthoneIsUnPadded( $month ) { |
| | 112 | + $this->assertUnPadded( 'revisionmonth1', $month ); |
| | 113 | + } |
| | 114 | + |
| | 115 | + ############### HELPERS ############################################ |
| | 116 | + |
| | 117 | + /** assertion helper expecting a magic output which is zero padded */ |
| | 118 | + PUBLIC function assertZeroPadded( $magic, $value ) { |
| | 119 | + $this->assertMagicPadding( $magic, $value, '%02d' ); |
| | 120 | + } |
| | 121 | + |
| | 122 | + /** assertion helper expecting a magic output which is unpadded */ |
| | 123 | + PUBLIC function assertUnPadded( $magic, $value ) { |
| | 124 | + $this->assertMagicPadding( $magic, $value, '%d' ); |
| | 125 | + } |
| | 126 | + |
| | 127 | + /** |
| | 128 | + * Main assertion helper for magic variables padding |
| | 129 | + * @param $magic string Magic variable name |
| | 130 | + * @param $value mixed Month or day |
| | 131 | + * @param $format string sprintf format for $value |
| | 132 | + */ |
| | 133 | + private function assertMagicPadding( $magic, $value, $format ) { |
| | 134 | + # Initialize parser timestamp as year 2010 at 12h34 56s. |
| | 135 | + # month and day are given by the caller ($value). Month < 12! |
| | 136 | + if( $value > 12 ) { $month = $value % 12; } |
| | 137 | + else { $month = $value; } |
| | 138 | + |
| | 139 | + $this->setParserTS( |
| | 140 | + sprintf( '2010%02d%02d123456', $month, $value ) |
| | 141 | + ); |
| | 142 | + |
| | 143 | + # please keep the following commented line of code. It helps debugging. |
| | 144 | + //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n"; |
| | 145 | + |
| | 146 | + # format expectation and test it |
| | 147 | + $expected = sprintf( $format, $value ); |
| | 148 | + $this->assertMagic( $expected, $magic ); |
| | 149 | + } |
| | 150 | + |
| | 151 | + /** helper to set the parser timestamp and revision timestamp */ |
| | 152 | + private function setParserTS( $ts ) { |
| | 153 | + $this->testParser->Options()->setTimestamp( $ts ); |
| | 154 | + $this->testParser->mRevisionTimestamp = $ts; |
| | 155 | + } |
| | 156 | + |
| | 157 | + /** |
| | 158 | + * Assertion helper to test a magic variable output |
| | 159 | + */ |
| | 160 | + private function assertMagic( $expected, $magic ) { |
| | 161 | + if( in_array( $magic, $this->expectedAsInteger ) ) { |
| | 162 | + $expected = (int) $expected; |
| | 163 | + } |
| | 164 | + |
| | 165 | + # Generate a message for the assertion |
| | 166 | + $msg = sprintf( "Magic %s should be <%s:%s>", |
| | 167 | + $magic, |
| | 168 | + $expected, |
| | 169 | + gettype( $expected ) |
| | 170 | + ); |
| | 171 | + |
| | 172 | + $this->assertSame( |
| | 173 | + $expected, |
| | 174 | + $this->testParser->getVariableValue( $magic ), |
| | 175 | + $msg |
| | 176 | + ); |
| | 177 | + } |
| | 178 | + |
| | 179 | + |
| | 180 | + ############## PROVIDERS ########################################## |
| | 181 | + |
| | 182 | + /* provide an array of numbers from 1 up to @param $num */ |
| | 183 | + private function createProviderUpTo( $num ) { |
| | 184 | + $ret = array(); |
| | 185 | + for( $i=1; $i<=$num;$i++ ) { |
| | 186 | + $ret[] = array( $i ); |
| | 187 | + } |
| | 188 | + return $ret; |
| | 189 | + } |
| | 190 | + |
| | 191 | + /* array of months numbers (as an integer) */ |
| | 192 | + public function provideMonths() { |
| | 193 | + return $this->createProviderUpTo( 12 ); |
| | 194 | + } |
| | 195 | + |
| | 196 | + /* array of days numbers (as an integer) */ |
| | 197 | + public function provideDays() { |
| | 198 | + return $this->createProviderUpTo( 31 ); |
| | 199 | + } |
| | 200 | +} |
| Property changes on: trunk/phase3/tests/phpunit/includes/parser/MagicVariableTest.php |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| 1 | 201 | + native |