1 | <?php |
||
2 | |||
3 | namespace Mnabialek\LaravelQuickMigrations\Tests; |
||
4 | |||
5 | use ArrayAccess; |
||
6 | use Illuminate\Container\Container; |
||
7 | use Illuminate\Contracts\Console\Kernel; |
||
8 | use Illuminate\Foundation\Testing\RefreshDatabaseState; |
||
9 | use Illuminate\Foundation\Testing\TestCase; |
||
10 | use Mnabialek\LaravelQuickMigrations\QuickDatabaseMigrations; |
||
11 | use Mockery; |
||
12 | use stdClass; |
||
13 | |||
14 | class QuickDatabaseMigrationsTest extends UnitTestCase |
||
15 | { |
||
16 | /** @test */ |
||
17 | public function it_runs_default_method_when_turned_off() |
||
18 | { |
||
19 | $app = Mockery::mock(Container::class, ArrayAccess::class); |
||
20 | |||
21 | $config = Mockery::mock(stdClass::class); |
||
22 | |||
23 | $app->shouldReceive('make')->once()->with('config', [])->andReturn($config); |
||
24 | |||
25 | $config->shouldReceive('get')->once()->with('quick_migrations.enabled', null)->andReturn(false); |
||
26 | |||
27 | Container::setInstance($app); |
||
28 | |||
29 | $class = Mockery::mock(TestClass::class)->makePartial()->shouldAllowMockingProtectedMethods(); |
||
30 | |||
31 | $class->shouldReceive('useDefaultWay')->once()->withNoArgs(); |
||
32 | $class->shouldNotReceive('artisan'); |
||
33 | $class->shouldNotReceive('beforeApplicationDestroyed'); |
||
34 | |||
35 | $class->runDatabaseMigrations(); |
||
36 | $this->assertTrue(true); |
||
37 | } |
||
38 | |||
39 | /** @test */ |
||
40 | public function it_runs_custom_code_when_turned_on() |
||
41 | { |
||
42 | $app = Mockery::mock(Container::class, ArrayAccess::class); |
||
43 | |||
44 | $config = Mockery::mock(stdClass::class); |
||
45 | |||
46 | $app->shouldReceive('make')->once()->with('config', [])->andReturn($config); |
||
47 | |||
48 | $config->shouldReceive('get')->once()->with('quick_migrations.enabled', null)->andReturn(true); |
||
49 | |||
50 | Container::setInstance($app); |
||
51 | |||
52 | $class = Mockery::mock(TestClass::class)->makePartial()->shouldAllowMockingProtectedMethods(); |
||
53 | $class->setApp($app); |
||
54 | |||
55 | $class->shouldNotReceive('useDefaultWay'); |
||
56 | |||
57 | $class->shouldReceive('artisan')->once()->with('migrate:fresh', [ |
||
58 | '--path' => realpath(__DIR__ . '/../migrations'), |
||
59 | '--realpath' => 1, |
||
60 | ]); |
||
61 | |||
62 | $kernel = Mockery::mock(stdClass::class); |
||
63 | |||
64 | $app->shouldReceive('offsetGet')->once()->with(Kernel::class)->andReturn($kernel); |
||
65 | |||
66 | $kernel->shouldReceive('setArtisan')->once()->with(null); |
||
67 | |||
68 | // set some state |
||
69 | RefreshDatabaseState::$migrated = true; |
||
70 | $this->assertSame(true, RefreshDatabaseState::$migrated); |
||
71 | |||
72 | $class->shouldReceive('beforeApplicationDestroyed')->once()->passthru(); |
||
73 | |||
74 | $class->runDatabaseMigrations(); |
||
75 | |||
76 | $callbacks = $class->getCallbacks(); |
||
77 | $this->assertCount(1, $callbacks); |
||
78 | |||
79 | // execute callback |
||
80 | $callbacks[0](); |
||
81 | |||
82 | // make sure state was changed |
||
83 | $this->assertSame(false, RefreshDatabaseState::$migrated); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | class TestClass extends TestCase |
||
88 | { |
||
89 | use QuickDatabaseMigrations; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
90 | |||
91 | public function setApp($app) |
||
92 | { |
||
93 | $this->app = $app; |
||
94 | } |
||
95 | |||
96 | public function createApplication() |
||
97 | { |
||
98 | } |
||
99 | |||
100 | public function getCallbacks() |
||
101 | { |
||
102 | return $this->beforeApplicationDestroyedCallbacks; |
||
103 | } |
||
104 | } |
||
105 |