Skip to content
Snippets Groups Projects
Unverified Commit 7771e04f authored by Thomas Flori's avatar Thomas Flori
Browse files

implement create migrations

parent 768ce6fb
No related branches found
No related tags found
No related merge requests found
<?php // FILE_NAME
namespace Breyta\NAMESPACE_NAME;
use Breyta\AbstractMigration;
class CLASS_NAME extends AbstractMigration
{
public function up(): void
{
}
public function down(): void
{
}
}
...@@ -21,6 +21,10 @@ class Migrations ...@@ -21,6 +21,10 @@ class Migrations
* @var string */ * @var string */
public static $table = 'migrations'; public static $table = 'migrations';
/** The path to the template for migrations
* @var string */
public static $templatePath = __DIR__ . '/../resources/MigrationTemplate.php';
/** @var \PDO */ /** @var \PDO */
protected $db; protected $db;
...@@ -306,6 +310,40 @@ class Migrations ...@@ -306,6 +310,40 @@ class Migrations
return true; return true;
} }
/**
* Creates a migration
*
* We recommend StudlyCase naming for PSR2 compatibility. Also the files will get a namespace.
*
* @param string $name
* @return bool
*/
public function createMigration(string $name): bool
{
static $template;
if (is_null($template)) {
$template = file_get_contents(self::$templatePath);
}
$path = explode('/', $name);
$name = array_pop($path);
$fileName = implode('/', array_merge($path, [date('Y-m-d\TH.i.s\Z') . '_' . $name . '.php']));
$className = $name;
$namespace = implode('\\', array_merge(['Migration'], $path));
$fullPath = $this->path . DIRECTORY_SEPARATOR . $fileName;
if (!file_exists(dirname($fullPath))) {
mkdir(dirname($fullPath), umask() ^ 0777, true);
}
file_put_contents($fullPath, strtr($template, [
'NAMESPACE_NAME' => $namespace,
'CLASS_NAME' => $className,
'FILE_NAME' => $fileName,
]));
return true;
}
/** @codeCoverageIgnore */ /** @codeCoverageIgnore */
public function getProgress(): ProgressInterface public function getProgress(): ProgressInterface
{ {
......
<?php
namespace Breyta\Test\Migrations;
use Breyta\FileHelper;
use Breyta\Migrations;
use Breyta\Test\TestCase;
use Mockery as m;
class CreateMigrationTest extends TestCase
{
protected $path = '/tmp/breyta-test';
protected function setUp()
{
if (file_exists($this->path)) {
exec('rm -Rf ' . $this->path);
}
mkdir($this->path);
$this->migrations = new Migrations($this->mockPdo(), $this->path);
}
/** @test */
public function createsAFileWithTimestamp()
{
$this->migrations->createMigration('JustAName');
self::assertNotNull(FileHelper::getTimeFromFileName($this->getCreatedFileName()));
}
/** @test */
public function addsNameToFileName()
{
$this->migrations->createMigration('JustAName');
self::assertContains('JustAName', $this->getCreatedFileName());
}
/** @test */
public function createsAClassWithNamespace()
{
$this->migrations->createMigration('JustAName');
self::assertSame('Breyta\Migration\JustAName', FileHelper::getClassFromFile($this->getCreatedFileName()));
}
/** @test */
public function appendsFoldersToNamespace()
{
$this->migrations->createMigration('News/CreateArticleTable');
self::assertSame(
'Breyta\Migration\News\CreateArticleTable',
FileHelper::getClassFromFile($this->getCreatedFileName())
);
}
/** @test */
public function containsACommentWithMigrationName()
{
$this->migrations->createMigration('News/CreateArticleTable');
$status = $this->migrations->getStatus();
self::assertContains('// ' . $status->migrations[1]->file, file_get_contents($this->getCreatedFileName()));
}
protected function getCreatedFileName()
{
$files = [];
exec('find ' . $this->path . ' -type f', $files);
self::assertCount(1, $files);
return $files[0];
}
}
...@@ -33,19 +33,10 @@ abstract class TestCase extends MockeryTestCase ...@@ -33,19 +33,10 @@ abstract class TestCase extends MockeryTestCase
{ {
date_default_timezone_set('UTC'); date_default_timezone_set('UTC');
$pdo = $this->pdo = m::mock(\PDO::class);
$pdo->shouldReceive('setAttribute')->andReturn(true)->byDefault();
$pdo->shouldReceive('beginTransaction')->byDefault();
$pdo->shouldReceive('commit')->byDefault();
$pdo->shouldReceive('rollback')->byDefault();
$pdo->shouldReceive('query')->andReturn(false)->byDefault();
$this->mockPreparedStatement('/^insert into migrations/i', true);
$this->mockPreparedStatement('/^update migrations set/i', true, 0);
$resolver = $this->resolver = m::spy(function ($class, ...$args) { $resolver = $this->resolver = m::spy(function ($class, ...$args) {
return new $class(...$args); return new $class(...$args);
}); });
$this->migrations = m::mock(Migrations::class, [$this->pdo, __DIR__ . '/Example', $resolver]) $this->migrations = m::mock(Migrations::class, [$this->mockPdo(), __DIR__ . '/Example', $resolver])
->makePartial(); ->makePartial();
$resolver->shouldReceive('__invoke')->with(AdapterInterface::class, m::type(\Closure::class)) $resolver->shouldReceive('__invoke')->with(AdapterInterface::class, m::type(\Closure::class))
->andReturnUsing(function ($class, callable $executor) { ->andReturnUsing(function ($class, callable $executor) {
...@@ -139,4 +130,18 @@ abstract class TestCase extends MockeryTestCase ...@@ -139,4 +130,18 @@ abstract class TestCase extends MockeryTestCase
$property->setAccessible(true); $property->setAccessible(true);
return $property->getValue($obj); return $property->getValue($obj);
} }
protected function mockPdo()
{
$pdo = $this->pdo = m::mock(\PDO::class);
$pdo->shouldReceive('setAttribute')->andReturn(true)->byDefault();
$pdo->shouldReceive('beginTransaction')->byDefault();
$pdo->shouldReceive('commit')->byDefault();
$pdo->shouldReceive('rollback')->byDefault();
$pdo->shouldReceive('query')->andReturn(false)->byDefault();
$this->mockPreparedStatement('/^insert into migrations/i', true);
$this->mockPreparedStatement('/^update migrations set/i', true, 0);
return $pdo;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment