From 86f88aad7ed46942183abecf56fbd627e6b736e2 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 24 Sep 2017 09:37:11 +0800 Subject: [PATCH] Add laravel browserkit-testing as dependency --- composer.json | 3 ++- src/CrudMake.php | 21 +++++++++++++++++ src/stubs/test-browserkit-base-class.stub | 21 +++++++++++++++++ src/stubs/test-feature.stub | 2 +- tests/Generators/FeatureTestGeneratorTest.php | 33 ++++++++++++++++++++++++++- tests/Generators/RouteWebGeneratorTest.php | 2 +- tests/TestCase.php | 1 + 7 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 src/stubs/test-browserkit-base-class.stub diff --git a/composer.json b/composer.json index c1861f5..114248d 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ }, "require": { "php": ">=7.0", - "illuminate/support": "5.5.*" + "illuminate/support": "5.5.*", + "laravel/browser-kit-testing": "^2.0" }, "require-dev": { "phpunit/phpunit": "~6.0", diff --git a/src/CrudMake.php b/src/CrudMake.php index c97233c..a72439d 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -122,6 +122,8 @@ class CrudMake extends Command public function generateTests() { + $this->createBrowserKitBaseTestClass(); + $featureTestPath = $this->makeDirectory(base_path('tests/Feature')); $this->files->put("{$featureTestPath}/Manage{$this->pluralModelName}Test.php", $this->getFeatureTestContent()); $this->info('Manage'.$this->pluralModelName.'Test generated.'); @@ -131,6 +133,20 @@ class CrudMake extends Command $this->info($this->modelName.'Test (model) generated.'); } + public function createBrowserKitBaseTestClass() + { + $testsPath = base_path('tests'); + if (! $this->files->isDirectory($testsPath)) { + $this->files->makeDirectory($testsPath, 0777, true, true); + } + + if (! $this->files->exists($testsPath.'/BrowserKitTest.php')) { + $this->files->put($testsPath.'/BrowserKitTest.php', $this->getBrowserKitBaseTestContent()); + } + + $this->info('BrowserKitTest generated.'); + } + public function generateResourceRoute() { $webRoutePath = $this->makeRouteFile(base_path('routes'), 'web.php'); @@ -174,6 +190,11 @@ class CrudMake extends Command return $this->replaceStubString($stub); } + public function getBrowserKitBaseTestContent() + { + return $this->files->get(__DIR__.'/stubs/test-browserkit-base-class.stub'); + } + public function getFeatureTestContent() { $stub = $this->files->get(__DIR__.'/stubs/test-feature.stub'); diff --git a/src/stubs/test-browserkit-base-class.stub b/src/stubs/test-browserkit-base-class.stub new file mode 100644 index 0000000..cd9f611 --- /dev/null +++ b/src/stubs/test-browserkit-base-class.stub @@ -0,0 +1,21 @@ +create(); + $this->actingAs($user); + + return $user; + } +} diff --git a/src/stubs/test-feature.stub b/src/stubs/test-feature.stub index 05193f2..09ada99 100644 --- a/src/stubs/test-feature.stub +++ b/src/stubs/test-feature.stub @@ -3,7 +3,7 @@ namespace Tests\Feature; use App\Master; -use Tests\TestCase; +use Tests\BrowserKitTestCase as TestCase; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; use Illuminate\Foundation\Testing\DatabaseMigrations; diff --git a/tests/Generators/FeatureTestGeneratorTest.php b/tests/Generators/FeatureTestGeneratorTest.php index 09c72cf..b305ad6 100644 --- a/tests/Generators/FeatureTestGeneratorTest.php +++ b/tests/Generators/FeatureTestGeneratorTest.php @@ -7,6 +7,37 @@ use Tests\TestCase; class FeatureTestGeneratorTest extends TestCase { /** @test */ + public function it_creates_browser_kit_base_test_class() + { + $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); + + $this->assertFileExists(base_path("tests/BrowserKitTest.php")); + $browserKitTestClassContent = "create(); + \$this->actingAs(\$user); + + return \$user; + } +} +"; + $this->assertEquals($browserKitTestClassContent, file_get_contents(base_path("tests/BrowserKitTest.php"))); + } + + /** @test */ public function it_creates_correct_feature_test_class_content() { $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); @@ -17,7 +48,7 @@ class FeatureTestGeneratorTest extends TestCase namespace Tests\Feature; use App\Item; -use Tests\TestCase; +use Tests\BrowserKitTestCase as TestCase; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; use Illuminate\Foundation\Testing\DatabaseMigrations; diff --git a/tests/Generators/RouteWebGeneratorTest.php b/tests/Generators/RouteWebGeneratorTest.php index af9bcb3..909b350 100644 --- a/tests/Generators/RouteWebGeneratorTest.php +++ b/tests/Generators/RouteWebGeneratorTest.php @@ -15,7 +15,7 @@ class RouteWebGeneratorTest extends TestCase $this->assertFileExists($routeWebPath); $routeWebFileContent = "assertEquals($routeWebFileContent, file_get_contents($routeWebPath)); } diff --git a/tests/TestCase.php b/tests/TestCase.php index b707c78..16d3bd3 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -28,6 +28,7 @@ abstract class TestCase extends BaseTestCase exec('rm '.database_path('migrations/*')); exec('rm -r '.resource_path('views/'.$this->tableName)); exec('rm -r '.base_path('routes')); + exec('rm '.base_path('tests/BrowserKitTest.php')); exec('rm -r '.base_path('tests/Feature')); exec('rm -r '.base_path('tests/Unit'));