|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ConfigWriter\Tests; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use ReflectionClass; |
| 7 | +use ConfigWriter\Config; |
| 8 | +use ConfigWriter\Writers\PhpWriter; |
| 9 | + |
| 10 | +class PhpWriterTest extends TestCase |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var \ConfigWriter\Config |
| 14 | + */ |
| 15 | + protected $config; |
| 16 | + |
| 17 | + public function setUp() |
| 18 | + { |
| 19 | + $this->config = new Config; |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * @covers \ConfigWriter\Writers\PhpWriter::write() |
| 24 | + * @covers \ConfigWriter\Writers\PhpWriter::export() |
| 25 | + * @covers \ConfigWriter\Writers\PhpWriter::comment() |
| 26 | + */ |
| 27 | + public function testWriteConfig() |
| 28 | + { |
| 29 | + $database = $this->config->addSection('database', [], 'Database settings'); |
| 30 | + $database->addRecord('user', 'root', 'Database username'); |
| 31 | + $database->addRecord('pass', '', 'Database password'); |
| 32 | + |
| 33 | + $development = $this->config->addSection('development', [], 'Development settings'); |
| 34 | + $development->addRecord('debug', true, 'Debugging'); |
| 35 | + $development->addRecord('errors', 32767, 'Error reporting'); |
| 36 | + |
| 37 | + $filename = sys_get_temp_dir() . '/ConfigWriterTests/' . uniqid(rand()) . '.php'; |
| 38 | + $this->config->toFile($filename); |
| 39 | + |
| 40 | + $expected = <<<EOD |
| 41 | +<?php |
| 42 | +
|
| 43 | +return [ |
| 44 | + // Database settings |
| 45 | + 'database' => [ |
| 46 | + 'user' => 'root', // Database username |
| 47 | + 'pass' => '', // Database password |
| 48 | + ], |
| 49 | + // Development settings |
| 50 | + 'development' => [ |
| 51 | + 'debug' => true, // Debugging |
| 52 | + 'errors' => 32767, // Error reporting |
| 53 | + ], |
| 54 | +]; |
| 55 | +
|
| 56 | +EOD; |
| 57 | + $actual = file_get_contents($filename); |
| 58 | + |
| 59 | + $this->assertEquals($expected, $actual); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @covers \ConfigWriter\Writers\PhpWriter::comment() |
| 64 | + */ |
| 65 | + public function testCStyleComment() |
| 66 | + { |
| 67 | + $reflection = new ReflectionClass(PhpWriter::class); |
| 68 | + $method = $reflection->getMethod('comment'); |
| 69 | + $method->setAccessible(true); |
| 70 | + |
| 71 | + $expected = '// Hello World'; |
| 72 | + $actual = $method->invokeArgs(new PhpWriter, ['Hello World', 'c-style']); |
| 73 | + |
| 74 | + $this->assertEquals($expected, $actual); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @covers \ConfigWriter\Writers\PhpWriter::comment() |
| 79 | + */ |
| 80 | + public function testPerlStyleComment() |
| 81 | + { |
| 82 | + $reflection = new ReflectionClass(PhpWriter::class); |
| 83 | + $method = $reflection->getMethod('comment'); |
| 84 | + $method->setAccessible(true); |
| 85 | + |
| 86 | + $expected = '# Hello World'; |
| 87 | + $actual = $method->invokeArgs(new PhpWriter, ['Hello World', 'perl-style']); |
| 88 | + |
| 89 | + $this->assertEquals($expected, $actual); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @covers \ConfigWriter\Writers\PhpWriter::comment() |
| 94 | + */ |
| 95 | + public function testMultiLineComment() |
| 96 | + { |
| 97 | + $reflection = new ReflectionClass(PhpWriter::class); |
| 98 | + $method = $reflection->getMethod('comment'); |
| 99 | + $method->setAccessible(true); |
| 100 | + |
| 101 | + $expected = '/* Hello World */'; |
| 102 | + $actual = $method->invokeArgs(new PhpWriter, ['Hello World', 'multi-line']); |
| 103 | + |
| 104 | + $this->assertEquals($expected, $actual); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @covers \ConfigWriter\Writers\PhpWriter::comment() |
| 109 | + */ |
| 110 | + public function testDocBlockComment() |
| 111 | + { |
| 112 | + $reflection = new ReflectionClass(PhpWriter::class); |
| 113 | + $method = $reflection->getMethod('comment'); |
| 114 | + $method->setAccessible(true); |
| 115 | + |
| 116 | + $expected = '/** Hello World */'; |
| 117 | + $actual = $method->invokeArgs(new PhpWriter, ['Hello World', 'doc-block']); |
| 118 | + |
| 119 | + $this->assertEquals($expected, $actual); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @covers \ConfigWriter\Writers\PhpWriter::comment() |
| 124 | + */ |
| 125 | + public function testUnknownComment() |
| 126 | + { |
| 127 | + $reflection = new ReflectionClass(PhpWriter::class); |
| 128 | + $method = $reflection->getMethod('comment'); |
| 129 | + $method->setAccessible(true); |
| 130 | + |
| 131 | + $expected = '// Hello World'; |
| 132 | + $actual = $method->invokeArgs(new PhpWriter, ['Hello World', 'unknown-comment']); |
| 133 | + |
| 134 | + $this->assertEquals($expected, $actual); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @covers \ConfigWriter\Writers\PhpWriter::getSupportedExtensions() |
| 139 | + */ |
| 140 | + public function testGetSupportedExtensions() |
| 141 | + { |
| 142 | + $expected = ['php']; |
| 143 | + $actual = PhpWriter::getSupportedExtensions(); |
| 144 | + |
| 145 | + $this->assertEquals($expected, $actual); |
| 146 | + } |
| 147 | +} |
0 commit comments