Skip to content

Commit

Permalink
Install tests command
Browse files Browse the repository at this point in the history
  • Loading branch information
royduin committed Oct 6, 2022
1 parent f2f39ae commit 72da9c1
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 34 deletions.
45 changes: 45 additions & 0 deletions src/Commands/InstallTestsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Rapidez\Core\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class InstallTestsCommand extends Command
{
protected $signature = 'rapidez:install:tests';

protected $description = 'Install Rapidez Tests';

public function handle()
{
File::copyDirectory(base_path('vendor/rapidez/core/tests/Browser'), base_path('tests/Browser'));
File::copyDirectory(base_path('vendor/rapidez/core/tests/Feature'), base_path('tests/Feature'));
File::copy(base_path('vendor/rapidez/core/phpunit.dusk.xml'), base_path('phpunit.dusk.xml'));

foreach (File::files(base_path('tests/Feature')) as $file) {
file_put_contents($file->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 🚀');
}
}
2 changes: 2 additions & 0 deletions src/RapidezServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,6 +48,7 @@ protected function bootCommands(): self
IndexProductsCommand::class,
ValidateCommand::class,
InstallCommand::class,
InstallTestsCommand::class,
]);

return $this;
Expand Down
29 changes: 1 addition & 28 deletions tests/DuskTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
36 changes: 36 additions & 0 deletions tests/DuskTestCaseSetup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rapidez\Core\Tests;

use Laravel\Dusk\Browser;
use Rapidez\Core\Models\Product;

trait DuskTestCaseSetup
{
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', env('TEST_PRODUCT', '24-WB02'));
}
}
4 changes: 2 additions & 2 deletions tests/Feature/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ 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()
);
}
}

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());
}
}
}
4 changes: 0 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 72da9c1

Please sign in to comment.