Skip to content

Commit

Permalink
#109. Fix functional tests crashing with incompatible ids + readme
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Jun 19, 2018
1 parent 7c6c892 commit a0b75fc
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 17 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ JSON API support turned on by default - see `Turn off JSON API support` section
* [Models](#user-content-models)
* [Routes](#user-content-routes)
* [Migrations](#user-content-migrations)
* [Tests](#user-content-tests)
* [Relationships](#user-content-relationships-particular-qualities)
* [Query parameters](#user-content-query-parameters)
* [Security](#user-content-security)
Expand Down Expand Up @@ -591,6 +592,19 @@ an example for foreign key would be like:
onUpdate: cascade
```

#### Tests
To provide convenient way for integration/functional testing, one can generate tests by providing `--tests` command option, e.g.:
```bash
php artisan raml:generate raml/articles.raml --migrations --tests
```
in command output you'll see the following files have been created:
```bash
tests/functional/ArticleCest.php created
...
tests/functional/TagCest.php created
```
For more info on how to set an environment for functional tests in Laravel - see https://codeception.com/for/laravel

### Relationships particular qualities
To let generator know about what a particular relationship to apply (ex.: ManyToMany, OneToMany, OneToOne)
set the ```relationships``` property in an Entity like so - for ex. let's see how to set ManyToOne relationship between Article and Tag entities.
Expand Down
14 changes: 10 additions & 4 deletions src/blocks/ContentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,20 @@ private function getMethodParams(array $params) : string
/**
* @param array $params
*
* @param bool $arrayToJson
* @return string
*/
private function getMethodParamsToPass(array $params) : string
private function getMethodParamsToPass(array $params, $arrayToJson = true) : string
{
$paramsStr = '';
$cnt = count($params);
foreach ($params as $value) {
--$cnt;
$paramsStr .= is_array($value) ? $this->quoteParam(json_encode($value)) : $this->quoteParam($value);
if (is_array($value)) {
$paramsStr .= $arrayToJson ? $this->quoteParam(json_encode($value)) : var_export($value, true);
} else {
$paramsStr .= $this->quoteParam($value);
}
if ($cnt > 0) {
$paramsStr .= PhpInterface::COMMA . PhpInterface::SPACE;
}
Expand Down Expand Up @@ -400,12 +405,13 @@ private function setAfterMethods() : void
* @param string $object
* @param string $method
* @param array $params
* @param bool $arrayToJson
*/
private function methodCallOnObject(string $object, string $method, array $params = []) : void
private function methodCallOnObject(string $object, string $method, array $params = [], $arrayToJson = true) : void
{
$this->sourceCode .= $this->setTabs(2) . PhpInterface::DOLLAR_SIGN . $object
. PhpInterface::ARROW . $method . PhpInterface::OPEN_PARENTHESES
. $this->getMethodParamsToPass($params)
. $this->getMethodParamsToPass($params, $arrayToJson)
. PhpInterface::CLOSE_PARENTHESES . PhpInterface::SEMICOLON . PHP_EOL;
}
}
75 changes: 64 additions & 11 deletions src/blocks/Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace rjapi\blocks;

use Faker\Factory;
use rjapi\extension\JSONApiInterface;
use rjapi\helpers\Classes;
use rjapi\helpers\MethodOptions;
use rjapi\types\DefaultInterface;
Expand All @@ -17,6 +16,7 @@ class Tests
use ContentManager;

private $className;
private $attributesState = [];

protected $sourceCode = '';
protected $isSoftDelete = false;
Expand All @@ -42,6 +42,7 @@ public function setContent() : void
$this->startMethod($methodOpts);
$this->endMethod();
// main test methods
$this->collectProps();
$this->setComment(DefaultInterface::METHOD_START);
$this->setCreateContent($methodOpts);
$this->setIndexContent($methodOpts);
Expand All @@ -64,7 +65,7 @@ private function setIndexContent(MethodOptions $methodOpts) : void
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEND_GET,
[PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH . mb_strtolower($this->generator->objectName)]);
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_IS_JSON);
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS, [$this->getJsonApiRequest()]);
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS_JSON, [$this->getJsonApiResponse(true)], false);
$this->endMethod();
}

Expand All @@ -73,7 +74,7 @@ private function setIndexContent(MethodOptions $methodOpts) : void
*/
private function setViewContent(MethodOptions $methodOpts) : void
{
$id = 1;
$id = $id = $this->getId();
$methodOpts->setName(TestsInterface::TRY . $this->generator->objectName . ucfirst(MethodsInterface::VIEW));
$this->startMethod($methodOpts);
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::IM_GOING_TO,
Expand All @@ -83,7 +84,7 @@ private function setViewContent(MethodOptions $methodOpts) : void
[PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH . mb_strtolower($this->generator->objectName)
. PhpInterface::SLASH . $id]);
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_IS_JSON);
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS, [$this->getJsonApiRequest()]);
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS_JSON, [$this->getJsonApiResponse(true)], false);
$this->endMethod();
}

Expand All @@ -101,7 +102,7 @@ private function setCreateContent(MethodOptions $methodOpts) : void
[PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH . mb_strtolower($this->generator->objectName),
$this->getJsonApiRequest()]);
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_IS_JSON);
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS, [$this->getJsonApiRequest()]);
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS_JSON, [$this->getJsonApiResponse(true)], false);
$this->endMethod();
}

Expand All @@ -110,7 +111,7 @@ private function setCreateContent(MethodOptions $methodOpts) : void
*/
private function setUpdateContent(MethodOptions $methodOpts) : void
{
$id = 1;
$id = $id = $this->getId();
$methodOpts->setName(TestsInterface::TRY . $this->generator->objectName . ucfirst(MethodsInterface::UPDATE));
$this->startMethod($methodOpts);
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::IM_GOING_TO,
Expand All @@ -120,7 +121,7 @@ private function setUpdateContent(MethodOptions $methodOpts) : void
[PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH . mb_strtolower($this->generator->objectName)
. PhpInterface::SLASH . $id, $this->getJsonApiRequest()]);
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_IS_JSON);
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS, [$this->getJsonApiRequest()]);
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS_JSON, [$this->getJsonApiResponse(true)], false);
$this->endMethod();
}

Expand All @@ -129,7 +130,10 @@ private function setUpdateContent(MethodOptions $methodOpts) : void
*/
private function setDeleteContent(MethodOptions $methodOpts) : void
{
$id = 1;
$id = $this->getId();
if (empty($this->attributesState[RamlInterface::RAML_ID]) === false) {
$id = $this->attributesState[RamlInterface::RAML_ID];
}
$methodOpts->setName(TestsInterface::TRY . $this->generator->objectName . ucfirst(MethodsInterface::DELETE));
$this->startMethod($methodOpts);
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::IM_GOING_TO,
Expand All @@ -140,15 +144,30 @@ private function setDeleteContent(MethodOptions $methodOpts) : void
$this->endMethod();
}

private function getId()
{
if (empty($this->attributesState[RamlInterface::RAML_ID]) === false) {
return $this->attributesState[RamlInterface::RAML_ID];
}
return 1;
}

/**
* @param bool $required
* @return array
*/
private function getJsonApiRequest() : array
private function getJsonApiRequest($required = false) : array
{
$attrs = [];
// set id if it's string
if (empty($this->attributesState[RamlInterface::RAML_ID]) === false) {
$attrs[RamlInterface::RAML_ID] = $this->attributesState[RamlInterface::RAML_ID];
}
$props = $this->generator->types[$this->generator->objectProps[RamlInterface::RAML_ATTRS]][RamlInterface::RAML_PROPS];
foreach ($props as $attrKey => $attrVal) {
$attrs[$attrKey] = $this->getAttributeValue($attrVal);
foreach ($this->attributesState as $attrKey => $attrVal) {
if ($required === false || ($required === true && empty($props[$attrKey][RamlInterface::RAML_KEY_REQUIRED]) === false)) {
$attrs[$attrKey] = $attrVal;
}
}
return [
RamlInterface::RAML_DATA => [
Expand All @@ -158,6 +177,40 @@ private function getJsonApiRequest() : array
];
}

/**
* @param bool $required
* @return array
*/
private function getJsonApiResponse($required = false) : array
{
$attrs = [];
$props = $this->generator->types[$this->generator->objectProps[RamlInterface::RAML_ATTRS]][RamlInterface::RAML_PROPS];
foreach ($this->attributesState as $attrKey => $attrVal) {
if ($required === false || ($required === true && empty($props[$attrKey][RamlInterface::RAML_KEY_REQUIRED]) === false)) {
$attrs[$attrKey] = $attrVal;
}
}
return [
RamlInterface::RAML_DATA => [
RamlInterface::RAML_TYPE => mb_strtolower($this->generator->objectName),
RamlInterface::RAML_ID => $this->getId(),
RamlInterface::RAML_ATTRS => $attrs,
],
];
}

private function collectProps()
{
$idObject = $this->generator->types[$this->generator->types[$this->generator->objectName][RamlInterface::RAML_PROPS][RamlInterface::RAML_ID]];
if ($idObject[RamlInterface::RAML_TYPE] === RamlInterface::RAML_TYPE_STRING) {
$this->attributesState[RamlInterface::RAML_ID] = uniqid();
}
$props = $this->generator->types[$this->generator->objectProps[RamlInterface::RAML_ATTRS]][RamlInterface::RAML_PROPS];
foreach ($props as $attrKey => $attrVal) {
$this->attributesState[$attrKey] = $this->getAttributeValue($attrVal);
}
}

/**
* @param array $attrVal
* @return mixed
Expand Down
4 changes: 2 additions & 2 deletions src/types/TestsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ interface TestsInterface
public const SEND_PATCH = 'sendPATCH';
public const SEND_DELETE = 'sendDELETE';

public const SEE_RESP_IS_JSON = 'seeResponseIsJson';
public const SEE_RESP_CONTAINS = 'seeResponseContains';
public const SEE_RESP_IS_JSON = 'seeResponseIsJson';
public const SEE_RESP_CONTAINS_JSON = 'seeResponseContainsJson';
}

0 comments on commit a0b75fc

Please sign in to comment.