1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Quantick\DeployMigration\Tests\Lib\Service; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Illuminate\Console\OutputStyle; |
8
|
|
|
use Illuminate\Container\Container; |
9
|
|
|
use Illuminate\Contracts\Console\Kernel; |
10
|
|
|
use Illuminate\Database\Connection; |
11
|
|
|
use Illuminate\Database\Query\Builder; |
12
|
|
|
use Illuminate\Filesystem\Filesystem; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Quantick\DeployMigration\Lib\Service\Instantiator; |
15
|
|
|
use Quantick\DeployMigration\Lib\Service\Migrator; |
16
|
|
|
use Quantick\DeployMigration\Tests\Lib\Service\stub\TestCommand; |
17
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
18
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
19
|
|
|
|
20
|
|
|
class MigratorTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @throws \ReflectionException |
24
|
|
|
* @throws \Throwable |
25
|
|
|
*/ |
26
|
|
|
public function testRun() |
27
|
|
|
{ |
28
|
|
|
$connection = $this->createMock(Connection::class); |
29
|
|
|
$connection->expects(static::once())->method('beginTransaction'); |
30
|
|
|
$connection->expects(static::once())->method('commit'); |
31
|
|
|
$container = $this->createMock(Container::class); |
32
|
|
|
$kernel = $this->createMock(Kernel::class); |
33
|
|
|
$kernel->method('output')->willReturn(''); |
34
|
|
|
|
35
|
|
|
$instantiator = Instantiator::create((new Filesystem())->files(__DIR__ . '/stub/migrations')); |
36
|
|
|
$collection = $instantiator->run(); |
37
|
|
|
|
38
|
|
|
$migrator = new Migrator($connection, $container, $kernel); |
39
|
|
|
|
40
|
|
|
$builder = $this->createMock(Builder::class); |
41
|
|
|
$infoBuilder = $this->createMock(Builder::class); |
42
|
|
|
|
43
|
|
|
$connection->expects(static::exactly(2))->method('table') |
44
|
|
|
->willReturnOnConsecutiveCalls( |
45
|
|
|
$builder, |
46
|
|
|
$infoBuilder |
47
|
|
|
) |
48
|
|
|
; |
49
|
|
|
|
50
|
|
|
$builder->expects(static::once())->method('insert'); |
51
|
|
|
$infoBuilder->expects(static::once())->method('insert'); |
52
|
|
|
|
53
|
|
|
$builder->method('where')->willReturn($builder); |
54
|
|
|
$builder->method('count')->willReturn(0); |
55
|
|
|
|
56
|
|
|
$testCommand = new TestCommand(); |
57
|
|
|
|
58
|
|
|
$container->method('get')->with(TestCommand::class)->willReturn($testCommand); |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
$output = new OutputStyle(new ArrayInput([]), new ConsoleOutput()); |
62
|
|
|
$output->progressStart(1); |
63
|
|
|
|
64
|
|
|
$container->method('make')->willReturn($output); |
65
|
|
|
$migrator->run($collection, $output); |
66
|
|
|
$output->progressFinish(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @throws \ReflectionException |
71
|
|
|
* @throws \Throwable |
72
|
|
|
*/ |
73
|
|
|
public function testRunWithError() |
74
|
|
|
{ |
75
|
|
|
$connection = $this->createMock(Connection::class); |
76
|
|
|
$connection->expects(static::once())->method('beginTransaction'); |
77
|
|
|
$connection->expects(static::once())->method('rollBack'); |
78
|
|
|
$container = $this->createMock(Container::class); |
79
|
|
|
$kernel = $this->createMock(Kernel::class); |
80
|
|
|
$kernel->method('output')->willReturn(''); |
81
|
|
|
$kernel->method('call')->willThrowException(new \RuntimeException('exception')); |
82
|
|
|
|
83
|
|
|
$instantiator = Instantiator::create((new Filesystem())->files(__DIR__ . '/stub/migrations')); |
84
|
|
|
$collection = $instantiator->run(); |
85
|
|
|
|
86
|
|
|
$migrator = new Migrator($connection, $container, $kernel); |
87
|
|
|
|
88
|
|
|
$builder = $this->createMock(Builder::class); |
89
|
|
|
$infoBuilder = $this->createMock(Builder::class); |
90
|
|
|
|
91
|
|
|
$connection->expects(static::exactly(2))->method('table') |
92
|
|
|
->willReturnOnConsecutiveCalls( |
93
|
|
|
$builder, |
94
|
|
|
$infoBuilder |
95
|
|
|
) |
96
|
|
|
; |
97
|
|
|
|
98
|
|
|
$infoBuilder->expects(static::once())->method('insert'); |
99
|
|
|
|
100
|
|
|
$builder->method('where')->willReturn($builder); |
101
|
|
|
$builder->method('count')->willReturn(0); |
102
|
|
|
|
103
|
|
|
$output = new OutputStyle(new ArrayInput([]), new ConsoleOutput()); |
104
|
|
|
$output->progressStart(1); |
105
|
|
|
|
106
|
|
|
$container->method('make')->willReturn($output); |
107
|
|
|
$this->expectException(\RuntimeException::class); |
108
|
|
|
$migrator->run($collection, $output); |
109
|
|
|
$output->progressFinish(); |
110
|
|
|
} |
111
|
|
|
} |