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