Skip to content

Commit

Permalink
feat(api tester): use IN tag to filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzov committed Nov 29, 2024
1 parent 989516b commit 101e337
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 80 deletions.
22 changes: 5 additions & 17 deletions src/Config/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,6 @@ 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 All @@ -174,9 +160,11 @@ private function handleTags(string|int|TaggedValue $value): array
$operator = '=';

if ($value instanceof TaggedValue) {
if ($value->getTag() === 'NOT') {
$operator = '!=';
}
match ($value->getTag()) {
'NOT' => $operator = '!=',
'IN' => $operator = 'contains',
default => $operator,
};
$value = (string) $value->getValue();
}

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

namespace APITester\Preparator\Config;

use APITester\Config\Filters;

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

public ResponseConfig $response;

public Filters $filters;

public function __construct()
{
$this->response = new ResponseConfig();
$this->filters = new Filters();
}
}
4 changes: 0 additions & 4 deletions src/Preparator/Error400Preparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ protected function getStatusCode(): string
*/
private function prepareTestCases(Operation $operation): array
{
if ($this->config->filters->excludes($operation)) {
return [];
}

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

return array_merge(
Expand Down
6 changes: 0 additions & 6 deletions src/Util/Traits/FilterableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ 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
49 changes: 1 addition & 48 deletions tests/Preparator/Error400MissingRequiredFieldsPreparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class Error400MissingRequiredFieldsPreparatorTest extends \PHPUnit\Framewo
* @dataProvider getData
*
* @param array<string, array<mixed>> $config
* @param TestCase[] $expected
* @param TestCase[] $expected
*/
public function test(Api $api, array $expected, array $config = []): void
{
Expand Down Expand Up @@ -165,52 +165,5 @@ public function getData(): iterable
),
],
];

yield 'openapi endpoint is ignored' => [
Api::create()
->addOperation(
Operation::create(
'test',
'/test',
'POST'
)
->addRequestBody(
(new Body(
new Schema([
'type' => 'object',
'properties' => [
'foo' => [
'type' => 'string',
],
'bar' => [
'type' => 'string',
],
],
'required' => ['foo'],
]),
'application/json'
))
)
->addExample(
OperationExample::create('foo')
->setBodyContent([
'foo' => 'foo_body1',
'bar' => 'bar_body1',
])
->setQueryParameter('foo_query', 'foo1')
)->setExtensions([
'x-usecase' => 'UseCaseName',
])
),
[
],
[
'filters' => [
'exclude' => [[
'extensions' => 'x-usecase',
]],
],
],
];
}
}

0 comments on commit 101e337

Please sign in to comment.