diff --git a/src/Commands/InstallTestsCommand.php b/src/Commands/InstallTestsCommand.php new file mode 100644 index 000000000..e890ba95c --- /dev/null +++ b/src/Commands/InstallTestsCommand.php @@ -0,0 +1,45 @@ +getPathname(), str_replace( + 'use Rapidez\Core\Tests\TestCase;', + 'use Tests\TestCase;', + $file->getContents() + )); + } + + shell_exec('cd '.base_path().' && composer require --dev laravel/dusk && php artisan dusk:install && php artisan dusk:chrome-driver --detect'); + + $duskTestCaseFile = base_path('tests/DuskTestCase.php'); + file_put_contents($duskTestCaseFile, str(file_get_contents($duskTestCaseFile)) + ->replace('use CreatesApplication;', 'use CreatesApplication, DuskTestCaseSetup;') + ->replace('use Laravel\Dusk\TestCase as BaseTestCase;\n', 'use Laravel\Dusk\TestCase as BaseTestCase;\nuse Rapidez\Core\Tests\DuskTestCaseSetup;\n')); + + foreach (File::files(base_path('tests/Browser')) as $file) { + file_put_contents($file->getPathname(), str_replace( + 'use Rapidez\Core\Tests\DuskTestCase;', + 'use Tests\DuskTestCase;', + $file->getContents() + )); + } + + $this->info('Done 🚀'); + } +} diff --git a/src/RapidezServiceProvider.php b/src/RapidezServiceProvider.php index 8ad374714..a5459ff61 100644 --- a/src/RapidezServiceProvider.php +++ b/src/RapidezServiceProvider.php @@ -8,6 +8,7 @@ use Illuminate\Support\ServiceProvider; use Rapidez\Core\Commands\IndexProductsCommand; use Rapidez\Core\Commands\InstallCommand; +use Rapidez\Core\Commands\InstallTestsCommand; use Rapidez\Core\Commands\ValidateCommand; use Rapidez\Core\Http\Middleware\DetermineAndSetShop; use Rapidez\Core\Http\ViewComposers\ConfigComposer; @@ -47,6 +48,7 @@ protected function bootCommands(): self IndexProductsCommand::class, ValidateCommand::class, InstallCommand::class, + InstallTestsCommand::class, ]); return $this; diff --git a/tests/DuskTestCase.php b/tests/DuskTestCase.php index 9652f0914..4e6138d64 100644 --- a/tests/DuskTestCase.php +++ b/tests/DuskTestCase.php @@ -4,41 +4,14 @@ use BladeUI\Heroicons\BladeHeroiconsServiceProvider; use BladeUI\Icons\BladeIconsServiceProvider; -use Laravel\Dusk\Browser; use Orchestra\Testbench\Dusk\TestCase as BaseTestCase; use Rapidez\Core\Facades\Rapidez; -use Rapidez\Core\Models\Product; use Rapidez\Core\RapidezServiceProvider; use TorMorten\Eventy\EventServiceProvider; abstract class DuskTestCase extends BaseTestCase { - public string $flat; - - public Product $testProduct; - - protected function setUp(): void - { - parent::setUp(); - - Browser::macro('waitUntilAllAjaxCallsAreFinished', function ($pause = false) { - $this->waitUntil('!window.app.$data.loading', 10); - - if ($pause) { - $this->pause($pause); - } - - return $this; - }); - - $this->flat = (new Product())->getTable(); - - $this->testProduct = Product::selectAttributes([ - 'name', - 'price', - 'url_key', - ])->firstWhere($this->flat.'.sku', '24-WB02'); - } + use DuskTestCaseSetup; protected function getPackageProviders($app) { diff --git a/tests/DuskTestCaseSetup.php b/tests/DuskTestCaseSetup.php new file mode 100644 index 000000000..b0c7934e4 --- /dev/null +++ b/tests/DuskTestCaseSetup.php @@ -0,0 +1,36 @@ +waitUntil('!window.app.$data.loading', 10); + + if ($pause) { + $this->pause($pause); + } + + return $this; + }); + + $this->flat = (new Product())->getTable(); + + $this->testProduct = Product::selectAttributes([ + 'name', + 'price', + 'url_key', + ])->firstWhere($this->flat.'.sku', env('TEST_PRODUCT', '24-WB02')); + } +} diff --git a/tests/Feature/ProductTest.php b/tests/Feature/ProductTest.php index ab4d3e3aa..451df7a0c 100644 --- a/tests/Feature/ProductTest.php +++ b/tests/Feature/ProductTest.php @@ -12,7 +12,7 @@ public function test_supported_product_types_can_be_received() foreach (['simple', 'configurable', 'downloadable', 'grouped'] as $supportedType) { $this->assertInstanceOf( Product::class, - Product::selectAttributes(['sku'])->where($this->flat.'.type_id', $supportedType)->first() + Product::selectAttributes(['sku'])->where((new Product())->getTable().'.type_id', $supportedType)->first() ); } } @@ -20,7 +20,7 @@ public function test_supported_product_types_can_be_received() public function test_unsupported_product_types_can_not_be_received() { foreach (['bundle'] as $unsupportedType) { - $this->assertNull(Product::selectAttributes(['sku'])->where($this->flat.'.type_id', $unsupportedType)->first()); + $this->assertNull(Product::selectAttributes(['sku'])->where((new Product())->getTable().'.type_id', $unsupportedType)->first()); } } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 84b92dd21..5b041b6bf 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -11,14 +11,10 @@ class TestCase extends BaseTestCase { - public string $flat; - public function setUp(): void { parent::setUp(); - $this->flat = (new Product())->getTable(); - $this->setUpDatabase($this->app); }