forked from laravel/framework
/
MigratorTest.php
152 lines (116 loc) · 5.4 KB
/
MigratorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace Illuminate\Tests\Integration\Migration;
use Illuminate\Support\Facades\DB;
use Mockery as m;
use Orchestra\Testbench\TestCase;
use Symfony\Component\Console\Output\OutputInterface;
class MigratorTest extends TestCase
{
/**
* @var \Mockery\Mock
*/
private $output;
protected function setUp(): void
{
parent::setUp();
$this->output = m::mock(OutputInterface::class);
$this->subject = $this->app->make('migrator');
$this->subject->setOutput($this->output);
$this->subject->getRepository()->createRepository();
}
public function testMigrate()
{
$this->expectInfo('Running migrations.');
$this->expectTask('2014_10_12_000000_create_people_table', 'DONE');
$this->expectTask('2015_10_04_000000_modify_people_table', 'DONE');
$this->expectTask('2016_10_04_000000_modify_people_table', 'DONE');
$this->output->shouldReceive('writeln')->once();
$this->subject->run([__DIR__.'/fixtures']);
$this->assertTrue(DB::getSchemaBuilder()->hasTable('people'));
$this->assertTrue(DB::getSchemaBuilder()->hasColumn('people', 'first_name'));
$this->assertTrue(DB::getSchemaBuilder()->hasColumn('people', 'last_name'));
}
public function testMigrateWithoutOutput()
{
$this->app->forgetInstance('migrator');
$this->subject = $this->app->make('migrator');
$this->subject->run([__DIR__.'/fixtures']);
$this->assertTrue(DB::getSchemaBuilder()->hasTable('people'));
$this->assertTrue(DB::getSchemaBuilder()->hasColumn('people', 'first_name'));
$this->assertTrue(DB::getSchemaBuilder()->hasColumn('people', 'last_name'));
}
public function testRollback()
{
$this->getConnection()->statement('CREATE TABLE people(id INT, first_name VARCHAR, last_name VARCHAR);');
$this->subject->getRepository()->log('2014_10_12_000000_create_people_table', 1);
$this->subject->getRepository()->log('2015_10_04_000000_modify_people_table', 1);
$this->subject->getRepository()->log('2016_10_04_000000_modify_people_table', 1);
$this->expectInfo('Rollbacking migrations.');
$this->expectTask('2016_10_04_000000_modify_people_table', 'DONE');
$this->expectTask('2015_10_04_000000_modify_people_table', 'DONE');
$this->expectTask('2014_10_12_000000_create_people_table', 'DONE');
$this->output->shouldReceive('writeln')->once();
$this->subject->rollback([__DIR__.'/fixtures']);
$this->assertFalse(DB::getSchemaBuilder()->hasTable('people'));
}
public function testPretendMigrate()
{
$this->expectInfo('Running migrations.');
$this->expectTwoColumnDetail('CreatePeopleTable');
$this->expectBulletList([
'create table "people" ("id" integer not null primary key autoincrement, "name" varchar not null, "email" varchar not null, "password" varchar not null, "remember_token" varchar, "created_at" datetime, "updated_at" datetime)',
'create unique index "people_email_unique" on "people" ("email")',
]);
$this->expectTwoColumnDetail('ModifyPeopleTable');
$this->expectBulletList(['alter table "people" add column "first_name" varchar']);
$this->expectTwoColumnDetail('2016_10_04_000000_modify_people_table');
$this->expectBulletList(['alter table "people" add column "last_name" varchar']);
$this->output->shouldReceive('writeln')->once();
$this->subject->run([__DIR__.'/fixtures'], ['pretend' => true]);
$this->assertFalse(DB::getSchemaBuilder()->hasTable('people'));
}
protected function expectInfo($message): void
{
$this->output->shouldReceive('writeln')->once()->with(m::on(
fn ($argument) => str($argument)->contains($message),
), m::any());
}
protected function expectTwoColumnDetail($first, $second = null)
{
$this->output->shouldReceive('writeln')->with(m::on(function ($argument) use ($first, $second) {
$result = str($argument)->contains($first);
if ($result && $second) {
$result = str($argument)->contains($second);
}
return $result;
}), m::any());
}
protected function expectBulletList($elements): void
{
$this->output->shouldReceive('writeln')->once()->with(m::on(function ($argument) use ($elements) {
foreach ($elements as $element) {
if (! str($argument)->contains("⇂ $element")) {
return false;
}
}
return true;
}), m::any());
}
protected function expectTask($description, $result): void
{
// Ignore dots...
$this->output->shouldReceive('write')->with(m::on(
fn ($argument) => str($argument)->contains(['<fg=gray></>', '<fg=gray>.</>']),
), m::any(), m::any());
// Ignore duration...
$this->output->shouldReceive('write')->with(m::on(
fn ($argument) => str($argument)->contains(['ms</>']),
), m::any(), m::any());
$this->output->shouldReceive('write')->once()->with(m::on(
fn ($argument) => str($argument)->contains($description),
), m::any(), m::any());
$this->output->shouldReceive('writeln')->once()->with(m::on(
fn ($argument) => str($argument)->contains($result),
), m::any());
}
}