Skip to content

Commit

Permalink
#111. Add MigrationsTest create new migrations, Change yoda style vis…
Browse files Browse the repository at this point in the history
…e-versa
  • Loading branch information
arthurkushman committed Mar 10, 2019
1 parent cb74a35 commit dcde5c8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Blocks/Migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,22 @@ public function create()
PhpInterface::UNDERSCORE . ModelsInterface::MIGRATION_TABLE;
$attrKey = $this->generator->objectName . CustomsInterface::CUSTOM_TYPES_ATTRIBUTES;

$isFileExist = FileManager::migrationNotExists($this->generator, $migrationName);
$isAdding = (true === $this->generator->isMerge && empty($this->generator->diffTypes[$attrKey]) === false);
if(true === $isFileExist)
$isFileNonExistent = FileManager::migrationNotExists($this->generator, $migrationName);
$isAdding = ($this->generator->isMerge === true && empty($this->generator->diffTypes[$attrKey]) === false);

if($isFileNonExistent === true)
{
$this->setContent();
} else if (true === $isAdding) {
} else if ($isAdding === true) {
$migrationName = str_replace(ModelsInterface::MIGRATION_TABLE_PTTRN, $this->tableName,
ModelsInterface::MIGRATION_ADD_COLUMN);
// file exists and it is merge op - add columns/indices for this table
$columnName = key($this->generator->diffTypes[$attrKey]);
$migrationName = str_replace(ModelsInterface::MIGRATION_COLUMN_PTTRN, $columnName, $migrationName);
$this->resetContent($this->generator->diffTypes[$attrKey], $columnName);
}
if (true === $isFileExist || true === $isAdding) {

if ($isFileNonExistent === true || $isAdding === true) {
$this->createMigrationFile($migrationName);
}
}
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/Blocks/MigrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace SoliDryTest\Unit\Blocks;

use PHPUnit_Framework_MockObject_MockObject;
use SoliDry\Blocks\FileManager;
use SoliDry\Blocks\MigrationsAbstract;
use SoliDry\ApiGenerator;
use SoliDry\Types\ConsoleInterface;
use SoliDry\Types\DirsInterface;
use SoliDry\Types\ApiInterface;
use SoliDry\Types\PhpInterface;
use SoliDryTest\Unit\TestCase;
use SoliDry\Blocks\Migrations;
use Symfony\Component\Yaml\Yaml;
Expand Down Expand Up @@ -63,6 +65,40 @@ public function it_creates_entity()
$this->migrations->createPivot();
}

/**
* This test creates a new entity to force setContent method
*
* @test
*/
public function it_creates_new_entity()
{
$from = '';
$to = '';

// intentionally del migration file to recreate a new one
$path = FileManager::getModulePath($this->gen) . DirsInterface::DATABASE_DIR . PhpInterface::SLASH
. $this->gen->migrationsDir . PhpInterface::SLASH;
$file = $path . PhpInterface::ASTERISK . 'create_' . strtolower($this->gen->objectName) . PhpInterface::ASTERISK
. PhpInterface::PHP_EXT;
$files = glob($file);

if (empty($files) === false) {
$from = $files[0];
$to = str_replace('_create', '_create_cp', $files[0]);
copy($from, $to);
unlink($from);
}

$this->migrations->setCodeState($this->gen);
$this->assertInstanceOf(MigrationsAbstract::class, $this->migrations);
$this->migrations->create();

if (empty($files) === false) {
copy($to, $from);
unlink($to);
}
}

/**
* @test
*/
Expand Down

0 comments on commit dcde5c8

Please sign in to comment.