TestCase.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Tests;
  3. use Dcat\Admin\Models\Administrator;
  4. use Illuminate\Filesystem\Filesystem;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Schema;
  8. use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
  9. class TestCase extends BaseTestCase
  10. {
  11. use CreatesApplication;
  12. protected $baseUrl = 'http://localhost:8000';
  13. /**
  14. * @var Administrator
  15. */
  16. protected $user;
  17. protected $login = false;
  18. public function setUp(): void
  19. {
  20. parent::setUp();
  21. $adminConfig = require __DIR__.'/config/admin.php';
  22. $this->app['config']->set('database.default', 'mysql');
  23. $this->app['config']->set('database.connections.mysql.host', env('MYSQL_HOST', 'localhost'));
  24. $this->app['config']->set('database.connections.mysql.database', 'laravel_dcat_admin_test');
  25. $this->app['config']->set('database.connections.mysql.username', 'root');
  26. $this->app['config']->set('database.connections.mysql.password', '');
  27. $this->app['config']->set('app.key', 'AckfSECXIvnK5r28GVIWUAxmbBSjTsmF');
  28. $this->app['config']->set('filesystems', require __DIR__.'/config/filesystems.php');
  29. $this->app['config']->set('admin', $adminConfig);
  30. foreach (Arr::dot(Arr::get($adminConfig, 'auth'), 'auth.') as $key => $value) {
  31. $this->app['config']->set($key, $value);
  32. }
  33. $this->artisan('vendor:publish', ['--provider' => 'Dcat\Admin\AdminServiceProvider']);
  34. Schema::defaultStringLength(191);
  35. $this->artisan('admin:install');
  36. $this->migrateTestTables();
  37. if (file_exists($routes = admin_path('routes.php'))) {
  38. require $routes;
  39. }
  40. require __DIR__.'/routes.php';
  41. require __DIR__.'/seeds/factory.php';
  42. view()->addNamespace('admin-tests', __DIR__.'/views');
  43. if ($this->login) {
  44. $this->be($this->user = Administrator::first(), 'admin');
  45. }
  46. }
  47. public function tearDown(): void
  48. {
  49. (new \CreateAdminTables())->down();
  50. (new \CreateTestTables())->down();
  51. DB::select("delete from `migrations` where `migration` = '2016_01_04_173148_create_admin_tables'");
  52. DB::select("delete from `migrations` where `migration` = '2016_11_22_093148_create_test_tables'");
  53. parent::tearDown();
  54. }
  55. /**
  56. * run package database migrations.
  57. *
  58. * @return void
  59. */
  60. public function migrateTestTables()
  61. {
  62. $fileSystem = new Filesystem();
  63. $fileSystem->requireOnce(__DIR__.'/migrations/2016_11_22_093148_create_test_tables.php');
  64. (new \CreateTestTables())->up();
  65. }
  66. }