CreatesApplication.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\Facades\Artisan;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Schema;
  9. trait CreatesApplication
  10. {
  11. public function createApplication()
  12. {
  13. $app = require $this->getAppPath();
  14. $app->make(Kernel::class)->bootstrap();
  15. return $app;
  16. }
  17. protected function boot()
  18. {
  19. $this->artisan('admin:publish');
  20. Schema::defaultStringLength(191);
  21. $this->artisan('admin:install');
  22. $this->migrateTestTables();
  23. require __DIR__.'/helpers.php';
  24. require __DIR__.'/resources/seeds/factory.php';
  25. view()->addNamespace('admin-tests', __DIR__.'/resources/views');
  26. }
  27. protected function getAppPath()
  28. {
  29. $path = __DIR__.'/../bootstrap/app.php';
  30. if (! is_file($path)) {
  31. $path = __DIR__.'/../../bootstrap/app.php';
  32. }
  33. if (! is_file($path)) {
  34. $path = __DIR__.'/../../../bootstrap/app.php';
  35. }
  36. return $path;
  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. public function migrateTestTables()
  47. {
  48. $fileSystem = new Filesystem();
  49. $fileSystem->requireOnce(__DIR__.'/resources/migrations/2016_11_22_093148_create_test_tables.php');
  50. (new \CreateTestTables())->up();
  51. }
  52. /**
  53. * @return Administrator
  54. */
  55. protected function getUser()
  56. {
  57. if ($this->user) {
  58. return $this->user;
  59. }
  60. return $this->user = Administrator::first();
  61. }
  62. }