Browse Source

Add laravel browserkit-testing as dependency

tags/0.1.0
Nafies Luthfi 8 years ago
parent
commit
86f88aad7e
  1. 3
      composer.json
  2. 21
      src/CrudMake.php
  3. 21
      src/stubs/test-browserkit-base-class.stub
  4. 2
      src/stubs/test-feature.stub
  5. 33
      tests/Generators/FeatureTestGeneratorTest.php
  6. 2
      tests/Generators/RouteWebGeneratorTest.php
  7. 1
      tests/TestCase.php

3
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",

21
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');

21
src/stubs/test-browserkit-base-class.stub

@ -0,0 +1,21 @@
<?php
namespace Tests;
use App\User;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
abstract class BrowserKitTestCase extends BaseTestCase
{
use CreatesApplication;
protected $baseUrl = 'http://localhost';
protected function loginAsUser()
{
$user = factory(User::class)->create();
$this->actingAs($user);
return $user;
}
}

2
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;

33
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 = "<?php
namespace Tests;
use App\User;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
abstract class BrowserKitTestCase extends BaseTestCase
{
use CreatesApplication;
protected \$baseUrl = 'http://localhost';
protected function loginAsUser()
{
\$user = factory(User::class)->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;

2
tests/Generators/RouteWebGeneratorTest.php

@ -15,7 +15,7 @@ class RouteWebGeneratorTest extends TestCase
$this->assertFileExists($routeWebPath);
$routeWebFileContent = "<?php
Route::apiResource('items', 'ItemsController';
Route::apiResource('items', 'ItemsController');
";
$this->assertEquals($routeWebFileContent, file_get_contents($routeWebPath));
}

1
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'));

Loading…
Cancel
Save