| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace Wikimedia\Tests; |
| 22 |
|
| 23 |
use PHPUnit\Framework\TestCase; |
| 24 |
use Wikimedia\StaticArrayWriter; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class StaticArrayWriterTest extends TestCase { |
| 30 |
public function testCreate() { |
| 31 |
$data = [ |
| 32 |
'foo' => 'bar', |
| 33 |
'baz' => 'rawr', |
| 34 |
"they're" => '"quoted properly"', |
| 35 |
'nested' => [ 'elements', 'work' ], |
| 36 |
'intlike' => [ '050' => true, '101' => true, '221B' => true ], |
| 37 |
'and' => [ 'these' => 'do too' ], |
| 38 |
]; |
| 39 |
$writer = new StaticArrayWriter(); |
| 40 |
$actual = $writer->create( $data, "Header\nWith\nNewlines" ); |
| 41 |
$expected = <<<PHP |
| 42 |
<?php |
| 43 |
// Header |
| 44 |
// With |
| 45 |
// Newlines |
| 46 |
return [ |
| 47 |
'foo' => 'bar', |
| 48 |
'baz' => 'rawr', |
| 49 |
'they\'re' => '"quoted properly"', |
| 50 |
'nested' => [ |
| 51 |
'elements', |
| 52 |
'work', |
| 53 |
], |
| 54 |
'intlike' => [ |
| 55 |
'050' => true, |
| 56 |
101 => true, |
| 57 |
'221B' => true, |
| 58 |
], |
| 59 |
'and' => [ |
| 60 |
'these' => 'do too', |
| 61 |
], |
| 62 |
]; |
| 63 |
|
| 64 |
PHP; |
| 65 |
$this->assertSame( $expected, $actual ); |
| 66 |
} |
| 67 |
|
| 68 |
public function testWriteClass() { |
| 69 |
$data = [ |
| 70 |
'foo' => 'bar', |
| 71 |
'baz' => 'rawr', |
| 72 |
]; |
| 73 |
$writer = new StaticArrayWriter(); |
| 74 |
$actual = $writer->writeClass( $data, [ |
| 75 |
'header' => "Header\nWith\nNewlines", |
| 76 |
'namespace' => 'Example\\Foo', |
| 77 |
'class' => 'Bar', |
| 78 |
'const' => 'THING', |
| 79 |
] ); |
| 80 |
$expected = <<<PHP |
| 81 |
<?php |
| 82 |
// Header |
| 83 |
// With |
| 84 |
// Newlines |
| 85 |
|
| 86 |
namespace Example\Foo; |
| 87 |
|
| 88 |
class Bar { |
| 89 |
public const THING = [ |
| 90 |
'foo' => 'bar', |
| 91 |
'baz' => 'rawr', |
| 92 |
]; |
| 93 |
} |
| 94 |
|
| 95 |
PHP; |
| 96 |
$this->assertSame( $expected, $actual ); |
| 97 |
} |
| 98 |
} |