Skip to content

Commit

Permalink
feat(api tester): use filters to exclude openapi endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzov committed Nov 28, 2024
1 parent 699dcda commit 989516b
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 44 deletions.
14 changes: 14 additions & 0 deletions src/Config/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ public function includes(Filterable $object): bool
return $include;
}

public function excludes(Filterable $object): bool
{
foreach ($this->getExclude() as $item) {
foreach ($item as $key => $value) {
[$operator, $value] = $this->handleTags($value);
if ($object->has($key, $value, $operator)) {
return true;
}
}
}

return false;
}

/**
* @return array{'exclude': ?array<int, array<string, string>>}
*/
Expand Down
9 changes: 0 additions & 9 deletions src/Preparator/Config/Error400BadFormatsPreparatorConfig.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Preparator/Config/Error400BadTypesPreparatorConfig.php

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions src/Preparator/Config/Error400PreparatorConfig.php

This file was deleted.

5 changes: 5 additions & 0 deletions src/Preparator/Config/PreparatorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace APITester\Preparator\Config;

use APITester\Config\Filters;

class PreparatorConfig
{
/**
Expand All @@ -15,8 +17,11 @@ class PreparatorConfig

public ResponseConfig $response;

public Filters $filters;

public function __construct()
{
$this->response = new ResponseConfig();
$this->filters = new Filters();
}
}
8 changes: 2 additions & 6 deletions src/Preparator/Error400Preparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
use APITester\Definition\Example\ResponseExample;
use APITester\Definition\Operation;
use APITester\Definition\Parameter;
use APITester\Preparator\Config\Error400PreparatorConfig;
use APITester\Test\TestCase;

/**
* @property Error400PreparatorConfig $config
*/
abstract class Error400Preparator extends TestCasesPreparator
{
/**
Expand Down Expand Up @@ -122,10 +118,10 @@ protected function getStatusCode(): string
*/
private function prepareTestCases(Operation $operation): array
{
if ($this->config->excludeOpenApiEndpoints
&& isset($operation->getExtensions()['x-usecase'])) {
if ($this->config->filters->excludes($operation)) {
return [];
}

$requiredParams = $operation->getParameters(true);

return array_merge(
Expand Down
7 changes: 7 additions & 0 deletions src/Util/Traits/FilterableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ trait FilterableTrait
public function has(string $prop, $value, string $operator = '='): bool
{
$self = collect([$this]);

$object = $self->whereNotNull($prop)
->first();
if (is_array($object->{$prop}) && array_key_exists((string) $value, $object->{$prop})) {
return true;
}

if (str_contains($prop, '*')) {
$operator = 'contains';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ public function getData(): iterable
[
],
[
'excludeOpenApiEndpoints' => true,
'filters' => [
'exclude' => [[
'extensions' => 'x-usecase',
]],
],
],
];
}
Expand Down

0 comments on commit 989516b

Please sign in to comment.