Browse Source

单元测试

jqh 5 years ago
parent
commit
0ce8f6ac73

+ 2 - 2
.travis.yml

@@ -31,8 +31,7 @@ install:
   - rm -rf ./laravel-tests/tests
   - cp -rf ./tests ./laravel-tests/tests
   - cp -f ./phpunit.dusk.xml ./laravel-tests
-  - rm -f ./laravel-tests/.env
-  - mv -f ./.env.testing ./laravel-tests/.env
+  - cp -f ./.env.testing ./laravel-tests/.env
   - cd ./laravel-tests
   - php artisan admin:composer-config
   - travis_retry composer require dcat/laravel-admin:*@dev
@@ -41,6 +40,7 @@ install:
   - php artisan admin:install
   - php artisan migrate:rollback
   - php artisan dusk:chrome-driver
+  - cp -f ./tests/routes ./app/Admin/
 
 before_script:
   - export DISPLAY=:99.0

+ 1 - 1
src/Form/Concerns/HasFiles.php

@@ -56,7 +56,7 @@ trait HasFiles
         if ($field = $this->builder->field($column)) {
             return $field;
         }
-        
+
         if (mb_strpos($column, '.')) {
             [$relation, $column] = explode('.', $column);
 

+ 5 - 4
tests/Browser/Components/Grid/RowSelector.php

@@ -17,7 +17,7 @@ class RowSelector extends Component
      */
     public function selector()
     {
-        return '@item';
+        return '@container';
     }
 
     /**
@@ -28,7 +28,7 @@ class RowSelector extends Component
      */
     public function assert(Browser $browser)
     {
-        $browser->assertVisible('table thead th .checkbox-grid');
+//        $browser->assertVisible('table:visible thead th .checkbox-grid');
     }
 
     /**
@@ -39,6 +39,7 @@ class RowSelector extends Component
     public function elements()
     {
         return [
+            '@container' => '#grid-table',
             '@all' => 'input.select-all',
             '@item' => 'input.grid-row-checkbox',
         ];
@@ -58,7 +59,7 @@ class RowSelector extends Component
             $browser->script(
                 <<<JS
 setTimeout(function () {
-    $('{$this->formatSelector($browser)}[data-id="{$v}"]').prop('checked', true);
+    $('{$this->formatSelector($browser, '@item')}[data-id="{$v}"]').prop('checked', true);
 }, 10)
 JS
             );
@@ -78,7 +79,7 @@ JS
     {
         $browser->script("Dcat.ready(
             setTimeout(function () {
-                $('.grid-select-all').first().click()
+                $('{$this->formatSelector($browser)} .select-all').first().click()
             }, 10)
         );");
 

+ 22 - 0
tests/Browser/HasManyTest.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace Tests\Browser;
+
+use Laravel\Dusk\Browser;
+use Tests\TestCase;
+
+/**
+ * 一对多表单功能测试.
+ *
+ * @group has-many
+ */
+class HasManyTest extends TestCase
+{
+    public function testCreate()
+    {
+        $this->browse(function (Browser $browser) {
+            $browser->visit(test_admin_path('tests/painters/create'))
+                ->assertPathIs(test_admin_path('tests/painters/create'));
+        });
+    }
+}

+ 74 - 0
tests/Controllers/PainterController.php

@@ -0,0 +1,74 @@
+<?php
+
+namespace Tests\Controllers;
+
+use Tests\Models\Painter;
+use Dcat\Admin\Form;
+use Dcat\Admin\Grid;
+use Dcat\Admin\Show;
+use Dcat\Admin\Controllers\AdminController;
+
+class PainterController extends AdminController
+{
+    /**
+     * Make a grid builder.
+     *
+     * @return Grid
+     */
+    protected function grid()
+    {
+        return Grid::make(new Painter(), function (Grid $grid) {
+            $grid->id->sortable();
+            $grid->username;
+            $grid->bio;
+            $grid->created_at;
+            $grid->updated_at->sortable();
+
+            $grid->filter(function (Grid\Filter $filter) {
+                $filter->equal('id');
+
+            });
+        });
+    }
+
+    /**
+     * Make a show builder.
+     *
+     * @param mixed $id
+     *
+     * @return Show
+     */
+    protected function detail($id)
+    {
+        return Show::make($id, new Painter(), function (Show $show) {
+            $show->id;
+            $show->username;
+            $show->bio;
+            $show->created_at;
+            $show->updated_at;
+        });
+    }
+
+    /**
+     * Make a form builder.
+     *
+     * @return Form
+     */
+    protected function form()
+    {
+        return Form::make(Painter::with('paintings'), function (Form $form) {
+            $form->display('id', 'ID');
+
+            $form->text('username')->rules('required');
+            $form->textarea('bio')->rules('required');
+
+            $form->hasMany('paintings', function (Form\NestedForm $form) {
+                $form->text('title');
+                $form->textarea('body');
+                $form->datetime('completed_at');
+            });
+
+            $form->display('created_at', 'Created At');
+        });
+    }
+}

+ 0 - 6
tests/CreatesApplication.php

@@ -33,14 +33,8 @@ trait CreatesApplication
 
         $this->migrateTestTables();
 
-        if (file_exists($routes = admin_path('routes.php'))) {
-            require $routes;
-        }
-
         require __DIR__.'/helpers.php';
 
-        require __DIR__.'/routes.php';
-
         require __DIR__.'/resources/seeds/factory.php';
 
         view()->addNamespace('admin-tests', __DIR__.'/resources/views');

+ 15 - 0
tests/Models/Painter.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace Tests\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Painter extends Model
+{
+    protected $table = 'demo_painters';
+
+    public function paintings()
+    {
+        return $this->hasMany(Painting::class, 'painter_id');
+    }
+}

+ 17 - 0
tests/Models/Painting.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace Tests\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Painting extends Model
+{
+    protected $table = 'demo_paintings';
+
+    protected $fillable = ['title', 'body', 'completed_at'];
+
+    public function painter()
+    {
+        return $this->belongsTo(Painter::class, 'painter_id');
+    }
+}

+ 19 - 0
tests/resources/migrations/2016_11_22_093148_create_test_tables.php

@@ -1,5 +1,6 @@
 <?php
 
+use Illuminate\Support\Facades\Schema;
 use Illuminate\Database\Migrations\Migration;
 use Illuminate\Database\Schema\Blueprint;
 
@@ -78,6 +79,22 @@ class CreateTestTables extends Migration
             $table->index(['user_id', 'tag_id']);
             $table->timestamps();
         });
+
+        Schema::create('test_painters', function (Blueprint $table) {
+            $table->increments('id');
+            $table->string('username')->default('');
+            $table->string('bio')->default('');
+            $table->timestamps();
+        });
+
+        Schema::create('test_paintings', function (Blueprint $table) {
+            $table->increments('id');
+            $table->string('painter_id')->default('');
+            $table->string('title')->default('');
+            $table->string('body')->nullable();
+            $table->timestamp('completed_at');
+            $table->timestamps();
+        });
     }
 
     /**
@@ -94,5 +111,7 @@ class CreateTestTables extends Migration
         Schema::dropIfExists('test_user_profiles');
         Schema::dropIfExists('test_tags');
         Schema::dropIfExists('test_user_tags');
+        Schema::dropIfExists('test_painters');
+        Schema::dropIfExists('test_paintings');
     }
 }

+ 17 - 1
tests/routes.php

@@ -1,10 +1,26 @@
 <?php
 
+use Illuminate\Routing\Router;
+use Illuminate\Support\Facades\Route;
+use Dcat\Admin\Admin;
+
+Admin::routes();
+
+Route::group([
+    'prefix'     => config('admin.route.prefix'),
+    'namespace'  => config('admin.route.namespace'),
+    'middleware' => config('admin.route.middleware'),
+], function (Router $router) {
+    $router->get('/', 'HomeController@index');
+});
+
+
 Route::group([
     'prefix'     => config('admin.route.prefix'),
     'namespace'  => 'Tests\Controllers',
     'middleware' => ['web', 'admin'],
-], function ($router) {
+], function (Router $router) {
     $router->resource('tests/users', UserController::class);
     $router->resource('tests/report', ReportController::class);
+    $router->resource('tests/painters', PainterController::class);
 });