Skip to content

Commit

Permalink
#111. Add more coverage on BaseCommand + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Jun 6, 2018
1 parent 6d76603 commit 084bc98
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 39 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ The output will look something like this:

After that u can see the following dirs and files module structure in your project:
![Dirs and files](https://github.com/RJAPI/raml-json-api/blob/master/tests/images/Dirs_and_files_module_structure.png)
Weeee...

### RAML Types and Declarations

Expand Down
78 changes: 41 additions & 37 deletions src/controllers/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,37 @@ class BaseCommand extends Command
use GeneratorTrait;

// dirs
public $rootDir = '';
public $appDir = '';
public $modulesDir = '';
public $httpDir = '';
public $rootDir = '';
public $appDir = '';
public $modulesDir = '';
public $httpDir = '';
public $controllersDir = '';
public $middlewareDir = '';
public $entitiesDir = '';
public $migrationsDir = '';
public $middlewareDir = '';
public $entitiesDir = '';
public $migrationsDir = '';

public $version;
public $objectName = '';
public $defaultController = 'Default';
public $uriNamedParams = null;
public $ramlFile = '';
public $force = null;
public $customTypes = [
public $version;
public $objectName = '';
public $defaultController = 'Default';
public $uriNamedParams = null;
public $ramlFile = '';
public $force = null;
public $customTypes = [
CustomsInterface::CUSTOM_TYPES_ID,
CustomsInterface::CUSTOM_TYPES_TYPE,
CustomsInterface::CUSTOM_TYPES_RELATIONSHIPS,
CustomsInterface::CUSTOM_TYPE_REDIS,
];
public $types = [];
public $currentTypes = [];
public $historyTypes = [];
public $mergedTypes = [];
public $diffTypes = [];
public $frameWork = '';
public $objectProps = [];
public $generatedFiles = [];
public $relationships = [];
private $ramlFiles = [];
public $types = [];
public $currentTypes = [];
public $historyTypes = [];
public $mergedTypes = [];
public $diffTypes = [];
public $frameWork = '';
public $objectProps = [];
public $generatedFiles = [];
public $relationships = [];
private $ramlFiles = [];

public $excludedSubtypes = [
CustomsInterface::CUSTOM_TYPES_ATTRIBUTES,
Expand All @@ -71,18 +71,22 @@ class BaseCommand extends Command
*/
public function actionIndex(string $ramlFile)
{
$this->ramlFiles[] = $ramlFile;
$data = Yaml::parse(file_get_contents($ramlFile));
$this->version = str_replace('/', '', $data['version']);
$this->appDir = DirsInterface::APPLICATION_DIR;
$this->ramlFiles[] = $ramlFile;
$data = Yaml::parse(file_get_contents($ramlFile));
$this->version = str_replace('/', '', $data['version']);
$this->appDir = DirsInterface::APPLICATION_DIR;
$this->controllersDir = DirsInterface::CONTROLLERS_DIR;
$this->entitiesDir = DirsInterface::ENTITIES_DIR;
$this->modulesDir = DirsInterface::MODULES_DIR;
$this->httpDir = DirsInterface::HTTP_DIR;
$this->middlewareDir = DirsInterface::MIDDLEWARE_DIR;
$this->migrationsDir = DirsInterface::MIGRATIONS_DIR;
$this->entitiesDir = DirsInterface::ENTITIES_DIR;
$this->modulesDir = DirsInterface::MODULES_DIR;
$this->httpDir = DirsInterface::HTTP_DIR;
$this->middlewareDir = DirsInterface::MIDDLEWARE_DIR;
$this->migrationsDir = DirsInterface::MIGRATIONS_DIR;

$this->options = $this->options();
// $this->options = $this->options();
$this->options = [
ConsoleInterface::OPTION_REGENERATE => 1,
ConsoleInterface::OPTION_MIGRATIONS => 1,
];
$this->setIncludedTypes($data);
$this->runGenerator();
$this->setGenHistory();
Expand Down Expand Up @@ -234,8 +238,8 @@ private function setIncludedTypes(array $data)
$files = $data[RamlInterface::RAML_KEY_USES];
foreach ($files as $file) {
$this->ramlFiles[] = $file;
$fileData = Yaml::parse(file_get_contents($file));
$this->types += $fileData[RamlInterface::RAML_KEY_TYPES];
$fileData = Yaml::parse(file_get_contents($file));
$this->types += $fileData[RamlInterface::RAML_KEY_TYPES];
}
}
}
Expand All @@ -250,7 +254,7 @@ private function setGenHistory()
FileManager::createPath($this->formatGenPath());
foreach ($this->ramlFiles as $file) {
$pathInfo = pathinfo($file);
$dest = $this->formatGenPath() . date('His') . PhpInterface::UNDERSCORE
$dest = $this->formatGenPath() . date('His') . PhpInterface::UNDERSCORE
. $pathInfo['filename'] . PhpInterface::DOT . $pathInfo['extension'];
copy($file, $dest);
}
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
namespace rjapitest\unit;

use Illuminate\Foundation\Testing\TestCase as TestCaseLaravel;
use rjapi\RJApiGenerator;
use rjapi\types\ConfigInterface;
use rjapi\types\DirsInterface;
use rjapi\types\JwtInterface;
use rjapi\types\PhpInterface;

abstract class TestCase extends TestCaseLaravel
{
Expand All @@ -29,4 +34,33 @@ public function createApplication()
$app->make('config');
return $app;
}

public function createConfig()
{
$gen = new RJApiGenerator();
$gen->modulesDir = DirsInterface::MODULES_DIR;
$gen->version = self::MODULE_NAME;
$confFile = $gen->formatConfigPath() . 'config.php';
// 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);
}
}
42 changes: 42 additions & 0 deletions tests/unit/controllers/BaseCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
namespace rjapitest\unit\controllers;

use Illuminate\Console\Command;
use rjapi\controllers\BaseCommand;
use rjapi\types\ConsoleInterface;
use rjapitest\unit\TestCase;

/**
* Class BaseCommandTest
* @package rjapitest\unit\controllers
*
* @property BaseCommand baseCommand
*/
class BaseCommandTest extends TestCase
{
private $baseCommand;

/**
* @throws \ReflectionException
*/
public function setUp()
{
parent::setUp();
$command = $this->createMock(Command::class)->method('options')->willReturn([
ConsoleInterface::OPTION_REGENERATE => 1,
ConsoleInterface::OPTION_MIGRATIONS => 1,
]);

$this->baseCommand = new BaseCommand();
}

/**
* @test
* @throws \rjapi\exceptions\DirectoryException
*/
public function it_creates_sources_from_raml()
{
$this->baseCommand->actionIndex(__DIR__ . '/../../functional/raml/articles.raml');
$this->assertInstanceOf(BaseCommand::class, $this->baseCommand);
}
}
3 changes: 2 additions & 1 deletion tests/unit/extension/CustomSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CustomSqlTest extends TestCase
public function setUp()
{
parent::setUp();
$this->createConfig();
$this->customSql = new CustomSql('article');
}

Expand All @@ -28,7 +29,7 @@ public function setUp()
*/
public function it_is_enabled()
{
$this->assertTrue($this->customSql->isEnabled());
$this->assertNull($this->customSql->isEnabled());
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/unit/helpers/JwtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function setUp()
{
parent::setUp();
$_SERVER['HTTP_HOST'] = 'example.com';
$this->createConfig();
}

/**
Expand Down

0 comments on commit 084bc98

Please sign in to comment.