Repository navigation
Expand file tree
/
Copy pathDatabaseMigrationCreatorTest.php
More file actions
executable file
·121 lines (98 loc) · 5.11 KB
/
Copy pathDatabaseMigrationCreatorTest.php
File metadata and controls
executable file
·121 lines (98 loc) · 5.11 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Database;
use Hyperf\Database\Migrations\MigrationCreator;
use Hyperf\Support\Filesystem\Filesystem;
use HyperfTest\Database\Stubs\ContainerStub;
use HyperfTest\Database\Stubs\MigrationCreatorFakeMigration;
use InvalidArgumentException;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class DatabaseMigrationCreatorTest extends TestCase
{
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
protected function setUp(): void
{
ContainerStub::unsetContainer();
}
protected function tearDown(): void
{
Mockery::close();
}
public function testBasicCreateMethodStoresMigrationFile()
{
$creator = $this->getCreator();
$creator->expects($this->any())->method('getDatePrefix')->will($this->returnValue('foo'));
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath() . '/blank.stub')->andReturn('return new class');
$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']);
$creator->afterCreate(function ($table) {
$_SERVER['__migration.creator'] = $table;
});
$creator->expects($this->any())->method('getDatePrefix')->will($this->returnValue('foo'));
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath() . '/update.stub')->andReturn('return new class DummyTable');
$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);
unset($_SERVER['__migration.creator']);
}
public function testTableUpdateMigrationStoresMigrationFile()
{
$creator = $this->getCreator();
$creator->expects($this->any())->method('getDatePrefix')->will($this->returnValue('foo'));
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath() . '/update.stub')->andReturn('return new class DummyTable');
$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')->will($this->returnValue('foo'));
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath() . '/create.stub')->andReturn('return new class DummyTable');
$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 HyperfTest\Database\Stubs\MigrationCreatorFakeMigration class already exists.');
$creator = $this->getCreator();
$creator->getFilesystem()->shouldReceive('get');
$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(MigrationCreatorFakeMigration::class, 'foo');
}
protected function getCreator()
{
$files = Mockery::mock(Filesystem::class);
return $this->getMockBuilder(MigrationCreator::class)->onlyMethods(['getDatePrefix'])->setConstructorArgs([$files])->getMock();
}
}