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
2 parents 49fc9dd + dcde5c8 commit e3bf6f9
Show file tree
Hide file tree
Showing 34 changed files with 122 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ database/
*_create*_table.php
tests/functional/*Cest.php
composer.lock
infection.json.dist
infection.json.dist
infection-log.txt
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1646,7 +1646,15 @@ There are also more things you can do, about rewinding history:

Although, if you need to totally rollback the state of a system - use `--rollback` option, with the same keys as in merge.

======================
==== Infection code coverage ====

Metrics:

Mutation Score Indicator (MSI): 81%
Mutation Code Coverage: 86%
Covered Code MSI: 93%

==========

HTTP request/response examples can be found on WiKi page - https://github.com/SoliDry/api-generator/wiki

Expand Down
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
2 changes: 1 addition & 1 deletion tests/Unit/Blocks/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ConfigTest extends TestCase
private $gen;
private $config;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->gen = new ApiGenerator();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Blocks/ControllersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ControllersTest extends TestCase
/** @var Controllers $controller */
private $controller;

public function setUp()
public function setUp(): void
{
parent::setUp();
$gen = new ApiGenerator();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Blocks/EntitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class EntitiesTest extends TestCase
/**
* @throws \ReflectionException
*/
public function setUp()
public function setUp(): void
{
parent::setUp();
/** @var ApiGenerator $gen */
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Blocks/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MiddlewareTest extends TestCase
/**
* @throws \ReflectionException
*/
public function setUp()
public function setUp(): void
{
parent::setUp();
/** @var ApiGenerator|PHPUnit_Framework_MockObject_MockObject $gen */
Expand Down
41 changes: 37 additions & 4 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 All @@ -24,10 +26,7 @@ class MigrationsTest extends TestCase
private $migrations;
private $gen;

/**
* @throws \ReflectionException
*/
public function setUp()
public function setUp(): void
{
parent::setUp();
/** @var ApiGenerator|PHPUnit_Framework_MockObject_MockObject $gen */
Expand Down Expand Up @@ -66,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
2 changes: 1 addition & 1 deletion tests/Unit/Blocks/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RouterTest extends TestCase
private $gen;
private $router;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->gen = new ApiGenerator();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Controllers/BaseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class BaseCommandTest extends TestCase
{
private $baseCommand;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->baseCommand = new BaseCommand();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Controllers/GeneratorTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GeneratorTraitTest extends TestCase

private $options;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->options = [ // merge last option
Expand Down
5 changes: 4 additions & 1 deletion tests/Unit/Extension/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ class ApiControllerTest extends TestCase

private $baseController;

public function setUp()
/**
* @throws \SoliDry\Exceptions\HeadersException
*/
public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Extension/BaseControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BaseControllerTest extends TestCase
{
private $baseController;

public function setUp()
public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Extension/BaseFormRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BaseFormRequestTest extends TestCase
{
private $baseFormRequest;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->baseFormRequest = new BaseFormRequest();
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Extension/BaseModelTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BaseModelTraitTest extends TestCase
private $customSql;
private $isTree = false;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->article = ArticleFixture::createAndGet();
Expand Down Expand Up @@ -88,7 +88,7 @@ public function it_gets_model_entities()
$this->assertInstanceOf(Builder::class, $articles);
}

public function tearDown()
public function tearDown(): void
{
ArticleFixture::delete($this->article->id);
parent::tearDown();
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Extension/BaseRelationsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BaseRelationsTraitTest extends TestCase
private $topic;
private $modelEntity;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->article = ArticleFixture::createAndGet();
Expand Down Expand Up @@ -48,7 +48,7 @@ public function it_sets_relationships()
$this->assertEquals($this->topic->id, $article->topic_id);
}

public function tearDown()
public function tearDown(): void
{
ArticleFixture::delete($this->article->id);
TopicFixture::delete($this->topic->id);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Extension/BitMaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BitMaskTest extends TestCase
private const FIELD = 'permissions';
private $bitMask;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->bitMask = new BitMask([
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Extension/BitMaskTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BitMaskTraitTest extends TestCase
private $user;
private $model;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->bitMask = new BitMask([
Expand Down Expand Up @@ -118,7 +118,7 @@ public function it_sets_mask_update()
$this->assertEquals(16, $this->model->permissions);
}

public function tearDown()
public function tearDown(): void
{
UserFixture::delete($this->user->id);
UserFixture::delete($this->users[0]->id);
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Extension/CacheTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CacheTraitTest extends TestCase
private $req;
private $item;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->configOptions = new ConfigOptions();
Expand Down Expand Up @@ -85,7 +85,7 @@ private function getEntity($id, array $data = ModelsInterface::DEFAULT_DATA)
return ArticleFixture::createAndGet();
}

public function tearDown()
public function tearDown(): void
{
ArticleFixture::delete($this->item->id);
Redis::flushall();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Extension/CustomSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CustomSqlTest extends TestCase
{
private $customSql;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->createConfig();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Extension/JwtTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class JwtTraitTest extends TestCase

private $model;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->model = new User();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Extension/OptionsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class OptionsTraitTest extends TestCase
private $configOptions;
private $entity;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->entity = 'article'; // test cache etc
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Extension/SpellCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SpellCheckTest extends TestCase
/**
*
*/
public function setUp()
public function setUp(): void
{
parent::setUp();
$this->spellCheck = new SpellCheck(self::ENTITY);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Extension/StateMachineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class StateMachineTest extends TestCase
private $stateMachine;
private $entity;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->entity = 'article';
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Helpers/ConfigHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ConfigHelperTest extends TestCase
'expires' => 3600,
];

public function setUp()
public function setUp(): void
{
parent::setUp();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Helpers/ConfigOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ConfigOptionsTest extends TestCase
{
private $configOptions;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->configOptions = new ConfigOptions();
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/Helpers/ErrorsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace SoliDryTest\Unit\Helpers;


use SoliDry\Extension\JSONApiInterface;
use SoliDry\Helpers\Errors;
use SoliDryTest\Unit\TestCase;

class ErrorsTest extends TestCase
{

private const ENTITY = 'article';
private const ID = 123;

/**
* @test
*/
public function it_tests_error_model_not_found()
{
$error = (new Errors())->getModelNotFound(self::ENTITY, self::ID);
$this->assertArraySubset([
[
JSONApiInterface::ERROR_TITLE => 'Database object '
. self::ENTITY . ' with $id = ' . self::ID .
' - not found.',
],
], $error);
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Helpers/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class JsonTest extends TestCase
/** @var PHPUnit_Framework_MockObject_MockObject | BaseModel baseFormRequest */
private $baseModel = null;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->baseModel = new Article();
Expand Down
Loading

0 comments on commit e3bf6f9

Please sign in to comment.