CreatesApplication.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Tests;
  3. use Dcat\Admin\Models\Administrator;
  4. use Illuminate\Contracts\Console\Kernel;
  5. use Illuminate\Filesystem\Filesystem;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Support\Facades\Artisan;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Schema;
  10. trait CreatesApplication
  11. {
  12. public function createApplication()
  13. {
  14. $app = require $this->getAppPath();
  15. $app->make(Kernel::class)->bootstrap();
  16. return $app;
  17. }
  18. protected function boot()
  19. {
  20. $this->config();
  21. $this->artisan('admin:publish');
  22. Schema::defaultStringLength(191);
  23. $this->artisan('admin:install');
  24. $this->migrateTestTables();
  25. if (file_exists($routes = admin_path('routes.php'))) {
  26. require $routes;
  27. }
  28. require __DIR__.'/helpers.php';
  29. require __DIR__.'/routes.php';
  30. require __DIR__.'/resources/seeds/factory.php';
  31. view()->addNamespace('admin-tests', __DIR__.'/resources/views');
  32. }
  33. protected function config()
  34. {
  35. $adminConfig = require __DIR__.'/resources/config/admin.php';
  36. $config = $this->app['config'];
  37. $config->set('database.default', 'mysql');
  38. $config->set('database.connections.mysql.host', env('DB_HOST', 'localhost'));
  39. $config->set('database.connections.mysql.database', env('DB_DATABASE', 'laravel_dcat_admin_test'));
  40. $config->set('database.connections.mysql.username', env('DB_USERNAME', 'root'));
  41. $config->set('database.connections.mysql.password', env('DB_PASSWORD', ''));
  42. $config->set('app.key', 'AckfSECXIvnK5r28GVIWUAxmbBSjTsmF');
  43. $config->set('filesystems', require __DIR__.'/resources/config/filesystems.php');
  44. $config->set('admin', $adminConfig);
  45. $config->set('app.debug', true);
  46. foreach (Arr::dot(Arr::get($adminConfig, 'auth'), 'auth.') as $key => $value) {
  47. $this->app['config']->set($key, $value);
  48. }
  49. }
  50. protected function getAppPath()
  51. {
  52. $path = __DIR__.'/../bootstrap/app.php';
  53. if (! is_file($path)) {
  54. $path = __DIR__.'/../../bootstrap/app.php';
  55. }
  56. return $path;
  57. }
  58. protected function destory()
  59. {
  60. (new \CreateAdminTables())->down();
  61. (new \CreateTestTables())->down();
  62. DB::select("delete from `migrations` where `migration` = '2016_01_04_173148_create_admin_tables'");
  63. DB::select("delete from `migrations` where `migration` = '2016_11_22_093148_create_test_tables'");
  64. Artisan::call('migrate:rollback');
  65. }
  66. public function migrateTestTables()
  67. {
  68. $fileSystem = new Filesystem();
  69. $fileSystem->requireOnce(__DIR__.'/resources/migrations/2016_11_22_093148_create_test_tables.php');
  70. (new \CreateTestTables())->up();
  71. }
  72. /**
  73. * @return Administrator
  74. */
  75. protected function getUser()
  76. {
  77. if ($this->user) {
  78. return $this->user;
  79. }
  80. return $this->user = Administrator::first();
  81. }
  82. }