Skip to content

Commit a0b75fc

Browse files
committed
#109. Fix functional tests crashing with incompatible ids + readme
1 parent 7c6c892 commit a0b75fc

File tree

4 files changed

+90
-17
lines changed

4 files changed

+90
-17
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ JSON API support turned on by default - see `Turn off JSON API support` section
2323
* [Models](#user-content-models)
2424
* [Routes](#user-content-routes)
2525
* [Migrations](#user-content-migrations)
26+
* [Tests](#user-content-tests)
2627
* [Relationships](#user-content-relationships-particular-qualities)
2728
* [Query parameters](#user-content-query-parameters)
2829
* [Security](#user-content-security)
@@ -591,6 +592,19 @@ an example for foreign key would be like:
591592
onUpdate: cascade
592593
```
593594

595+
#### Tests
596+
To provide convenient way for integration/functional testing, one can generate tests by providing `--tests` command option, e.g.:
597+
```bash
598+
php artisan raml:generate raml/articles.raml --migrations --tests
599+
```
600+
in command output you'll see the following files have been created:
601+
```bash
602+
tests/functional/ArticleCest.php created
603+
...
604+
tests/functional/TagCest.php created
605+
```
606+
For more info on how to set an environment for functional tests in Laravel - see https://codeception.com/for/laravel
607+
594608
### Relationships particular qualities
595609
To let generator know about what a particular relationship to apply (ex.: ManyToMany, OneToMany, OneToOne)
596610
set the ```relationships``` property in an Entity like so - for ex. let's see how to set ManyToOne relationship between Article and Tag entities.

src/blocks/ContentManager.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,20 @@ private function getMethodParams(array $params) : string
235235
/**
236236
* @param array $params
237237
*
238+
* @param bool $arrayToJson
238239
* @return string
239240
*/
240-
private function getMethodParamsToPass(array $params) : string
241+
private function getMethodParamsToPass(array $params, $arrayToJson = true) : string
241242
{
242243
$paramsStr = '';
243244
$cnt = count($params);
244245
foreach ($params as $value) {
245246
--$cnt;
246-
$paramsStr .= is_array($value) ? $this->quoteParam(json_encode($value)) : $this->quoteParam($value);
247+
if (is_array($value)) {
248+
$paramsStr .= $arrayToJson ? $this->quoteParam(json_encode($value)) : var_export($value, true);
249+
} else {
250+
$paramsStr .= $this->quoteParam($value);
251+
}
247252
if ($cnt > 0) {
248253
$paramsStr .= PhpInterface::COMMA . PhpInterface::SPACE;
249254
}
@@ -400,12 +405,13 @@ private function setAfterMethods() : void
400405
* @param string $object
401406
* @param string $method
402407
* @param array $params
408+
* @param bool $arrayToJson
403409
*/
404-
private function methodCallOnObject(string $object, string $method, array $params = []) : void
410+
private function methodCallOnObject(string $object, string $method, array $params = [], $arrayToJson = true) : void
405411
{
406412
$this->sourceCode .= $this->setTabs(2) . PhpInterface::DOLLAR_SIGN . $object
407413
. PhpInterface::ARROW . $method . PhpInterface::OPEN_PARENTHESES
408-
. $this->getMethodParamsToPass($params)
414+
. $this->getMethodParamsToPass($params, $arrayToJson)
409415
. PhpInterface::CLOSE_PARENTHESES . PhpInterface::SEMICOLON . PHP_EOL;
410416
}
411417
}

src/blocks/Tests.php

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace rjapi\blocks;
44

55
use Faker\Factory;
6-
use rjapi\extension\JSONApiInterface;
76
use rjapi\helpers\Classes;
87
use rjapi\helpers\MethodOptions;
98
use rjapi\types\DefaultInterface;
@@ -17,6 +16,7 @@ class Tests
1716
use ContentManager;
1817

1918
private $className;
19+
private $attributesState = [];
2020

2121
protected $sourceCode = '';
2222
protected $isSoftDelete = false;
@@ -42,6 +42,7 @@ public function setContent() : void
4242
$this->startMethod($methodOpts);
4343
$this->endMethod();
4444
// main test methods
45+
$this->collectProps();
4546
$this->setComment(DefaultInterface::METHOD_START);
4647
$this->setCreateContent($methodOpts);
4748
$this->setIndexContent($methodOpts);
@@ -64,7 +65,7 @@ private function setIndexContent(MethodOptions $methodOpts) : void
6465
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEND_GET,
6566
[PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH . mb_strtolower($this->generator->objectName)]);
6667
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_IS_JSON);
67-
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS, [$this->getJsonApiRequest()]);
68+
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS_JSON, [$this->getJsonApiResponse(true)], false);
6869
$this->endMethod();
6970
}
7071

@@ -73,7 +74,7 @@ private function setIndexContent(MethodOptions $methodOpts) : void
7374
*/
7475
private function setViewContent(MethodOptions $methodOpts) : void
7576
{
76-
$id = 1;
77+
$id = $id = $this->getId();
7778
$methodOpts->setName(TestsInterface::TRY . $this->generator->objectName . ucfirst(MethodsInterface::VIEW));
7879
$this->startMethod($methodOpts);
7980
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::IM_GOING_TO,
@@ -83,7 +84,7 @@ private function setViewContent(MethodOptions $methodOpts) : void
8384
[PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH . mb_strtolower($this->generator->objectName)
8485
. PhpInterface::SLASH . $id]);
8586
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_IS_JSON);
86-
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS, [$this->getJsonApiRequest()]);
87+
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS_JSON, [$this->getJsonApiResponse(true)], false);
8788
$this->endMethod();
8889
}
8990

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

@@ -110,7 +111,7 @@ private function setCreateContent(MethodOptions $methodOpts) : void
110111
*/
111112
private function setUpdateContent(MethodOptions $methodOpts) : void
112113
{
113-
$id = 1;
114+
$id = $id = $this->getId();
114115
$methodOpts->setName(TestsInterface::TRY . $this->generator->objectName . ucfirst(MethodsInterface::UPDATE));
115116
$this->startMethod($methodOpts);
116117
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::IM_GOING_TO,
@@ -120,7 +121,7 @@ private function setUpdateContent(MethodOptions $methodOpts) : void
120121
[PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH . mb_strtolower($this->generator->objectName)
121122
. PhpInterface::SLASH . $id, $this->getJsonApiRequest()]);
122123
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_IS_JSON);
123-
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS, [$this->getJsonApiRequest()]);
124+
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS_JSON, [$this->getJsonApiResponse(true)], false);
124125
$this->endMethod();
125126
}
126127

@@ -129,7 +130,10 @@ private function setUpdateContent(MethodOptions $methodOpts) : void
129130
*/
130131
private function setDeleteContent(MethodOptions $methodOpts) : void
131132
{
132-
$id = 1;
133+
$id = $this->getId();
134+
if (empty($this->attributesState[RamlInterface::RAML_ID]) === false) {
135+
$id = $this->attributesState[RamlInterface::RAML_ID];
136+
}
133137
$methodOpts->setName(TestsInterface::TRY . $this->generator->objectName . ucfirst(MethodsInterface::DELETE));
134138
$this->startMethod($methodOpts);
135139
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::IM_GOING_TO,
@@ -140,15 +144,30 @@ private function setDeleteContent(MethodOptions $methodOpts) : void
140144
$this->endMethod();
141145
}
142146

147+
private function getId()
148+
{
149+
if (empty($this->attributesState[RamlInterface::RAML_ID]) === false) {
150+
return $this->attributesState[RamlInterface::RAML_ID];
151+
}
152+
return 1;
153+
}
154+
143155
/**
156+
* @param bool $required
144157
* @return array
145158
*/
146-
private function getJsonApiRequest() : array
159+
private function getJsonApiRequest($required = false) : array
147160
{
148161
$attrs = [];
162+
// set id if it's string
163+
if (empty($this->attributesState[RamlInterface::RAML_ID]) === false) {
164+
$attrs[RamlInterface::RAML_ID] = $this->attributesState[RamlInterface::RAML_ID];
165+
}
149166
$props = $this->generator->types[$this->generator->objectProps[RamlInterface::RAML_ATTRS]][RamlInterface::RAML_PROPS];
150-
foreach ($props as $attrKey => $attrVal) {
151-
$attrs[$attrKey] = $this->getAttributeValue($attrVal);
167+
foreach ($this->attributesState as $attrKey => $attrVal) {
168+
if ($required === false || ($required === true && empty($props[$attrKey][RamlInterface::RAML_KEY_REQUIRED]) === false)) {
169+
$attrs[$attrKey] = $attrVal;
170+
}
152171
}
153172
return [
154173
RamlInterface::RAML_DATA => [
@@ -158,6 +177,40 @@ private function getJsonApiRequest() : array
158177
];
159178
}
160179

180+
/**
181+
* @param bool $required
182+
* @return array
183+
*/
184+
private function getJsonApiResponse($required = false) : array
185+
{
186+
$attrs = [];
187+
$props = $this->generator->types[$this->generator->objectProps[RamlInterface::RAML_ATTRS]][RamlInterface::RAML_PROPS];
188+
foreach ($this->attributesState as $attrKey => $attrVal) {
189+
if ($required === false || ($required === true && empty($props[$attrKey][RamlInterface::RAML_KEY_REQUIRED]) === false)) {
190+
$attrs[$attrKey] = $attrVal;
191+
}
192+
}
193+
return [
194+
RamlInterface::RAML_DATA => [
195+
RamlInterface::RAML_TYPE => mb_strtolower($this->generator->objectName),
196+
RamlInterface::RAML_ID => $this->getId(),
197+
RamlInterface::RAML_ATTRS => $attrs,
198+
],
199+
];
200+
}
201+
202+
private function collectProps()
203+
{
204+
$idObject = $this->generator->types[$this->generator->types[$this->generator->objectName][RamlInterface::RAML_PROPS][RamlInterface::RAML_ID]];
205+
if ($idObject[RamlInterface::RAML_TYPE] === RamlInterface::RAML_TYPE_STRING) {
206+
$this->attributesState[RamlInterface::RAML_ID] = uniqid();
207+
}
208+
$props = $this->generator->types[$this->generator->objectProps[RamlInterface::RAML_ATTRS]][RamlInterface::RAML_PROPS];
209+
foreach ($props as $attrKey => $attrVal) {
210+
$this->attributesState[$attrKey] = $this->getAttributeValue($attrVal);
211+
}
212+
}
213+
161214
/**
162215
* @param array $attrVal
163216
* @return mixed

src/types/TestsInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ interface TestsInterface
1818
public const SEND_PATCH = 'sendPATCH';
1919
public const SEND_DELETE = 'sendDELETE';
2020

21-
public const SEE_RESP_IS_JSON = 'seeResponseIsJson';
22-
public const SEE_RESP_CONTAINS = 'seeResponseContains';
21+
public const SEE_RESP_IS_JSON = 'seeResponseIsJson';
22+
public const SEE_RESP_CONTAINS_JSON = 'seeResponseContainsJson';
2323
}

0 commit comments

Comments
 (0)