|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of Aplus Framework Dev Commands Library. |
| 4 | + * |
| 5 | + * (c) Natan Felles <[email protected]> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | +namespace Tests\CLI\Commands; |
| 11 | + |
| 12 | +use Framework\Config\Config; |
| 13 | +use Framework\Testing\TestCase; |
| 14 | +use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses; |
| 15 | + |
| 16 | +#[RunTestsInSeparateProcesses] |
| 17 | +final class MergeConfigsTest extends TestCase |
| 18 | +{ |
| 19 | + protected function prepareDefaults() : void |
| 20 | + { |
| 21 | + $this->config = new Config([ |
| 22 | + 'console' => [ |
| 23 | + 'default' => [ |
| 24 | + 'directories' => [ |
| 25 | + __DIR__ . '/../src', |
| 26 | + ], |
| 27 | + ], |
| 28 | + ], |
| 29 | + ]); |
| 30 | + parent::prepareDefaults(); |
| 31 | + } |
| 32 | + |
| 33 | + protected function makeDir(string $relativePath) : string |
| 34 | + { |
| 35 | + $dir = __DIR__ . \DIRECTORY_SEPARATOR . $relativePath; |
| 36 | + return \str_replace(' ', '\ ', $dir); |
| 37 | + } |
| 38 | + |
| 39 | + protected function runCli(string $relativePath, string $append = '') : void |
| 40 | + { |
| 41 | + $dir = $this->makeDir($relativePath); |
| 42 | + $append = $append !== '' ? ' ' . $append : ''; |
| 43 | + $this->app->runCli('mergeconfigs ' . $dir . $append); |
| 44 | + } |
| 45 | + |
| 46 | + public function testNoDir() : void |
| 47 | + { |
| 48 | + $this->app->runCli('mergeconfigs'); |
| 49 | + self::assertStdoutContains('<?php'); |
| 50 | + } |
| 51 | + |
| 52 | + public function testCustomExtension() : void |
| 53 | + { |
| 54 | + $this->app->runCli('mergeconfigs --extension=.php'); |
| 55 | + self::assertStdoutContains('<?php'); |
| 56 | + } |
| 57 | + |
| 58 | + public function testInvalidDir() : void |
| 59 | + { |
| 60 | + $this->expectException(\RuntimeException::class); |
| 61 | + $path = $this->makeDir('foo'); |
| 62 | + $this->expectExceptionMessage('Config directory "' . $path . '" does not exist'); |
| 63 | + $this->runCli('foo'); |
| 64 | + } |
| 65 | + |
| 66 | + public function testEmptyDir() : void |
| 67 | + { |
| 68 | + $this->runCli('configs/empty-dir'); |
| 69 | + self::assertStdoutContains('<?php'); |
| 70 | + self::assertStdoutContains('// Do not edit this file. It is created automatically.'); |
| 71 | + self::assertStdoutContains('return array ('); |
| 72 | + self::assertStdoutContains(');'); |
| 73 | + self::assertStdoutNotContains("'bar' =>"); |
| 74 | + } |
| 75 | + |
| 76 | + public function testOk() : void |
| 77 | + { |
| 78 | + $this->runCli('configs/ok'); |
| 79 | + self::assertStdoutContains('<?php'); |
| 80 | + self::assertStdoutContains('// Do not edit this file. It is created automatically.'); |
| 81 | + self::assertStdoutContains('return array ('); |
| 82 | + self::assertStdoutContains("'bar' =>"); |
| 83 | + self::assertStdoutContains("'key' => 'value'"); |
| 84 | + self::assertStdoutContains("'other' =>"); |
| 85 | + self::assertStdoutContains("'foo' => 'baz'"); |
| 86 | + self::assertStdoutContains(');'); |
| 87 | + } |
| 88 | + |
| 89 | + public function testNotArray() : void |
| 90 | + { |
| 91 | + $this->expectException(\RuntimeException::class); |
| 92 | + $path = \realpath(__DIR__ . '/configs/not-array/foo.php'); |
| 93 | + $this->expectExceptionMessage('Config file "' . $path . '" does not return an array'); |
| 94 | + $this->runCli('configs/not-array'); |
| 95 | + } |
| 96 | + |
| 97 | + public function testNotArrayWithIgnore() : void |
| 98 | + { |
| 99 | + $this->runCli('configs/not-array', '-i'); |
| 100 | + self::assertStdoutContains('<?php'); |
| 101 | + } |
| 102 | + |
| 103 | + public function testEmptyArray() : void |
| 104 | + { |
| 105 | + $this->expectException(\RuntimeException::class); |
| 106 | + $path = \realpath(__DIR__ . '/configs/empty-array/foo.php'); |
| 107 | + $this->expectExceptionMessage('Config file "' . $path . '" return an empty array'); |
| 108 | + $this->runCli('configs/empty-array'); |
| 109 | + } |
| 110 | + |
| 111 | + public function testEmptyArrayWithIgnore() : void |
| 112 | + { |
| 113 | + $this->runCli('configs/empty-array', '-i'); |
| 114 | + self::assertStdoutContains('<?php'); |
| 115 | + } |
| 116 | + |
| 117 | + public function testInvalidKeys() : void |
| 118 | + { |
| 119 | + $this->expectException(\RuntimeException::class); |
| 120 | + $path = \realpath(__DIR__ . '/configs/invalid-keys/foo.php'); |
| 121 | + $this->expectExceptionMessage('Config file "' . $path . '" return invalid keys (must be strings)'); |
| 122 | + $this->runCli('configs/invalid-keys'); |
| 123 | + } |
| 124 | + |
| 125 | + public function testInvalidKeysWithIgnore() : void |
| 126 | + { |
| 127 | + $this->runCli('configs/invalid-keys', '-i'); |
| 128 | + self::assertStdoutContains('<?php'); |
| 129 | + } |
| 130 | + |
| 131 | + public function testInvalidValues() : void |
| 132 | + { |
| 133 | + $this->expectException(\RuntimeException::class); |
| 134 | + $path = \realpath(__DIR__ . '/configs/invalid-values/foo.php'); |
| 135 | + $this->expectExceptionMessage('Config file "' . $path . '" return invalid values (must be arrays)'); |
| 136 | + $this->runCli('configs/invalid-values'); |
| 137 | + } |
| 138 | + |
| 139 | + public function testInvalidValuesWithIgnore() : void |
| 140 | + { |
| 141 | + $this->runCli('configs/invalid-values', '-i'); |
| 142 | + self::assertStdoutContains('<?php'); |
| 143 | + } |
| 144 | +} |
0 commit comments