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