Newer
Older
<?php
namespace Breyta\Test\Migrations;
use Breyta\Test\TestCase;
class MigrateTest extends TestCase
{
/** @test */
public function returnsSuccessWhenNoMigrationsNeedToBeExecuted()
{
$this->setProtectedProperty($this->migrations, 'migrations', [Migration::createInstance([
'file' => '@breyta/CreateMigrationTable.php',
'status' => 'done'
$this->migrations->shouldReceive('up')->with()->once()->andReturn(true);
$result = $this->migrations->migrate();
self::assertTrue($result);
}
/** @test */
public function executesNewMigrations()
{
$this->setProtectedProperty($this->migrations, 'migrations', [$migration = Migration::createInstance([
'file' => '@breyta/CreateMigrationTable.php',
'status' => 'new',
$this->migrations->shouldReceive('up')->with($migration)->once()->andReturn(true);
$result = $this->migrations->migrate();
self::assertTrue($result);
}
/** @test */
public function executesFailedMigrations()
{
$this->setProtectedProperty($this->migrations, 'migrations', [$migration = Migration::createInstance([
'file' => '@breyta/CreateMigrationTable.php',
'status' => 'failed',
'executed' => date('c', strtotime('-1 hour')),
$this->migrations->shouldReceive('up')->with($migration)->once()->andReturn(true);
$result = $this->migrations->migrate();
self::assertTrue($result);
public function executesRevertedMigrations()
$this->setProtectedProperty($this->migrations, 'migrations', [$migration = Migration::createInstance([
'file' => '@breyta/CreateMigrationTable.php',
'status' => 'reverted',
'executed' => date('c', strtotime('-1 hour')),
'reverted' => date('c', strtotime('-10 minutes')),
$this->migrations->shouldReceive('up')->with($migration)->once()->andReturn(true);
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/** @test */
public function migratesToMatchingFile()
{
$migrations = [
Migration::createInstance([
'file' => 'FileA.php',
'status' => 'new',
]),
Migration::createInstance([
'file' => 'FileB.php',
'status' => 'new',
]),
Migration::createInstance([
'file' => 'FileC.php',
'status' => 'new',
]),
];
$this->setProtectedProperty($this->migrations, 'migrations', $migrations);
$this->migrations->shouldReceive('up')->withArgs(array_slice($migrations, 0, 2))
->once()->andReturn(true);
$result = $this->migrations->migrateTo('FileB');
self::assertTrue($result);
}
/** @test */
public function throwsWhenNoFileMatches()
{
$migrations = [
Migration::createInstance([
'file' => '@breyta/CreateMigrationTable.php',
'status' => 'new',
]),
];
$this->setProtectedProperty($this->migrations, 'migrations', $migrations);
self::expectException(\LogicException::class);
self::expectExceptionMessage('No migration found matching FileB');
$this->migrations->migrateTo('FileB');
}
/** @test */
public function filtersByFilesBeforeOrEqualTime()
{
$migrations = [
Migration::createInstance([
'file' => 'WithoutTime.php',
'status' => 'new',
]),
Migration::createInstance([
'file' => '2018-01-01T00.00.00Z Before.php',
'status' => 'new',
]),
Migration::createInstance([
'file' => '2018-01-02T00.00.00Z Equal.php',
'status' => 'new',
]),
Migration::createInstance([
'file' => '2018-01-03T00.00.00Z After.php',
'status' => 'new',
]),
];
$this->setProtectedProperty($this->migrations, 'migrations', $migrations);
$this->migrations->shouldReceive('up')->withArgs(array_slice($migrations, 0, 3))
->once()->andReturn(true);
$result = $this->migrations->migrateTo('2018-01-02T00:00:00Z');
self::assertTrue($result);
}
/** @test */
public function filtersDoneAfterSearch()
{
$migrations = [
Migration::createInstance([
'file' => 'FileA.php',
'status' => 'done',
]),
Migration::createInstance([
'file' => 'FileB.php',
'status' => 'done',
]),
Migration::createInstance([
'file' => 'FileC.php',
'status' => 'new',
]),
];
$this->setProtectedProperty($this->migrations, 'migrations', $migrations);
$this->migrations->shouldReceive('up')->with()
->once()->andReturn(true);
$result = $this->migrations->migrateTo('FileB');
self::assertTrue($result);
}