Skip to content

Commit 08804e7

Browse files
authored
Merge pull request #33 from JayBizzle/update_tests
Update tests for Laravel 7
2 parents 0a305e5 + 30852f9 commit 08804e7

File tree

5 files changed

+100
-23
lines changed

5 files changed

+100
-23
lines changed

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
],
1616
"require": {
17-
"php": ">=5.6.4",
17+
"php": "^7.2.5",
1818
"illuminate/support": "^7.0"
1919
},
2020
"autoload": {
@@ -23,10 +23,10 @@
2323
}
2424
},
2525
"require-dev": {
26-
"mockery/mockery": "~0.9",
27-
"phpunit/phpunit": "^4.8",
28-
"illuminate/database": "5.3.*",
29-
"illuminate/filesystem": "5.3.*"
26+
"mockery/mockery": "^1.3.1",
27+
"phpunit/phpunit": "^8.4|^9.0",
28+
"illuminate/database": "^7.0",
29+
"illuminate/filesystem": "^7.0"
3030
},
3131
"extra": {
3232
"laravel": {

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit backupGlobals="false"
33
backupStaticAttributes="false"
4+
beStrictAboutTestsThatDoNotTestAnything="false"
45
bootstrap="phpunit.php"
56
colors="true"
67
convertErrorsToExceptions="true"
78
convertNoticesToExceptions="true"
89
convertWarningsToExceptions="true"
910
processIsolation="false"
1011
stopOnFailure="false"
11-
syntaxCheck="false"
1212
>
1313
<testsuites>
1414
<testsuite name="Package Test Suite">

src/MigrationCreator.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,26 @@
33
namespace Jaybizzle\MigrationsOrganiser;
44

55
use Illuminate\Database\Migrations\MigrationCreator as MC;
6+
use InvalidArgumentException;
67

78
class MigrationCreator extends MC
89
{
10+
protected function ensureMigrationDoesntAlreadyExist($name, $migrationPath = null)
11+
{
12+
if (! empty($migrationPath)) {
13+
$migrationPath = $migrationPath.'/'.date('Y').'/'.date('m');
14+
$migrationFiles = $this->files->glob($migrationPath.'/*.php');
15+
16+
foreach ($migrationFiles as $migrationFile) {
17+
$this->files->requireOnce($migrationFile);
18+
}
19+
}
20+
21+
if (class_exists($className = $this->getClassName($name))) {
22+
throw new InvalidArgumentException("A {$className} class already exists.");
23+
}
24+
}
25+
926
protected function getPath($name, $path)
1027
{
1128
$path = $path.'/'.date('Y').'/'.date('m');

tests/MigrationCreatorTest.php

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
<?php
22

3+
namespace Tests;
4+
5+
use Illuminate\Filesystem\Filesystem;
6+
use InvalidArgumentException;
7+
use Jaybizzle\MigrationsOrganiser\MigrationCreator;
38
use Mockery as m;
49
use PHPUnit\Framework\TestCase;
510

611
class DatabaseMigrationCreatorTest extends TestCase
712
{
8-
public function tearDown()
13+
public function tearDown(): void
914
{
1015
m::close();
1116
}
@@ -14,46 +19,95 @@ public function testBasicCreateMethodStoresMigrationFile()
1419
{
1520
$creator = $this->getCreator();
1621

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+
}
1833

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;
2142
});
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');
2543

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);
2754

28-
$this->assertTrue($_SERVER['__migration.creator']);
55+
$this->assertEquals($_SERVER['__migration.creator'], $table);
2956

3057
unset($_SERVER['__migration.creator']);
3158
}
3259

3360
public function testTableUpdateMigrationStoresMigrationFile()
3461
{
3562
$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');
3971

4072
$creator->create('create_bar', 'foo', 'baz');
4173
}
4274

4375
public function testTableCreationMigrationStoresMigrationFile()
4476
{
4577
$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');
4986

5087
$creator->create('create_bar', 'foo', 'baz', true);
5188
}
5289

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+
53103
protected function getCreator()
54104
{
55-
$files = m::mock(Illuminate\Filesystem\Filesystem::class);
105+
$files = m::mock(Filesystem::class);
106+
$customStubs = 'stubs';
56107

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();
58112
}
59113
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
class MigrationCreatorFakeMigration
4+
{
5+
//
6+
}

0 commit comments

Comments
 (0)