Skip to content

Commit

Permalink
#111. Add more coverage on Json/StateMachine/Migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Jun 2, 2018
1 parent ad85d33 commit e286f45
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 194 deletions.
292 changes: 121 additions & 171 deletions clover.xml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/extension/FsmTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait FsmTrait
* @param array $jsonProps JSON input properties
* @throws \rjapi\exceptions\AttributesException
*/
private function checkFsmCreate(array &$jsonProps)
private function checkFsmCreate(array &$jsonProps) : void
{
$stateMachine = new StateMachine($this->entity);
$stateField = $stateMachine->getField();
Expand Down Expand Up @@ -51,7 +51,7 @@ private function checkFsmCreate(array &$jsonProps)
* @param $model
* @throws \rjapi\exceptions\AttributesException
*/
private function checkFsmUpdate(array $jsonProps, $model)
private function checkFsmUpdate(array $jsonProps, $model) : void
{
$stateMachine = new StateMachine($this->entity);
$field = $stateMachine->getField();
Expand Down
18 changes: 6 additions & 12 deletions src/extension/StateMachine.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,12 @@ public function __construct(string $entity)
/**
* @param string $field
* @return bool
* @throws AttributesException
*/
public function isStatedField(string $field)
public function isStatedField(string $field) : bool
{
if(empty($this->machine[$field]) === false
return empty($this->machine[$field]) === false
&& $this->machine[$field][ConfigInterface::ENABLED] === true
&& empty($this->machine[$field][ConfigInterface::STATES]) === false
) {
return true;
}

return false;
&& empty($this->machine[$field][ConfigInterface::STATES]) === false;
}

/**
Expand All @@ -48,7 +42,7 @@ public function isStatedField(string $field)
*/
public function isTransitive($from, $to): bool
{
return $from === $to || in_array($to, $this->states[$from]);
return $from === $to || in_array($to, $this->states[$from], true);
}

public function isInitial($state): bool
Expand All @@ -69,7 +63,7 @@ public function getInitial()
* @param string $field
* @throws AttributesException
*/
public function setStates(string $field)
public function setStates(string $field) : void
{
if(empty($this->machine[$field][ConfigInterface::STATES])) {
throw new AttributesException('There should be "states" element filled in with FSM.');
Expand All @@ -81,7 +75,7 @@ public function setStates(string $field)
* @param string $field
* @throws AttributesException
*/
public function setInitial(string $field)
public function setInitial(string $field) : void
{
if(empty($this->machine[$field][ConfigInterface::STATES][ConfigInterface::INITIAL][0])) {
throw new AttributesException('There should be an initial value for: "' . $field . '" field."');
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/MigrationsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class MigrationsHelper
{
const PATTERN_SPLIT_UC = '/(?=[A-Z])/';
private const PATTERN_SPLIT_UC = '/(?=[A-Z])/';

public static function getTableName(string $objectName)
{
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

namespace rjapitest\unit;

use Illuminate\Foundation\Testing\TestCase as TestCaseLaravel;

abstract class TestCase extends TestCaseLaravel
{
public const DIR_OUTPUT = './tests/_output/';
public const CONFIG_KEY = 'v2';
public const MODULE_NAME = 'V2';
public const DIR_OUTPUT = './tests/_output/';

/**
* The base URL to use while testing the application.
Expand Down
37 changes: 31 additions & 6 deletions tests/unit/blocks/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php

namespace rjapitest\unit\blocks;

use rjapi\blocks\Config;
use rjapi\RJApiGenerator;
use rjapi\types\ConfigInterface;
use rjapi\types\DirsInterface;
use rjapi\types\JwtInterface;
use rjapi\types\PhpInterface;
use rjapi\types\RamlInterface;
use rjapitest\unit\TestCase;
use Symfony\Component\Yaml\Yaml;
Expand All @@ -27,18 +30,19 @@ public function setUp()
$this->gen->modulesDir = DirsInterface::MODULES_DIR;
$this->gen->controllersDir = DirsInterface::CONTROLLERS_DIR;
$this->gen->httpDir = DirsInterface::HTTP_DIR;
$this->gen->version = 'V2';
$this->gen->version = self::MODULE_NAME;
$ramlData = Yaml::parse(file_get_contents(__DIR__ . '/../../functional/raml/articles.raml'));
$this->gen->types = $ramlData[RamlInterface::RAML_KEY_TYPES];
$this->gen->objectProps = [
$this->gen->types = $ramlData[RamlInterface::RAML_KEY_TYPES];
$this->gen->objectProps = [
'type' => 'Type',
'id' => 'ID',
'attributes' => 'ArticleAttributes',
'relationships' => [
'type' => 'TagRelationships[] | TopicRelationships',
]
];
$this->config = new Config($this->gen);
$this->config = new Config($this->gen);
$this->assertInstanceOf(ConfigInterface::class, $this->config);
}

/**
Expand All @@ -47,8 +51,29 @@ public function setUp()
public function it_creates_config()
{
$this->assertInstanceOf(ConfigInterface::class, $this->config);
// todo: mock ConfigInterface::DEFAULT_ACTIVATE for jwt to run without failing
$this->config->create();
$this->assertTrue(file_exists($this->gen->formatConfigPath() . 'config.php'));
$confFile = $this->gen->formatConfigPath() . 'config.php';
$this->assertTrue(file_exists($confFile));
// mocking config for further usage
$arr = include $confFile;
// to get jwt not expired for verifying in JwtTest
$arr[JwtInterface::JWT][ConfigInterface::ACTIVATE] = 0;
// custom sql for CustomSqlTest
$arr['custom_sql'] = [
'article' => [
'enabled' => true,
'query' => 'SELECT id, title FROM article a INNER JOIN tag_article ta ON ta.article_id=a.id
WHERE ta.tag_id IN (
SELECT id FROM tag WHERE CHAR_LENGTH(title) > :tag_len
) ORDER BY a.id DESC',
'bindings' => [
'tag_len' => 5,
]
],
];
$str = PhpInterface::PHP_OPEN_TAG . PhpInterface::SPACE . 'return' . PhpInterface::SPACE . var_export($arr, true) . ';';
$fp = fopen($confFile, 'r+');
fwrite($fp, $str);
fclose($fp);
}
}
2 changes: 1 addition & 1 deletion tests/unit/blocks/ControllersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function setUp()
parent::setUp();
$gen = new RJApiGenerator();
$gen->objectName = 'Article';
$gen->version = 'v2';
$gen->version = self::MODULE_NAME;
$gen->modulesDir = DirsInterface::MODULES_DIR;
$gen->controllersDir = DirsInterface::CONTROLLERS_DIR;
$gen->httpDir = DirsInterface::HTTP_DIR;
Expand Down
66 changes: 66 additions & 0 deletions tests/unit/extension/StateMachineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace rjapitest\unit\extensions;

use rjapi\exceptions\AttributesException;
use rjapi\extension\StateMachine;
use rjapitest\unit\TestCase;

/**
* Class StateMachineTest
* @package rjapitest\unit\extensions
*
* @property StateMachine stateMachine
*/
class StateMachineTest extends TestCase
{
private $stateMachine;

public function setUp()
{
parent::setUp();
$this->stateMachine = new StateMachine('article');
try {
$this->stateMachine->setStates('status');
} catch (AttributesException $e) {
}
}

/**
* @test
*/
public function it_is_stated()
{
$this->assertTrue($this->stateMachine->isStatedField('status'));
}

/**
* @test
* @expectedException \rjapi\exceptions\AttributesException
*/
public function it_is_transitive()
{
$this->assertTrue($this->stateMachine->isTransitive('draft', 'published'));
$this->stateMachine->setStates('foo');
}

/**
* @test
* @expectedException \rjapi\exceptions\AttributesException
*/
public function it_sets_initial_and_gets_field()
{
$this->stateMachine->setInitial('status');
$this->assertEquals($this->stateMachine->getInitial(), 'draft');
$this->assertTrue($this->stateMachine->isInitial('draft'));
$this->stateMachine->setInitial('foo');
}

/**
* @test
*/
public function it_gets_field()
{
$this->assertEquals('status', $this->stateMachine->getField());
}
}
27 changes: 27 additions & 0 deletions tests/unit/helpers/ConfigHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace rjapitest\unit\helpers;

use rjapi\helpers\ConfigHelper;
use rjapi\types\ConfigInterface;
use rjapitest\unit\TestCase;

/**
Expand Down Expand Up @@ -34,5 +35,31 @@ public function it_gets_query_param()
$paramSort = 'sort';
$sortData = ConfigHelper::getQueryParam($paramSort);
$this->assertEquals($sortData, $this->params[$paramSort]);
$this->assertNull(ConfigHelper::getQueryParam('foo'));
}

/**
* @test
*/
public function it_gets_config_key()
{
$this->assertEquals(ConfigHelper::getConfigKey(), self::CONFIG_KEY);
}

/**
* @test
*/
public function it_gets_module_name()
{
$this->assertEquals(ConfigHelper::getModuleName(), self::MODULE_NAME);
}

/**
* @test
*/
public function it_gets_nested_param()
{
$this->assertTrue(ConfigHelper::getNestedParam(ConfigInterface::JWT, ConfigInterface::ENABLED));
$this->assertTrue(ConfigHelper::getNestedParam(ConfigInterface::JWT, ConfigInterface::ENABLED, true));
}
}
13 changes: 13 additions & 0 deletions tests/unit/helpers/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ public function it_returns_json_error()
);
}

/**
* @test
*/
public function it_encodes_decodes_json()
{
$this->assertEquals('{"title":"Foo Bar Baz"}', Json::encode([
'title' => 'Foo Bar Baz'
]));
$this->assertEquals([
'title' => 'Foo Bar Baz'
], Json::decode('{"title":"Foo Bar Baz"}'));
}

// /**
// * @test
// */
Expand Down

0 comments on commit e286f45

Please sign in to comment.