1
1
<?php
2
2
3
+ namespace Tests ;
4
+
5
+ use Illuminate \Filesystem \Filesystem ;
6
+ use InvalidArgumentException ;
7
+ use Jaybizzle \MigrationsOrganiser \MigrationCreator ;
3
8
use Mockery as m ;
4
9
use PHPUnit \Framework \TestCase ;
5
10
6
11
class DatabaseMigrationCreatorTest extends TestCase
7
12
{
8
- public function tearDown ()
13
+ public function tearDown (): void
9
14
{
10
15
m::close ();
11
16
}
@@ -14,46 +19,95 @@ public function testBasicCreateMethodStoresMigrationFile()
14
19
{
15
20
$ creator = $ this ->getCreator ();
16
21
17
- unset($ _SERVER ['__migration.creator ' ]);
22
+ $ creator ->expects ($ this ->any ())->method ('getDatePrefix ' )->willReturn ('foo ' );
23
+ $ creator ->getFilesystem ()->shouldReceive ('exists ' )->once ()->with ('stubs/migration.stub ' )->andReturn (false );
24
+ $ creator ->getFilesystem ()->shouldReceive ('get ' )->once ()->with ($ creator ->stubPath ().'/migration.stub ' )->andReturn ('DummyClass ' );
25
+ $ creator ->getFilesystem ()->shouldReceive ('exists ' )->once ()->with ('foo/2020/03 ' )->andReturn (false );
26
+ $ creator ->getFilesystem ()->shouldReceive ('makeDirectory ' )->once ();
27
+ $ creator ->getFilesystem ()->shouldReceive ('put ' )->once ()->with ('foo/2020/03/foo_create_bar.php ' , 'CreateBar ' );
28
+ $ creator ->getFilesystem ()->shouldReceive ('glob ' )->once ()->with ('foo/2020/03/*.php ' )->andReturn (['foo/2020/03/foo_create_bar.php ' ]);
29
+ $ creator ->getFilesystem ()->shouldReceive ('requireOnce ' )->once ()->with ('foo/2020/03/foo_create_bar.php ' );
30
+
31
+ $ creator ->create ('create_bar ' , 'foo ' );
32
+ }
18
33
19
- $ creator ->afterCreate (function () {
20
- $ _SERVER ['__migration.creator ' ] = true ;
34
+ public function testBasicCreateMethodCallsPostCreateHooks ()
35
+ {
36
+ $ table = 'baz ' ;
37
+
38
+ $ creator = $ this ->getCreator ();
39
+ unset($ _SERVER ['__migration.creator ' ]);
40
+ $ creator ->afterCreate (function ($ table ) {
41
+ $ _SERVER ['__migration.creator ' ] = $ table ;
21
42
});
22
- $ creator ->expects ($ this ->any ())->method ('getDatePrefix ' )->will ($ this ->returnValue ('foo ' ));
23
- $ creator ->getFilesystem ()->shouldReceive ('get ' )->once ()->with ($ creator ->getStubPath ().'/blank.stub ' )->andReturn ('DummyClass ' );
24
- $ creator ->getFilesystem ()->shouldReceive ('exists ' )->once ()->shouldReceive ('makeDirectory ' )->once ()->shouldReceive ('put ' )->once ()->with ('foo/ ' .date ('Y ' ).'/ ' .date ('m ' ).'/foo_create_bar.php ' , 'CreateBar ' );
25
43
26
- $ creator ->create ('create_bar ' , 'foo ' );
44
+ $ creator ->expects ($ this ->any ())->method ('getDatePrefix ' )->willReturn ('foo ' );
45
+ $ creator ->getFilesystem ()->shouldReceive ('exists ' )->once ()->with ('stubs/migration.update.stub ' )->andReturn (false );
46
+ $ creator ->getFilesystem ()->shouldReceive ('get ' )->once ()->with ($ creator ->stubPath ().'/migration.update.stub ' )->andReturn ('DummyClass DummyTable ' );
47
+ $ creator ->getFilesystem ()->shouldReceive ('exists ' )->once ()->with ('foo/2020/03 ' )->andReturn (false );
48
+ $ creator ->getFilesystem ()->shouldReceive ('makeDirectory ' )->once ();
49
+ $ creator ->getFilesystem ()->shouldReceive ('put ' )->once ()->with ('foo/2020/03/foo_create_bar.php ' , 'CreateBar baz ' );
50
+ $ creator ->getFilesystem ()->shouldReceive ('glob ' )->once ()->with ('foo/2020/03/*.php ' )->andReturn (['foo/2020/03/foo_create_bar.php ' ]);
51
+ $ creator ->getFilesystem ()->shouldReceive ('requireOnce ' )->once ()->with ('foo/2020/03/foo_create_bar.php ' );
52
+
53
+ $ creator ->create ('create_bar ' , 'foo ' , $ table );
27
54
28
- $ this ->assertTrue ($ _SERVER ['__migration.creator ' ]);
55
+ $ this ->assertEquals ($ _SERVER ['__migration.creator ' ], $ table );
29
56
30
57
unset($ _SERVER ['__migration.creator ' ]);
31
58
}
32
59
33
60
public function testTableUpdateMigrationStoresMigrationFile ()
34
61
{
35
62
$ creator = $ this ->getCreator ();
36
- $ creator ->expects ($ this ->any ())->method ('getDatePrefix ' )->will ($ this ->returnValue ('foo ' ));
37
- $ creator ->getFilesystem ()->shouldReceive ('get ' )->once ()->with ($ creator ->getStubPath ().'/update.stub ' )->andReturn ('DummyClass DummyTable ' );
38
- $ creator ->getFilesystem ()->shouldReceive ('exists ' )->once ()->shouldReceive ('makeDirectory ' )->shouldReceive ('put ' )->once ()->with ('foo/ ' .date ('Y ' ).'/ ' .date ('m ' ).'/foo_create_bar.php ' , 'CreateBar baz ' );
63
+ $ creator ->expects ($ this ->any ())->method ('getDatePrefix ' )->willReturn ('foo ' );
64
+ $ creator ->getFilesystem ()->shouldReceive ('exists ' )->once ()->with ('stubs/migration.update.stub ' )->andReturn (false );
65
+ $ creator ->getFilesystem ()->shouldReceive ('get ' )->once ()->with ($ creator ->stubPath ().'/migration.update.stub ' )->andReturn ('DummyClass DummyTable ' );
66
+ $ creator ->getFilesystem ()->shouldReceive ('exists ' )->once ()->with ('foo/2020/03 ' )->andReturn (false );
67
+ $ creator ->getFilesystem ()->shouldReceive ('makeDirectory ' )->once ();
68
+ $ creator ->getFilesystem ()->shouldReceive ('put ' )->once ()->with ('foo/2020/03/foo_create_bar.php ' , 'CreateBar baz ' );
69
+ $ creator ->getFilesystem ()->shouldReceive ('glob ' )->once ()->with ('foo/2020/03/*.php ' )->andReturn (['foo/2020/03/foo_create_bar.php ' ]);
70
+ $ creator ->getFilesystem ()->shouldReceive ('requireOnce ' )->once ()->with ('foo/2020/03/foo_create_bar.php ' );
39
71
40
72
$ creator ->create ('create_bar ' , 'foo ' , 'baz ' );
41
73
}
42
74
43
75
public function testTableCreationMigrationStoresMigrationFile ()
44
76
{
45
77
$ creator = $ this ->getCreator ();
46
- $ creator ->expects ($ this ->any ())->method ('getDatePrefix ' )->will ($ this ->returnValue ('foo ' ));
47
- $ creator ->getFilesystem ()->shouldReceive ('get ' )->once ()->with ($ creator ->getStubPath ().'/create.stub ' )->andReturn ('DummyClass DummyTable ' );
48
- $ creator ->getFilesystem ()->shouldReceive ('exists ' )->once ()->shouldReceive ('makeDirectory ' )->shouldReceive ('put ' )->once ()->with ('foo/ ' .date ('Y ' ).'/ ' .date ('m ' ).'/foo_create_bar.php ' , 'CreateBar baz ' );
78
+ $ creator ->expects ($ this ->any ())->method ('getDatePrefix ' )->willReturn ('foo ' );
79
+ $ creator ->getFilesystem ()->shouldReceive ('exists ' )->once ()->with ('stubs/migration.create.stub ' )->andReturn (false );
80
+ $ creator ->getFilesystem ()->shouldReceive ('get ' )->once ()->with ($ creator ->stubPath ().'/migration.create.stub ' )->andReturn ('DummyClass DummyTable ' );
81
+ $ creator ->getFilesystem ()->shouldReceive ('exists ' )->once ()->with ('foo/2020/03 ' )->andReturn (false );
82
+ $ creator ->getFilesystem ()->shouldReceive ('makeDirectory ' )->once ();
83
+ $ creator ->getFilesystem ()->shouldReceive ('put ' )->once ()->with ('foo/2020/03/foo_create_bar.php ' , 'CreateBar baz ' );
84
+ $ creator ->getFilesystem ()->shouldReceive ('glob ' )->once ()->with ('foo/2020/03/*.php ' )->andReturn (['foo/2020/03/foo_create_bar.php ' ]);
85
+ $ creator ->getFilesystem ()->shouldReceive ('requireOnce ' )->once ()->with ('foo/2020/03/foo_create_bar.php ' );
49
86
50
87
$ creator ->create ('create_bar ' , 'foo ' , 'baz ' , true );
51
88
}
52
89
90
+ public function testTableUpdateMigrationWontCreateDuplicateClass ()
91
+ {
92
+ $ this ->expectException (InvalidArgumentException::class);
93
+ $ this ->expectExceptionMessage ('A MigrationCreatorFakeMigration class already exists. ' );
94
+
95
+ $ creator = $ this ->getCreator ();
96
+
97
+ $ creator ->getFilesystem ()->shouldReceive ('glob ' )->once ()->with ('foo/2020/03/*.php ' )->andReturn (['foo/2020/03/foo_create_bar.php ' ]);
98
+ $ creator ->getFilesystem ()->shouldReceive ('requireOnce ' )->once ()->with ('foo/2020/03/foo_create_bar.php ' );
99
+
100
+ $ creator ->create ('migration_creator_fake_migration ' , 'foo ' );
101
+ }
102
+
53
103
protected function getCreator ()
54
104
{
55
- $ files = m::mock (Illuminate \Filesystem \Filesystem::class);
105
+ $ files = m::mock (Filesystem::class);
106
+ $ customStubs = 'stubs ' ;
56
107
57
- return $ this ->getMock (Jaybizzle \MigrationsOrganiser \MigrationCreator::class, ['getDatePrefix ' ], [$ files ]);
108
+ return $ this ->getMockBuilder (MigrationCreator::class)
109
+ ->setMethods (['getDatePrefix ' ])
110
+ ->setConstructorArgs ([$ files , $ customStubs ])
111
+ ->getMock ();
58
112
}
59
113
}
0 commit comments