|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Command; |
| 4 | + |
| 5 | +use Database\Seeders\DatabaseSeeder; |
| 6 | +use Illuminate\Filesystem\Filesystem; |
| 7 | +use Laravel\Nova\DevTool\DevToolServiceProvider; |
| 8 | +use Orchestra\Canvas\LaravelServiceProvider; |
| 9 | +use Orchestra\Testbench\Foundation\Config; |
| 10 | +use Orchestra\Testbench\Foundation\TestbenchServiceProvider; |
| 11 | +use Orchestra\Workbench\Workbench; |
| 12 | +use Orchestra\Workbench\WorkbenchServiceProvider; |
| 13 | +use RuntimeException; |
| 14 | +use Workbench\Database\Seeders\DatabaseSeeder as WorkbenchDatabaseSeeder; |
| 15 | + |
| 16 | +use function Orchestra\Testbench\default_skeleton_path; |
| 17 | +use function Orchestra\Testbench\join_paths; |
| 18 | + |
| 19 | +abstract class CommandTestCase extends \Orchestra\Testbench\TestCase |
| 20 | +{ |
| 21 | + /** {@inheritDoc} */ |
| 22 | + #[\Override] |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + $filesystem = new Filesystem; |
| 26 | + $workingPath = static::stubWorkingPath(); |
| 27 | + |
| 28 | + $this->beforeApplicationDestroyed(function () use ($filesystem, $workingPath) { |
| 29 | + $filesystem->deleteDirectory($workingPath); |
| 30 | + unset($_ENV['TESTBENCH_WORKING_PATH']); |
| 31 | + Workbench::flush(); |
| 32 | + }); |
| 33 | + |
| 34 | + $_ENV['TESTBENCH_WORKING_PATH'] = $workingPath; |
| 35 | + $filesystem->ensureDirectoryExists($workingPath); |
| 36 | + $filesystem->copy(join_paths(__DIR__, 'stubs', 'composer.json'), join_paths($workingPath, 'composer.json')); |
| 37 | + $filesystem->copy(join_paths(__DIR__, 'stubs', 'phpunit.xml.dist'), join_paths($workingPath, 'phpunit.xml.dist')); |
| 38 | + |
| 39 | + parent::setUp(); |
| 40 | + } |
| 41 | + |
| 42 | + /** {@inheritDoc} */ |
| 43 | + #[\Override] |
| 44 | + protected function getPackageProviders($app) |
| 45 | + { |
| 46 | + return [ |
| 47 | + TestbenchServiceProvider::class, |
| 48 | + WorkbenchServiceProvider::class, |
| 49 | + LaravelServiceProvider::class, |
| 50 | + DevToolServiceProvider::class, |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + protected function defineEnvironment($app) |
| 55 | + { |
| 56 | + if (! defined('TESTBENCH_CORE')) { |
| 57 | + define('TESTBENCH_CORE', true); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Assert `workbench:devtool` or `workbench:install --devtool` command executed. |
| 63 | + */ |
| 64 | + protected function assertCommandExecutedWithDevTool(bool $prefix = true): void |
| 65 | + { |
| 66 | + $workingPath = static::stubWorkingPath(); |
| 67 | + |
| 68 | + $this->assertDirectoryExists(join_paths($workingPath, 'workbench', 'app', 'Models')); |
| 69 | + $this->assertDirectoryExists(join_paths($workingPath, 'workbench', 'app', 'Providers')); |
| 70 | + $this->assertDirectoryExists(join_paths($workingPath, 'workbench', 'database', 'factories')); |
| 71 | + $this->assertDirectoryExists(join_paths($workingPath, 'workbench', 'database', 'seeders')); |
| 72 | + |
| 73 | + $this->assertFileContains([ |
| 74 | + sprintf('namespace %sModels;', $prefix ? 'Workbench\App\\' : 'App\\'), |
| 75 | + 'class User extends Authenticatable', |
| 76 | + ], join_paths($workingPath, 'workbench', 'app', 'Models', 'User.php')); |
| 77 | + |
| 78 | + $this->assertFileContains([ |
| 79 | + sprintf('namespace %sNova;', $prefix ? 'Workbench\App\\' : 'App\\'), |
| 80 | + 'abstract class Resource extends NovaResource', |
| 81 | + ], join_paths($workingPath, 'workbench', 'app', 'Nova', 'Resource.php')); |
| 82 | + |
| 83 | + $this->assertFileContains([ |
| 84 | + sprintf('namespace %sNova;', $prefix ? 'Workbench\App\\' : 'App\\'), |
| 85 | + 'class User extends Resource', |
| 86 | + sprintf('public static $model = \%sModels\User::class;', $prefix ? 'Workbench\App\\' : 'App\\'), |
| 87 | + ], join_paths($workingPath, 'workbench', 'app', 'Nova', 'User.php')); |
| 88 | + |
| 89 | + $this->assertFileContains([ |
| 90 | + sprintf('namespace %sProviders;', $prefix ? 'Workbench\App\\' : 'App\\'), |
| 91 | + 'class NovaServiceProvider extends NovaApplicationServiceProvider', |
| 92 | + ], join_paths($workingPath, 'workbench', 'app', 'Providers', 'NovaServiceProvider.php')); |
| 93 | + |
| 94 | + $this->assertFileContains([ |
| 95 | + sprintf('namespace %sProviders;', $prefix ? 'Workbench\App\\' : 'App\\'), |
| 96 | + 'class WorkbenchServiceProvider extends ServiceProvider', |
| 97 | + ], join_paths($workingPath, 'workbench', 'app', 'Providers', 'WorkbenchServiceProvider.php')); |
| 98 | + |
| 99 | + $this->assertFileContains([ |
| 100 | + sprintf('namespace %sFactories;', $prefix ? 'Workbench\Database\\' : 'Database\\'), |
| 101 | + sprintf('use %sModels\User;', $prefix ? 'Workbench\App\\' : 'App\\'), |
| 102 | + 'class UserFactory extends Factory', |
| 103 | + ], join_paths($workingPath, 'workbench', 'database', 'factories', 'UserFactory.php')); |
| 104 | + |
| 105 | + $this->assertFileContains([ |
| 106 | + sprintf('namespace %sSeeders;', $prefix ? 'Workbench\Database\\' : 'Database\\'), |
| 107 | + sprintf('use %sFactories\UserFactory;', $prefix ? 'Workbench\Database\\' : 'Database\\'), |
| 108 | + 'class DatabaseSeeder extends Seeder', |
| 109 | + '// UserFactory::new()->times(10)->create();', |
| 110 | + 'UserFactory::new()->create([', |
| 111 | + ], join_paths($workingPath, 'workbench', 'database', 'seeders', 'DatabaseSeeder.php')); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Assert `workbench:install` command executed with `--no-devtool`. |
| 116 | + */ |
| 117 | + protected function assertCommandExecutedWithoutDevTool(bool $prefix = true): void |
| 118 | + { |
| 119 | + $workingPath = static::stubWorkingPath(); |
| 120 | + |
| 121 | + $this->assertDirectoryDoesNotExist(join_paths($workingPath, 'workbench', 'app')); |
| 122 | + $this->assertDirectoryDoesNotExist(join_paths($workingPath, 'workbench', 'database')); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Assert command executed with `workbench:install` or `workbench:devtool --install`. |
| 127 | + */ |
| 128 | + protected function assertCommandExecutedWithInstall(bool $prefix = true): void |
| 129 | + { |
| 130 | + $workingPath = static::stubWorkingPath(); |
| 131 | + |
| 132 | + $this->assertFileExists(join_paths($workingPath, 'testbench.yaml')); |
| 133 | + |
| 134 | + $config = Config::loadFromYaml($workingPath); |
| 135 | + |
| 136 | + $this->assertSame(default_skeleton_path(), $config['laravel']); |
| 137 | + $this->assertSame([ |
| 138 | + $prefix ? WorkbenchDatabaseSeeder::class : DatabaseSeeder::class, |
| 139 | + ], $config->seeders); |
| 140 | + $this->assertSame([ |
| 141 | + 'asset-publish', |
| 142 | + 'create-sqlite-db', |
| 143 | + 'db-wipe', |
| 144 | + 'migrate-fresh', |
| 145 | + ], $config->getWorkbenchAttributes()['build']); |
| 146 | + $this->assertSame([ |
| 147 | + 'laravel-assets', |
| 148 | + ], $config->getWorkbenchAttributes()['assets']); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Assert `workbench:install --basic` or `workbench:devtool --basic --install` command executed. |
| 153 | + */ |
| 154 | + protected function assertCommandFailedWithBasicInstall(bool $prefix = true): void |
| 155 | + { |
| 156 | + $this->expectException(RuntimeException::class); |
| 157 | + $this->expectExceptionMessage('Nova Devtool does not support installation with --basic` option'); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Assert `workbench:devtool` command executed with `--no-install` |
| 162 | + */ |
| 163 | + protected function assertCommandExecutedWithoutInstall(bool $prefix = true): void |
| 164 | + { |
| 165 | + $workingPath = static::stubWorkingPath(); |
| 166 | + $environmentFiles = collect(['.env', '.env.example', '.env.dist']); |
| 167 | + |
| 168 | + $this->assertFileDoesNotExist(join_paths($workingPath, 'testbench.yaml')); |
| 169 | + |
| 170 | + $environmentFiles->each(function ($env) use ($workingPath) { |
| 171 | + $this->assertFileDoesNotExist(join_paths($workingPath, 'workbench', $env)); |
| 172 | + }); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Assert from `environmentFileDataProviders` |
| 177 | + */ |
| 178 | + protected function assertFromEnvironmentFileDataProviders(?string $answer, bool $createEnvironmentFile): void |
| 179 | + { |
| 180 | + $workingPath = static::stubWorkingPath(); |
| 181 | + |
| 182 | + if (\is_null($answer) || $createEnvironmentFile === false) { |
| 183 | + collect(['.env', '.env.example', '.env.dist']) |
| 184 | + ->each(function ($file) use ($workingPath) { |
| 185 | + $this->assertFalse(is_file(join_paths($workingPath, 'workbench', $file))); |
| 186 | + }); |
| 187 | + } else { |
| 188 | + $this->assertTrue(is_file(join_paths($workingPath, 'workbench', $answer))); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Assert file does contains data. |
| 194 | + * |
| 195 | + * @param array<int, string> $contains |
| 196 | + */ |
| 197 | + protected function assertFileContains(array $contains, string $file, string $message = ''): void |
| 198 | + { |
| 199 | + $this->assertFileExists($file); |
| 200 | + |
| 201 | + $haystack = file_get_contents($file); |
| 202 | + |
| 203 | + foreach ($contains as $needle) { |
| 204 | + $this->assertStringContainsString($needle, $haystack, $message); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * `environmentFileDataProviders` data provider. |
| 210 | + */ |
| 211 | + public static function environmentFileDataProviders() |
| 212 | + { |
| 213 | + yield ['Skip exporting .env', false]; |
| 214 | + yield ['.env', true]; |
| 215 | + yield ['.env.example', true]; |
| 216 | + yield ['.env.dist', true]; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Get the stub working path. |
| 221 | + */ |
| 222 | + protected static function stubWorkingPath(): string |
| 223 | + { |
| 224 | + return join_paths(__DIR__, sprintf('%s_stubs', class_basename(static::class))); |
| 225 | + } |
| 226 | +} |
0 commit comments