TestCase.php 2.9 KB

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