Browse Source

tests

update
jqh 5 years ago
parent
commit
858fb0d8ea
6 changed files with 192 additions and 82 deletions
  1. 2 1
      composer.json
  2. 25 0
      tests/Browser/LoginTest.php
  3. 89 0
      tests/CreatesApplication.php
  4. 16 2
      tests/DuskTestCase.php
  5. 2 79
      tests/TestCase.php
  6. 58 0
      tests/artisan

+ 2 - 1
composer.json

@@ -26,7 +26,8 @@
         "phpunit/phpunit": "^7.5",
         "fzaninotto/faker": "^1.4",
         "mockery/mockery": "^1.0",
-        "matt-allan/laravel-code-style": "^0.3.0"
+        "matt-allan/laravel-code-style": "^0.3.0",
+        "beyondcode/dusk-dashboard": "^1.2"
     },
     "autoload": {
         "psr-4": {

+ 25 - 0
tests/Browser/LoginTest.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace Dcat\Admin\Tests\Browser;
+
+use Laravel\Dusk\Browser;
+use Dcat\Admin\Tests\DuskTestCase;
+
+/**
+ * @group login
+ */
+class LoginTest extends DuskTestCase
+{
+    /**
+     * A Dusk test example.
+     *
+     * @return void
+     */
+    public function testExample()
+    {
+        $this->browse(function (Browser $browser) {
+            $browser->visit('/admin')
+                ->assertSee('Dcat Admin');
+        });
+    }
+}

+ 89 - 0
tests/CreatesApplication.php

@@ -2,7 +2,12 @@
 
 namespace Dcat\Admin\Tests;
 
+use Dcat\Admin\Models\Administrator;
+use Illuminate\Filesystem\Filesystem;
 use Illuminate\Contracts\Console\Kernel;
+use Illuminate\Support\Arr;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Schema;
 
 trait CreatesApplication
 {
@@ -23,4 +28,88 @@ trait CreatesApplication
 
         return $app;
     }
+
+    protected function boot()
+    {
+        $this->config();
+
+        $this->artisan('admin:publish');
+
+        Schema::defaultStringLength(191);
+
+        $this->artisan('admin:install');
+
+        $this->migrateTestTables();
+
+        if (file_exists($routes = admin_path('routes.php'))) {
+            require $routes;
+        }
+
+        require __DIR__.'/routes.php';
+
+        require __DIR__.'/resources/seeds/factory.php';
+
+        view()->addNamespace('admin-tests', __DIR__.'/resources/views');
+
+        if ($this->login) {
+            $this->be($this->getUser(), 'admin');
+        }
+    }
+
+    protected function destory()
+    {
+        (new \CreateAdminTables())->down();
+
+        (new \CreateTestTables())->down();
+
+        DB::select("delete from `migrations` where `migration` = '2016_01_04_173148_create_admin_tables'");
+        DB::select("delete from `migrations` where `migration` = '2016_11_22_093148_create_test_tables'");
+    }
+
+    /**
+     * run package database migrations.
+     *
+     * @return void
+     */
+    public function migrateTestTables()
+    {
+        $fileSystem = new Filesystem();
+
+        $fileSystem->requireOnce(__DIR__.'/resources/migrations/2016_11_22_093148_create_test_tables.php');
+
+        (new \CreateTestTables())->up();
+    }
+
+    protected function config()
+    {
+        $adminConfig = require __DIR__.'/resources/config/admin.php';
+
+        $config = $this->app['config'];
+
+        $config->set('database.default', 'mysql');
+        $config->set('database.connections.mysql.host', env('MYSQL_HOST', 'localhost'));
+        $config->set('database.connections.mysql.database', 'laravel_dcat_admin_test');
+        $config->set('database.connections.mysql.username', env('MYSQL_USER', 'root'));
+        $config->set('database.connections.mysql.password', env('MYSQL_PASSWORD', ''));
+        $config->set('app.key', 'AckfSECXIvnK5r28GVIWUAxmbBSjTsmF');
+        $config->set('filesystems', require __DIR__.'/resources/config/filesystems.php');
+        $config->set('admin', $adminConfig);
+        $config->set('app.debug', true);
+
+        foreach (Arr::dot(Arr::get($adminConfig, 'auth'), 'auth.') as $key => $value) {
+            $this->app['config']->set($key, $value);
+        }
+    }
+
+    /**
+     * @return Administrator
+     */
+    protected function getUser()
+    {
+        if ($this->user) {
+            return $this->user;
+        }
+
+        return $this->user = Administrator::first();
+    }
 }

+ 16 - 2
tests/DuskTestCase.php

@@ -11,8 +11,7 @@ use Laravel\Dusk\TestCase as BaseTestCase;
 
 abstract class DuskTestCase extends BaseTestCase
 {
-    use CreatesApplication,
-        BasicTestCase;
+    use CreatesApplication;
 
     /**
      * @var Administrator
@@ -26,6 +25,21 @@ abstract class DuskTestCase extends BaseTestCase
         $browser->loginAs($this->getUser(), 'admin');
     }
 
+    public function setUp(): void
+    {
+        parent::setUp();
+
+        $this->boot();
+    }
+
+
+    public function tearDown(): void
+    {
+        $this->destory();
+
+        parent::tearDown();
+    }
+
     /**
      * Prepare for Dusk test execution.
      *

+ 2 - 79
tests/TestCase.php

@@ -3,10 +3,6 @@
 namespace Dcat\Admin\Tests;
 
 use Dcat\Admin\Models\Administrator;
-use Illuminate\Filesystem\Filesystem;
-use Illuminate\Support\Arr;
-use Illuminate\Support\Facades\DB;
-use Illuminate\Support\Facades\Schema;
 use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
 
 class TestCase extends BaseTestCase
@@ -26,87 +22,14 @@ class TestCase extends BaseTestCase
     {
         parent::setUp();
 
-        $this->config();
-
-        $this->artisan('admin:publish');
-
-        Schema::defaultStringLength(191);
-
-        $this->artisan('admin:install');
-
-        $this->migrateTestTables();
-
-        if (file_exists($routes = admin_path('routes.php'))) {
-            require $routes;
-        }
-
-        require __DIR__.'/routes.php';
-
-        require __DIR__.'/resources/seeds/factory.php';
-
-        view()->addNamespace('admin-tests', __DIR__.'/resources/views');
-
-        if ($this->login) {
-            $this->be($this->getUser(), 'admin');
-        }
+        $this->boot();
     }
 
-    protected function config()
-    {
-        $adminConfig = require __DIR__.'/resources/config/admin.php';
-
-        $config = $this->app['config'];
-
-        $config->set('database.default', 'mysql');
-        $config->set('database.connections.mysql.host', env('MYSQL_HOST', 'localhost'));
-        $config->set('database.connections.mysql.database', 'laravel_dcat_admin_test');
-        $config->set('database.connections.mysql.username', env('MYSQL_USER', 'root'));
-        $config->set('database.connections.mysql.password', env('MYSQL_PASSWORD', ''));
-        $config->set('app.key', 'AckfSECXIvnK5r28GVIWUAxmbBSjTsmF');
-        $config->set('filesystems', require __DIR__.'/resources/config/filesystems.php');
-        $config->set('admin', $adminConfig);
-        $config->set('app.debug', true);
-
-        foreach (Arr::dot(Arr::get($adminConfig, 'auth'), 'auth.') as $key => $value) {
-            $this->app['config']->set($key, $value);
-        }
-    }
-
-    /**
-     * @return Administrator
-     */
-    protected function getUser()
-    {
-        if ($this->user) {
-            return $this->user;
-        }
-
-        return $this->user = Administrator::first();
-    }
 
     public function tearDown(): void
     {
-        (new \CreateAdminTables())->down();
-
-        (new \CreateTestTables())->down();
-
-        DB::select("delete from `migrations` where `migration` = '2016_01_04_173148_create_admin_tables'");
-        DB::select("delete from `migrations` where `migration` = '2016_11_22_093148_create_test_tables'");
+        $this->destory();
 
         parent::tearDown();
     }
-
-    /**
-     * run package database migrations.
-     *
-     * @return void
-     */
-    public function migrateTestTables()
-    {
-        $fileSystem = new Filesystem();
-
-        $fileSystem->requireOnce(__DIR__.'/resources/migrations/2016_11_22_093148_create_test_tables.php');
-
-        (new \CreateTestTables())->up();
-    }
 }

+ 58 - 0
tests/artisan

@@ -0,0 +1,58 @@
+#!/usr/bin/env php
+<?php
+
+define('LARAVEL_START', microtime(true));
+
+/*
+|--------------------------------------------------------------------------
+| Register The Auto Loader
+|--------------------------------------------------------------------------
+|
+| Composer provides a convenient, automatically generated class loader
+| for our application. We just need to utilize it! We'll require it
+| into the script here so that we do not have to worry about the
+| loading of any our classes "manually". Feels great to relax.
+|
+*/
+
+require __DIR__.'/../vendor/autoload.php';
+
+$app = require_once __DIR__.'/../vendor/laravel/laravel/bootstrap/app.php';
+
+/*
+|--------------------------------------------------------------------------
+| Run The Artisan Application
+|--------------------------------------------------------------------------
+|
+| When we run the console application, the current CLI command will be
+| executed in this console and the response sent back to a terminal
+| or another output device for the developers. Here goes nothing!
+|
+*/
+
+$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
+
+$status = $kernel->handle(
+    $input = new Symfony\Component\Console\Input\ArgvInput,
+    new Symfony\Component\Console\Output\ConsoleOutput
+);
+
+$app['env'] = false;
+
+$app->register(\Laravel\Dusk\DuskServiceProvider::class);
+$app->register(\BeyondCode\DuskDashboard\DuskDashboardServiceProvider::class);
+
+/*
+|--------------------------------------------------------------------------
+| Shutdown The Application
+|--------------------------------------------------------------------------
+|
+| Once Artisan has finished running, we will fire off the shutdown events
+| so that any final work may be done by the application before we shut
+| down the process. This is the last thing to happen to the request.
+|
+*/
+
+$kernel->terminate($input, $status);
+
+exit($status);