Skip to content

Commit

Permalink
Merge pull request #33 from JayBizzle/update_tests
Browse files Browse the repository at this point in the history
Update tests for Laravel 7
  • Loading branch information
JayBizzle committed Mar 30, 2020
2 parents 0a305e5 + 30852f9 commit 08804e7
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 23 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"require": {
"php": ">=5.6.4",
"php": "^7.2.5",
"illuminate/support": "^7.0"
},
"autoload": {
Expand All @@ -23,10 +23,10 @@
}
},
"require-dev": {
"mockery/mockery": "~0.9",
"phpunit/phpunit": "^4.8",
"illuminate/database": "5.3.*",
"illuminate/filesystem": "5.3.*"
"mockery/mockery": "^1.3.1",
"phpunit/phpunit": "^8.4|^9.0",
"illuminate/database": "^7.0",
"illuminate/filesystem": "^7.0"
},
"extra": {
"laravel": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="false"
bootstrap="phpunit.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
Expand Down
17 changes: 17 additions & 0 deletions src/MigrationCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,26 @@
namespace Jaybizzle\MigrationsOrganiser;

use Illuminate\Database\Migrations\MigrationCreator as MC;
use InvalidArgumentException;

class MigrationCreator extends MC
{
protected function ensureMigrationDoesntAlreadyExist($name, $migrationPath = null)
{
if (! empty($migrationPath)) {
$migrationPath = $migrationPath.'/'.date('Y').'/'.date('m');
$migrationFiles = $this->files->glob($migrationPath.'/*.php');

foreach ($migrationFiles as $migrationFile) {
$this->files->requireOnce($migrationFile);
}
}

if (class_exists($className = $this->getClassName($name))) {
throw new InvalidArgumentException("A {$className} class already exists.");
}
}

protected function getPath($name, $path)
{
$path = $path.'/'.date('Y').'/'.date('m');
Expand Down
88 changes: 71 additions & 17 deletions tests/MigrationCreatorTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<?php

namespace Tests;

use Illuminate\Filesystem\Filesystem;
use InvalidArgumentException;
use Jaybizzle\MigrationsOrganiser\MigrationCreator;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class DatabaseMigrationCreatorTest extends TestCase
{
public function tearDown()
public function tearDown(): void
{
m::close();
}
Expand All @@ -14,46 +19,95 @@ public function testBasicCreateMethodStoresMigrationFile()
{
$creator = $this->getCreator();

unset($_SERVER['__migration.creator']);
$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('DummyClass');
$creator->getFilesystem()->shouldReceive('exists')->once()->with('foo/2020/03')->andReturn(false);
$creator->getFilesystem()->shouldReceive('makeDirectory')->once();
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/2020/03/foo_create_bar.php', 'CreateBar');
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/2020/03/*.php')->andReturn(['foo/2020/03/foo_create_bar.php']);
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/2020/03/foo_create_bar.php');

$creator->create('create_bar', 'foo');
}

$creator->afterCreate(function () {
$_SERVER['__migration.creator'] = true;
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->getStubPath().'/blank.stub')->andReturn('DummyClass');
$creator->getFilesystem()->shouldReceive('exists')->once()->shouldReceive('makeDirectory')->once()->shouldReceive('put')->once()->with('foo/'.date('Y').'/'.date('m').'/foo_create_bar.php', 'CreateBar');

$creator->create('create_bar', 'foo');
$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('DummyClass DummyTable');
$creator->getFilesystem()->shouldReceive('exists')->once()->with('foo/2020/03')->andReturn(false);
$creator->getFilesystem()->shouldReceive('makeDirectory')->once();
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/2020/03/foo_create_bar.php', 'CreateBar baz');
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/2020/03/*.php')->andReturn(['foo/2020/03/foo_create_bar.php']);
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/2020/03/foo_create_bar.php');

$creator->create('create_bar', 'foo', $table);

$this->assertTrue($_SERVER['__migration.creator']);
$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->getStubPath().'/update.stub')->andReturn('DummyClass DummyTable');
$creator->getFilesystem()->shouldReceive('exists')->once()->shouldReceive('makeDirectory')->shouldReceive('put')->once()->with('foo/'.date('Y').'/'.date('m').'/foo_create_bar.php', 'CreateBar baz');
$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('DummyClass DummyTable');
$creator->getFilesystem()->shouldReceive('exists')->once()->with('foo/2020/03')->andReturn(false);
$creator->getFilesystem()->shouldReceive('makeDirectory')->once();
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/2020/03/foo_create_bar.php', 'CreateBar baz');
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/2020/03/*.php')->andReturn(['foo/2020/03/foo_create_bar.php']);
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/2020/03/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->getStubPath().'/create.stub')->andReturn('DummyClass DummyTable');
$creator->getFilesystem()->shouldReceive('exists')->once()->shouldReceive('makeDirectory')->shouldReceive('put')->once()->with('foo/'.date('Y').'/'.date('m').'/foo_create_bar.php', 'CreateBar baz');
$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('DummyClass DummyTable');
$creator->getFilesystem()->shouldReceive('exists')->once()->with('foo/2020/03')->andReturn(false);
$creator->getFilesystem()->shouldReceive('makeDirectory')->once();
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/2020/03/foo_create_bar.php', 'CreateBar baz');
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/2020/03/*.php')->andReturn(['foo/2020/03/foo_create_bar.php']);
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/2020/03/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/2020/03/*.php')->andReturn(['foo/2020/03/foo_create_bar.php']);
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/2020/03/foo_create_bar.php');

$creator->create('migration_creator_fake_migration', 'foo');
}

protected function getCreator()
{
$files = m::mock(Illuminate\Filesystem\Filesystem::class);
$files = m::mock(Filesystem::class);
$customStubs = 'stubs';

return $this->getMock(Jaybizzle\MigrationsOrganiser\MigrationCreator::class, ['getDatePrefix'], [$files]);
return $this->getMockBuilder(MigrationCreator::class)
->setMethods(['getDatePrefix'])
->setConstructorArgs([$files, $customStubs])
->getMock();
}
}
6 changes: 6 additions & 0 deletions tests/stubs/MigrationCreatorFakeMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

class MigrationCreatorFakeMigration
{
//
}

0 comments on commit 08804e7

Please sign in to comment.