Skip to content

Commit

Permalink
Support predicates array for AND, OR, NOT
Browse files Browse the repository at this point in the history
  • Loading branch information
demyan112rv committed Jul 20, 2024
1 parent 8522bee commit c7cdf85
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,39 @@ private function responseToArray(Response $response): array
/**
* @return array<string, mixed>
*/
private function predicateToArray(Predicate $predicate): array
private function predicateToArray(Predicate $predicate, bool $withAdditionalFields = true): array
{
$operator = $predicate->getOperator();
$value = $operator === Predicate::OPERATOR_INJECT ? $predicate->getInjectJs() : $predicate->getConfig();

switch ($operator) {
case Predicate::OPERATOR_INJECT:
$value = $predicate->getInjectJs();
break;
case Predicate::OPERATOR_AND:
case Predicate::OPERATOR_OR:
case Predicate::OPERATOR_NOT:
$value = [];
foreach ($predicate->getConfig() as $predicateOperator => $predicateConfig) {
if ($predicateConfig instanceof Predicate) {
$value = array_merge($value, $this->predicateToArray($predicateConfig, false));
} else {
$value[$predicateOperator] = $predicateConfig;
}
}
break;
default:
$value = $predicate->getConfig();
}

$array = [
$operator => $value,
'caseSensitive' => $predicate->isCaseSensitive(),
'except' => $predicate->getExcept(),
];

if (true === $withAdditionalFields) {
$array['caseSensitive'] = $predicate->isCaseSensitive();
$array['except'] = $predicate->getExcept();
}

if ($predicate->getXPath() !== null) {
$array['xpath'] = [
'selector' => $predicate->getXPath()->getSelector(),
Expand Down
76 changes: 76 additions & 0 deletions tests/Unit/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,80 @@ public function testSchemaAttached(): void

$this->assertArrayHasKey('schema', $array);
}

public function testPredicateWithPredicates(): void
{
$predicate1 = new Predicate(Predicate::OPERATOR_EQUALS);
$predicate1->setConfig(['path' => '/test1']);

$predicate2 = new Predicate(Predicate::OPERATOR_START_WITH);
$predicate2->setConfig(['path' => '/test2']);

$predicate = new Predicate(Predicate::OPERATOR_AND);
$predicate->setConfig([
$predicate1->getOperator() => $predicate1,
$predicate2->getOperator() => $predicate2,
]);

$stub = new Stub();
$stub->addPredicate($predicate);

$imposter = new Imposter('Test imposter', 1234, Imposter::PROTOCOL_HTTPS);
$imposter->addStub($stub);

$formatter = new Formatter();
$array = $formatter->imposterToArray($imposter);
$this->assertNotEmpty($array);
$this->assertArrayHasKey('stubs', $array);
$this->assertArrayHasKey('predicates', $array['stubs'][0]);
$this->assertSame([
[
'and' => [
'equals' => [
'path' => '/test1',
],
'startsWith' => [
'path' => '/test2',
],
],
'caseSensitive' => false,
'except' => '',
],
], $array['stubs'][0]['predicates']);
}

public function testPredicateWithPredicatesAsArray(): void
{
$predicate = new Predicate(Predicate::OPERATOR_AND);
$predicate->setConfig([
Predicate::OPERATOR_EQUALS => ['path' => '/test1'],
Predicate::OPERATOR_START_WITH => ['path' => '/test2'],
]);

$stub = new Stub();
$stub->addPredicate($predicate);

$imposter = new Imposter('Test imposter', 1234, Imposter::PROTOCOL_HTTPS);
$imposter->addStub($stub);

$formatter = new Formatter();
$array = $formatter->imposterToArray($imposter);
$this->assertNotEmpty($array);
$this->assertArrayHasKey('stubs', $array);
$this->assertArrayHasKey('predicates', $array['stubs'][0]);
$this->assertSame([
[
'and' => [
'equals' => [
'path' => '/test1',
],
'startsWith' => [
'path' => '/test2',
],
],
'caseSensitive' => false,
'except' => '',
],
], $array['stubs'][0]['predicates']);
}
}

0 comments on commit c7cdf85

Please sign in to comment.