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

implement basic locating logic

parent 7ba9b4c1
No related branches found
No related tags found
No related merge requests found
...@@ -12,7 +12,7 @@ class CreateMigrationTable extends AbstractMigration ...@@ -12,7 +12,7 @@ class CreateMigrationTable extends AbstractMigration
file CHARACTER VARYING (64) NOT NULL, file CHARACTER VARYING (64) NOT NULL,
executed TIMESTAMP NOT NULL, executed TIMESTAMP NOT NULL,
status CHARACTER VARYING (16) NOT NULL DEFAULT 'done', status CHARACTER VARYING (16) NOT NULL DEFAULT 'done',
info TEXT, executions TEXT,
executionTime DOUBLE PRECISION, executionTime DOUBLE PRECISION,
PRIMARY KEY (file) PRIMARY KEY (file)
)"); )");
......
...@@ -4,5 +4,58 @@ namespace Breyta; ...@@ -4,5 +4,58 @@ namespace Breyta;
class Migrations class Migrations
{ {
/** @var \PDO */
protected $db;
/** @var string */
protected $path;
/** @var array */
protected $migrations;
public function __construct(\PDO $db, string $path)
{
if (!file_exists($path) || !is_dir($path)) {
throw new \InvalidArgumentException('The path to migrations is not valid');
}
$this->db = $db;
$this->path = $path;
}
public function getStatus(): \stdClass
{
return (object)[
'migrations' => $this->getMigrations(),
'count' => 0,
];
}
protected function getMigrations(): array
{
if (!$this->migrations) {
$this->migrations = $this->findMigrations();
}
return $this->migrations;
}
protected function findMigrations(): array
{
$migrations = [(object)[
'file' => '@breyta/CreateMigrationTable.php',
'status' => 'new',
]];
$migrations = array_merge($migrations, array_map(function ($path) {
return (object)[
'file' => basename($path),
'status' => 'new',
];
}, array_filter(scandir($this->path), function ($path) {
return $path !== '..' && $path !== '.';
})));
return $migrations;
}
} }
<?php
namespace Breyta\Test\Migrations;
use Breyta\Migrations;
use Breyta\Test\TestCase;
use Mockery as m;
class LocatingMigrationsTest extends TestCase
{
protected $pdo;
protected function setUp()
{
parent::setUp();
$this->pdo = m::mock(\PDO::class);
}
/** @test */
public function returnsAStatusObject()
{
$migrations = new Migrations($this->pdo, __DIR__ . '/../Example');
$status = $migrations->getStatus();
self::assertObjectHasAttribute('migrations', $status);
self::assertObjectHasAttribute('count', $status);
}
/** @test */
public function findsMigrationsInTheGivenPath()
{
$migrations = new Migrations($this->pdo, __DIR__ . '/../Example');
$status = $migrations->getStatus();
self::assertContains((object)[
'file' => 'CreateAnimalsTable.php',
'status' => 'new',
], $status->migrations, '', false, false);
}
// /** @test */
// public function findsMigrationsInSubFolders()
// {
// $migrations = new Migrations($this->pdo, __DIR__ . '/../Example');
//
// $status = $migrations->getStatus();
//
// self::assertContains((object)[
// 'file' => 'Grouped/2018-11-22T22-59-59_FooBar.php',
// 'status' => 'new',
// ], $status->migrations, '', false, false);
// }
/** @test */
public function throwsWhenTheFolderDoesNotExist()
{
self::expectException(\InvalidArgumentException::class);
self::expectExceptionMessage('The path to migrations is not valid');
$migrations = new Migrations(m::mock(\PDO::class), '/any/non-existing/path');
}
/** @test */
public function throwsWhenTheGivenPathIsAFile()
{
self::expectException(\InvalidArgumentException::class);
self::expectExceptionMessage('The path to migrations is not valid');
$migrations = new Migrations(m::mock(\PDO::class), __FILE__);
}
/** @test */
public function throwsWhenTheGivenPathIsASymLinkToAFile()
{
if (file_exists('/tmp/symlink')) {
unlink('/tmp/symlink');
}
if (!@symlink(__FILE__, '/tmp/symlink')) {
$this->markTestSkipped('Could not create a symlink');
return;
}
self::expectException(\InvalidArgumentException::class);
self::expectExceptionMessage('The path to migrations is not valid');
$migrations = new Migrations(m::mock(\PDO::class), '/tmp/symlink');
}
/** @test */
public function allowsSymLinksToADirectory()
{
if (file_exists('/tmp/symlink')) {
unlink('/tmp/symlink');
}
if (!@symlink(__DIR__, '/tmp/symlink')) {
$this->markTestSkipped('Could not create a symlink');
return;
}
$migrations = new Migrations(m::mock(\PDO::class), '/tmp/symlink');
self::assertInstanceOf(Migrations::class, $migrations);
}
}
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