CreatesApplication.php 3.1 KB

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