RefreshDatabaseTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Illuminate\Tests\Foundation\Testing;
  3. use Illuminate\Contracts\Console\Kernel;
  4. use Illuminate\Foundation\Testing\RefreshDatabase;
  5. use Illuminate\Foundation\Testing\RefreshDatabaseState;
  6. use PHPUnit\Framework\TestCase;
  7. class RefreshDatabaseTest extends TestCase
  8. {
  9. protected $traitObject;
  10. protected function setUp(): void
  11. {
  12. RefreshDatabaseState::$migrated = false;
  13. $this->traitObject = $this->getMockForTrait(RefreshDatabase::class, [], '', true, true, true, [
  14. 'artisan',
  15. 'beginDatabaseTransaction',
  16. ]);
  17. $kernelObj = \Mockery::mock();
  18. $kernelObj->shouldReceive('setArtisan')
  19. ->with(null);
  20. $this->traitObject->app = [
  21. Kernel::class => $kernelObj,
  22. ];
  23. }
  24. private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName)
  25. {
  26. $migrateFreshUsingReflection = new \ReflectionMethod(
  27. get_class($this->traitObject),
  28. $methodName
  29. );
  30. $migrateFreshUsingReflection->setAccessible(true);
  31. return $migrateFreshUsingReflection;
  32. }
  33. public function testRefreshTestDatabaseDefault()
  34. {
  35. $this->traitObject
  36. ->expects($this->exactly(1))
  37. ->method('artisan')
  38. ->with('migrate:fresh', [
  39. '--drop-views' => false,
  40. '--drop-types' => false,
  41. '--seed' => false,
  42. ]);
  43. $refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('refreshTestDatabase');
  44. $refreshTestDatabaseReflection->invoke($this->traitObject);
  45. }
  46. public function testRefreshTestDatabaseWithDropViewsOption()
  47. {
  48. $this->traitObject->dropViews = true;
  49. $this->traitObject
  50. ->expects($this->exactly(1))
  51. ->method('artisan')
  52. ->with('migrate:fresh', [
  53. '--drop-views' => true,
  54. '--drop-types' => false,
  55. '--seed' => false,
  56. ]);
  57. $refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('refreshTestDatabase');
  58. $refreshTestDatabaseReflection->invoke($this->traitObject);
  59. }
  60. public function testRefreshTestDatabaseWithDropTypesOption()
  61. {
  62. $this->traitObject->dropTypes = true;
  63. $this->traitObject
  64. ->expects($this->exactly(1))
  65. ->method('artisan')
  66. ->with('migrate:fresh', [
  67. '--drop-views' => false,
  68. '--drop-types' => true,
  69. '--seed' => false,
  70. ]);
  71. $refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('refreshTestDatabase');
  72. $refreshTestDatabaseReflection->invoke($this->traitObject);
  73. }
  74. }