Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(examples-prepartor): exclude array bodies from auto complete #63

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
14 changes: 13 additions & 1 deletion src/Definition/Example/OperationExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ public function setName(string $name): self
return $this;
}

public function withName(string $name): self
{
/** @var OperationExample $clone */
$clone = $this->deepCopy->copy($this);
$clone->name = $name;

return $clone;
}

/**
* @return array<string, int|string>
*/
Expand Down Expand Up @@ -324,7 +333,10 @@ public function getBody(): ?BodyExample
if ($this->autoComplete) {
$randomBody = BodyExample::create($requestBody->getRandomContent());
if ($this->body !== null) {
$this->body->setContent(array_merge($randomBody->getContent(), $this->body->getContent()));
$content = $this->body->getContent();
if (!isset($content[0])) {
$this->body->setContent(array_merge($randomBody->getContent(), $content));
}
} else {
$this->body = $randomBody;
}
Expand Down
24 changes: 17 additions & 7 deletions src/Preparator/Error404Preparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace APITester\Preparator;

use APITester\Definition\Collection\Operations;
use APITester\Definition\Example\OperationExample;
use APITester\Definition\Example\ResponseExample;
use APITester\Definition\Response as DefinitionResponse;
use APITester\Test\TestCase;

final class Error404Preparator extends TestCasesPreparator
{
public const INT32_MAX = 2147483647;

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -40,13 +41,21 @@ private function prepareTestCase(DefinitionResponse $response): array

$testcases = [];

$pathParameters = array_map(
static fn ($parameter) => $parameter->getName(),
$operation->getPathParameters()
->toArray()
);
$pathParameters = array_fill_keys(array_values($pathParameters), self::INT32_MAX);

if ($operation->getRequestBodies()->count() === 0) {
$testcases[] = $this->buildTestCase(
OperationExample::create('RandomPath', $operation)
->setForceRandom()
$operation->getExample()
->withName('RandomPath')
->setPathParameters($pathParameters)
->setResponse(
ResponseExample::create()
->setStatusCode($this->config->response->getStatusCode() ?? '404')
->setStatusCode('404')
->setHeaders($this->config->response->headers ?? [])
->setContent($this->config->response->body ?? $response->getDescription())
)
Expand All @@ -55,11 +64,12 @@ private function prepareTestCase(DefinitionResponse $response): array

foreach ($operation->getRequestBodies() as $ignored) {
$testcases[] = $this->buildTestCase(
OperationExample::create('RandomPath', $operation)
->setForceRandom()
$operation->getExample()
->withName('RandomPath')
->setPathParameters($pathParameters)
->setResponse(
ResponseExample::create()
->setStatusCode($this->config->response->getStatusCode() ?? '404')
->setStatusCode('404')
->setHeaders($this->config->response->headers ?? [])
->setContent($this->config->response->body ?? $response->getDescription())
)
Expand Down
Loading