|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Commands; |
| 15 | + |
| 16 | +use CodeIgniter\Test\CIUnitTestCase; |
| 17 | +use CodeIgniter\Test\StreamFilterTrait; |
| 18 | +use PHPUnit\Framework\Attributes\Group; |
| 19 | + |
| 20 | +/** |
| 21 | + * @internal |
| 22 | + */ |
| 23 | +#[Group('Others')] |
| 24 | +final class WorkerCommandsTest extends CIUnitTestCase |
| 25 | +{ |
| 26 | + use StreamFilterTrait; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var list<string> |
| 30 | + */ |
| 31 | + private array $filesToCleanup = [ |
| 32 | + 'public/frankenphp-worker.php', |
| 33 | + 'Caddyfile', |
| 34 | + ]; |
| 35 | + |
| 36 | + protected function tearDown(): void |
| 37 | + { |
| 38 | + parent::tearDown(); |
| 39 | + |
| 40 | + $this->cleanupFiles(); |
| 41 | + } |
| 42 | + |
| 43 | + private function cleanupFiles(): void |
| 44 | + { |
| 45 | + foreach ($this->filesToCleanup as $file) { |
| 46 | + $path = ROOTPATH . $file; |
| 47 | + if (is_file($path)) { |
| 48 | + @unlink($path); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public function testWorkerInstallCreatesFiles(): void |
| 54 | + { |
| 55 | + command('worker:install'); |
| 56 | + |
| 57 | + $this->assertFileExists(ROOTPATH . 'public/frankenphp-worker.php'); |
| 58 | + $this->assertFileExists(ROOTPATH . 'Caddyfile'); |
| 59 | + |
| 60 | + $output = $this->getStreamFilterBuffer(); |
| 61 | + $this->assertStringContainsString('Worker mode files created successfully!', $output); |
| 62 | + $this->assertStringContainsString('File created:', $output); |
| 63 | + } |
| 64 | + |
| 65 | + public function testWorkerInstallSkipsExistingFilesWithoutForce(): void |
| 66 | + { |
| 67 | + command('worker:install'); |
| 68 | + $this->resetStreamFilterBuffer(); |
| 69 | + |
| 70 | + command('worker:install'); |
| 71 | + |
| 72 | + $output = $this->getStreamFilterBuffer(); |
| 73 | + $this->assertStringContainsString('Worker mode files already exist', $output); |
| 74 | + $this->assertStringContainsString('Use --force to overwrite', $output); |
| 75 | + } |
| 76 | + |
| 77 | + public function testWorkerInstallOverwritesWithForce(): void |
| 78 | + { |
| 79 | + command('worker:install'); |
| 80 | + |
| 81 | + $workerFile = ROOTPATH . 'public/frankenphp-worker.php'; |
| 82 | + file_put_contents($workerFile, '<?php // Modified content'); |
| 83 | + |
| 84 | + $this->resetStreamFilterBuffer(); |
| 85 | + |
| 86 | + command('worker:install --force'); |
| 87 | + |
| 88 | + $output = $this->getStreamFilterBuffer(); |
| 89 | + $this->assertStringContainsString('File overwritten:', $output); |
| 90 | + |
| 91 | + $content = file_get_contents($workerFile); |
| 92 | + $this->assertStringNotContainsString('// Modified content', (string) $content); |
| 93 | + $this->assertStringContainsString('FrankenPHP Worker', (string) $content); |
| 94 | + } |
| 95 | + |
| 96 | + public function testWorkerInstallShowsNextSteps(): void |
| 97 | + { |
| 98 | + command('worker:install'); |
| 99 | + |
| 100 | + $output = $this->getStreamFilterBuffer(); |
| 101 | + $this->assertStringContainsString('Next Steps:', $output); |
| 102 | + $this->assertStringContainsString('frankenphp run', $output); |
| 103 | + $this->assertStringContainsString('https://frankenphp.dev/docs/worker/', $output); |
| 104 | + } |
| 105 | + |
| 106 | + public function testWorkerUninstallRemovesFiles(): void |
| 107 | + { |
| 108 | + command('worker:install'); |
| 109 | + $this->resetStreamFilterBuffer(); |
| 110 | + |
| 111 | + command('worker:uninstall --force'); |
| 112 | + |
| 113 | + $this->assertFileDoesNotExist(ROOTPATH . 'public/frankenphp-worker.php'); |
| 114 | + $this->assertFileDoesNotExist(ROOTPATH . 'Caddyfile'); |
| 115 | + |
| 116 | + $output = $this->getStreamFilterBuffer(); |
| 117 | + $this->assertStringContainsString('Worker mode files removed successfully!', $output); |
| 118 | + $this->assertStringContainsString('File removed:', $output); |
| 119 | + } |
| 120 | + |
| 121 | + public function testWorkerUninstallWithNoFilesToRemove(): void |
| 122 | + { |
| 123 | + $this->cleanupFiles(); |
| 124 | + |
| 125 | + command('worker:uninstall --force'); |
| 126 | + |
| 127 | + $output = $this->getStreamFilterBuffer(); |
| 128 | + $this->assertStringContainsString('No worker mode files found to remove', $output); |
| 129 | + } |
| 130 | + |
| 131 | + public function testWorkerUninstallListsFilesToRemove(): void |
| 132 | + { |
| 133 | + command('worker:install'); |
| 134 | + $this->resetStreamFilterBuffer(); |
| 135 | + |
| 136 | + command('worker:uninstall --force'); |
| 137 | + |
| 138 | + $output = $this->getStreamFilterBuffer(); |
| 139 | + $this->assertStringContainsString('The following files will be removed:', $output); |
| 140 | + $this->assertStringContainsString('public/frankenphp-worker.php', $output); |
| 141 | + $this->assertStringContainsString('Caddyfile', $output); |
| 142 | + } |
| 143 | + |
| 144 | + public function testWorkerInstallAndUninstallCycle(): void |
| 145 | + { |
| 146 | + command('worker:install'); |
| 147 | + $this->assertFileExists(ROOTPATH . 'public/frankenphp-worker.php'); |
| 148 | + $this->assertFileExists(ROOTPATH . 'Caddyfile'); |
| 149 | + |
| 150 | + command('worker:uninstall --force'); |
| 151 | + $this->assertFileDoesNotExist(ROOTPATH . 'public/frankenphp-worker.php'); |
| 152 | + $this->assertFileDoesNotExist(ROOTPATH . 'Caddyfile'); |
| 153 | + } |
| 154 | + |
| 155 | + public function testWorkerInstallCreatesValidPHPFile(): void |
| 156 | + { |
| 157 | + command('worker:install'); |
| 158 | + |
| 159 | + $workerFile = ROOTPATH . 'public/frankenphp-worker.php'; |
| 160 | + $this->assertFileExists($workerFile); |
| 161 | + |
| 162 | + $content = file_get_contents($workerFile); |
| 163 | + $this->assertStringStartsWith('<?php', $content); |
| 164 | + |
| 165 | + $this->assertStringContainsString('frankenphp_handle_request', (string) $content); |
| 166 | + $this->assertStringContainsString('DatabaseConfig::validateForWorkerMode', (string) $content); |
| 167 | + $this->assertStringContainsString('DatabaseConfig::cleanupForWorkerMode', (string) $content); |
| 168 | + } |
| 169 | + |
| 170 | + public function testWorkerInstallCreatesValidCaddyfile(): void |
| 171 | + { |
| 172 | + command('worker:install'); |
| 173 | + |
| 174 | + $caddyfile = ROOTPATH . 'Caddyfile'; |
| 175 | + $this->assertFileExists($caddyfile); |
| 176 | + |
| 177 | + $content = file_get_contents($caddyfile); |
| 178 | + |
| 179 | + $this->assertStringContainsString('frankenphp', (string) $content); |
| 180 | + $this->assertStringContainsString('worker', (string) $content); |
| 181 | + $this->assertStringContainsString('public/frankenphp-worker.php', (string) $content); |
| 182 | + } |
| 183 | +} |
0 commit comments