Skip to content

Commit

Permalink
feat(command): add definition loader filter on operation id option
Browse files Browse the repository at this point in the history
  • Loading branch information
sidux committed Feb 27, 2024
1 parent ff40b42 commit ce8fedc
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/Command/ExecutePlanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ protected function configure(): void
InputOption::VALUE_OPTIONAL,
'Partition tests into groups and run only one of them, ex: --part=1/3'
)
->addOption(
'operation-id',
null,
InputOption::VALUE_OPTIONAL,
'takes an operation-id to load from api definition'
)
;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Definition/Example/OperationExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public function setForceRandom(bool $forceRandom = true): self
return $this;
}

public function authenticate(Tokens $tokens, bool $ignoreScope = false): self
public function setAuthenticationHeaders(Tokens $tokens, bool $ignoreScope = false): self
{
$operation = $this->getParent();
if ($operation === null) {
Expand Down
10 changes: 9 additions & 1 deletion src/Definition/Loader/DefinitionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@

interface DefinitionLoader
{
public const FORMAT_JSON = 'json';

public const FORMAT_YAML = 'yaml';

public const FORMATS = [self::FORMAT_JSON, self::FORMAT_YAML];

public static function getFormat(): string;

public function setLogger(LoggerInterface $logger): void;

/**
* @param array<string, string[]> $filters
*
* @throws DefinitionLoadingException
*/
public function load(string $filePath): Api;
public function load(string $filePath, string $format = self::FORMAT_YAML, array $filters = []): Api;
}
26 changes: 14 additions & 12 deletions src/Definition/Loader/OpenApiDefinitionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,14 @@

final class OpenApiDefinitionLoader implements DefinitionLoader
{
public const FORMAT_JSON = 'json';

public const FORMAT_YAML = 'yaml';

public const FORMATS = [self::FORMAT_JSON, self::FORMAT_YAML];

private LoggerInterface $logger;

public function __construct(?LoggerInterface $logger = null)
{
$this->logger = $logger ?? new NullLogger();
}

/**
* @throws DefinitionLoadingException
*/
public function load(string $filePath, string $format = self::FORMAT_YAML): Api
public function load(string $filePath, string $format = self::FORMAT_YAML, array $filters = []): Api
{
$api = Api::create();
if (!\in_array($format, self::FORMATS, true)) {
Expand All @@ -81,7 +72,13 @@ public function load(string $filePath, string $format = self::FORMAT_YAML): Api
$securitySchemes = $openApi->components !== null ? $openApi->components->securitySchemes : [];

return $api
->setOperations($this->getOperations($openApi->paths->getPaths(), $securitySchemes))
->setOperations(
$this->getOperations(
$openApi->paths->getPaths(),
$securitySchemes,
$filters
)
)
->setServers($this->getServers($openApi->servers))
->setTags($this->getTags($openApi->tags))
;
Expand All @@ -98,16 +95,21 @@ public function setLogger(LoggerInterface $logger): void
}

/**
* @param array<string, string[]> $filters
* @param array<string, SecurityScheme> $securitySchemes
* @param array<string, PathItem> $paths
*
* @throws DefinitionLoadingException
*/
private function getOperations(array $paths, array $securitySchemes): Operations
private function getOperations(array $paths, array $securitySchemes, array $filters = []): Operations
{
$operations = new Operations();
foreach ($paths as $path => $pathInfo) {
foreach ($pathInfo->getOperations() as $method => $operation) {
if (isset($filters['operationId'])
&& !in_array($operation->operationId, $filters['operationId'], true)) {
continue;
}
/** @var \cebe\openapi\spec\Parameter[] $parameters */
$parameters = array_merge($operation->parameters ?? [], $pathInfo->parameters ?? []);
/** @var RequestBody $requestBody */
Expand Down
4 changes: 2 additions & 2 deletions src/Preparator/SecurityErrorPreparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private function prepareTestCases(Security $security): iterable
$testCases->add(
$this->buildTestCase(
OperationExample::create($this->getTestCaseName(), $operation)
->authenticate(new Tokens([$token]), true)
->setAuthenticationHeaders(new Tokens([$token]), true)
->setResponse(ResponseExample::create()->setStatusCode($this->getStatusCode())),
false,
),
Expand All @@ -65,7 +65,7 @@ private function prepareTestCases(Security $security): iterable
$testCases->add(
$this->buildTestCase(
OperationExample::create($this->getTestCaseName(), $operation)
->authenticate(new Tokens([$token]), true)
->setAuthenticationHeaders(new Tokens([$token]), true)
->setResponse(ResponseExample::create()->setStatusCode($this->getStatusCode())),
false,
),
Expand Down
2 changes: 1 addition & 1 deletion src/Preparator/TestCasesPreparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final public function buildTestCase(
$operation = $example->getParent();

if ($auth) {
$example->authenticate($this->tokens);
$example->setAuthenticationHeaders($this->tokens);
}

return new TestCase(
Expand Down
23 changes: 18 additions & 5 deletions src/Test/Plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function execute(
if (!empty($options['set-baseline'])) {
$this->resetBaseLine($suiteConfig);
}
$testSuite = $this->prepareSuite($suiteConfig);
$testSuite = $this->prepareSuite($suiteConfig, $options);
if (!empty($options['ignore-baseline'])) {
$testSuite->setIgnoreBaseLine(true);
}
Expand Down Expand Up @@ -185,6 +185,8 @@ private function resetBaseLine(Config\Suite $suiteConfig): void
}

/**
* @param array<string, mixed> $options
*
* @throws AuthenticationException
* @throws AuthenticationLoadingException
* @throws DefinitionLoaderNotFoundException
Expand All @@ -194,14 +196,14 @@ private function resetBaseLine(Config\Suite $suiteConfig): void
*
* @return Suite<\PHPUnit\Framework\TestCase, HttpKernelInterface>
*/
private function prepareSuite(Config\Suite $suiteConfig): Suite
private function prepareSuite(Config\Suite $suiteConfig, array $options = []): Suite
{
$testCaseClass = Object_::validateClass(
$suiteConfig->getTestCaseClass(),
\PHPUnit\Framework\TestCase::class
);
$kernel = $this->loadSymfonyKernel($suiteConfig, $testCaseClass);
$definition = $this->loadApiDefinition($suiteConfig);
$definition = $this->loadApiDefinition($suiteConfig, $options);
$requester = $this->loadRequester(
$suiteConfig->getRequester(),
$suiteConfig->getBaseUrl() ?? $definition->getUrl(),
Expand Down Expand Up @@ -295,19 +297,30 @@ private function loadSymfonyKernel(Config\Suite $suiteConfig, string $testCaseCl
}

/**
* @param array<string, mixed> $options
*
* @throws DefinitionLoaderNotFoundException
* @throws DefinitionLoadingException
*/
private function loadApiDefinition(Config\Suite $config): Api
private function loadApiDefinition(Config\Suite $config, array $options = []): Api
{
$definitionLoader = $this->getConfiguredLoader(
$config->getDefinition()
->getFormat()
);

$filters = [];
if (isset($options['operation-id'])) {
$filters['operationId'] = array_map(
'trim',
explode(',', (string) $options['operation-id'])
);
}

return $definitionLoader->load(
$config->getDefinition()
->getPath()
->getPath(),
filters: $filters,
);
}

Expand Down

0 comments on commit ce8fedc

Please sign in to comment.