forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseMigrationCreatorTest.php
executable file
·111 lines (89 loc) · 5.6 KB
/
DatabaseMigrationCreatorTest.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
<?php
namespace Illuminate\Tests\Database;
use Illuminate\Database\Migrations\MigrationCreator;
use Illuminate\Filesystem\Filesystem;
use InvalidArgumentException;
use Mockery as m;
use PHPUnit\Framework\TestCase;
class DatabaseMigrationCreatorTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
public function testBasicCreateMethodStoresMigrationFile()
{
$creator = $this->getCreator();
$creator->expects($this->any())->method('getDatePrefix')->willReturn('foo');
$creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.stub')->andReturn(false);
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.stub')->andReturn('return new class');
$creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo');
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'return new class');
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
$creator->create('create_bar', 'foo');
}
public function testBasicCreateMethodCallsPostCreateHooks()
{
$table = 'baz';
$creator = $this->getCreator();
unset($_SERVER['__migration.creator.table'], $_SERVER['__migration.creator.path']);
$creator->afterCreate(function ($table, $path) {
$_SERVER['__migration.creator.table'] = $table;
$_SERVER['__migration.creator.path'] = $path;
});
$creator->expects($this->any())->method('getDatePrefix')->willReturn('foo');
$creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.update.stub')->andReturn(false);
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.update.stub')->andReturn('return new class DummyTable');
$creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo');
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'return new class baz');
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
$creator->create('create_bar', 'foo', $table);
$this->assertEquals($_SERVER['__migration.creator.table'], $table);
$this->assertEquals($_SERVER['__migration.creator.path'], 'foo/foo_create_bar.php');
unset($_SERVER['__migration.creator.table'], $_SERVER['__migration.creator.path']);
}
public function testTableUpdateMigrationStoresMigrationFile()
{
$creator = $this->getCreator();
$creator->expects($this->any())->method('getDatePrefix')->willReturn('foo');
$creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.update.stub')->andReturn(false);
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.update.stub')->andReturn('return new class DummyTable');
$creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo');
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'return new class baz');
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
$creator->create('create_bar', 'foo', 'baz');
}
public function testTableCreationMigrationStoresMigrationFile()
{
$creator = $this->getCreator();
$creator->expects($this->any())->method('getDatePrefix')->willReturn('foo');
$creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.create.stub')->andReturn(false);
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.create.stub')->andReturn('return new class DummyTable');
$creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo');
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'return new class baz');
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
$creator->create('create_bar', 'foo', 'baz', true);
}
public function testTableUpdateMigrationWontCreateDuplicateClass()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('A MigrationCreatorFakeMigration class already exists.');
$creator = $this->getCreator();
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
$creator->create('migration_creator_fake_migration', 'foo');
}
protected function getCreator()
{
$files = m::mock(Filesystem::class);
$customStubs = 'stubs';
return $this->getMockBuilder(MigrationCreator::class)
->onlyMethods(['getDatePrefix'])
->setConstructorArgs([$files, $customStubs])
->getMock();
}
}