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: Support fromProviderState generator in matching expression #679

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/PhpPact/Consumer/Matcher/Generators/ProviderState.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
namespace PhpPact\Consumer\Matcher\Generators;

use PhpPact\Consumer\Matcher\Model\Attributes;
use PhpPact\Consumer\Matcher\Model\Expression;
use PhpPact\Consumer\Matcher\Model\Generator\ExpressionFormattableInterface;
use PhpPact\Consumer\Matcher\Model\Generator\JsonFormattableInterface;

/**
* Generates a value that is looked up from the provider state context using the given expression
*
* Example expression: /products/${id}
*/
class ProviderState extends AbstractGenerator implements JsonFormattableInterface
class ProviderState extends AbstractGenerator implements JsonFormattableInterface, ExpressionFormattableInterface
{
public function __construct(private string $expression)
{
Expand All @@ -23,4 +25,9 @@ public function formatJson(): Attributes
'expression' => $this->expression,
]);
}

public function formatExpression(): Expression
{
return new Expression('fromProviderState(%expression%, %value%)', ['expression' => $this->expression]);
}
}
6 changes: 4 additions & 2 deletions src/PhpPact/Consumer/Matcher/Matchers/AbstractDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use PhpPact\Consumer\Matcher\Model\Expression;
use PhpPact\Consumer\Matcher\Model\Matcher\ExpressionFormattableInterface;
use PhpPact\Consumer\Matcher\Model\Matcher\JsonFormattableInterface;
use PhpPact\Consumer\Matcher\Trait\ExpressionFormattableTrait;
use PhpPact\Consumer\Matcher\Trait\JsonFormattableTrait;

abstract class AbstractDateTime extends GeneratorAwareMatcher implements JsonFormattableInterface, ExpressionFormattableInterface
{
use JsonFormattableTrait;
use ExpressionFormattableTrait;

public function __construct(protected string $format, private ?string $value = null)
{
Expand All @@ -35,13 +37,13 @@ public function formatExpression(): Expression

$type = $this->getType();

return new Expression(
return $this->mergeExpression(new Expression(
"matching({$type}, %format%, %value%)",
[
'format' => $this->format,
'value' => $this->value,
],
);
));
}

abstract protected function getType(): string;
Expand Down
4 changes: 3 additions & 1 deletion src/PhpPact/Consumer/Matcher/Matchers/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpPact\Consumer\Matcher\Model\Expression;
use PhpPact\Consumer\Matcher\Model\Matcher\ExpressionFormattableInterface;
use PhpPact\Consumer\Matcher\Model\Matcher\JsonFormattableInterface;
use PhpPact\Consumer\Matcher\Trait\ExpressionFormattableTrait;
use PhpPact\Consumer\Matcher\Trait\JsonFormattableTrait;

/**
Expand All @@ -16,6 +17,7 @@
class Decimal extends GeneratorAwareMatcher implements JsonFormattableInterface, ExpressionFormattableInterface
{
use JsonFormattableTrait;
use ExpressionFormattableTrait;

public function __construct(private ?float $value = null)
{
Expand All @@ -38,6 +40,6 @@ public function formatExpression(): Expression
if (!is_float($this->value)) {
throw new InvalidValueException(sprintf("Decimal matching expression doesn't support value of type %s", gettype($this->value)));
}
return new Expression('matching(decimal, %value%)', ['value' => $this->value]);
return $this->mergeExpression(new Expression('matching(decimal, %value%)', ['value' => $this->value]));
}
}
4 changes: 3 additions & 1 deletion src/PhpPact/Consumer/Matcher/Matchers/Equality.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpPact\Consumer\Matcher\Model\Expression;
use PhpPact\Consumer\Matcher\Model\Matcher\ExpressionFormattableInterface;
use PhpPact\Consumer\Matcher\Model\Matcher\JsonFormattableInterface;
use PhpPact\Consumer\Matcher\Trait\ExpressionFormattableTrait;
use PhpPact\Consumer\Matcher\Trait\JsonFormattableTrait;

/**
Expand All @@ -14,6 +15,7 @@
class Equality extends GeneratorAwareMatcher implements JsonFormattableInterface, ExpressionFormattableInterface
{
use JsonFormattableTrait;
use ExpressionFormattableTrait;

public function __construct(private mixed $value)
{
Expand All @@ -30,6 +32,6 @@ public function formatJson(): Attributes

public function formatExpression(): Expression
{
return new Expression('matching(equalTo, %value%)', ['value' => $this->value]);
return $this->mergeExpression(new Expression('matching(equalTo, %value%)', ['value' => $this->value]));
}
}
4 changes: 3 additions & 1 deletion src/PhpPact/Consumer/Matcher/Matchers/Integer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpPact\Consumer\Matcher\Model\Expression;
use PhpPact\Consumer\Matcher\Model\Matcher\ExpressionFormattableInterface;
use PhpPact\Consumer\Matcher\Model\Matcher\JsonFormattableInterface;
use PhpPact\Consumer\Matcher\Trait\ExpressionFormattableTrait;
use PhpPact\Consumer\Matcher\Trait\JsonFormattableTrait;

/**
Expand All @@ -16,6 +17,7 @@
class Integer extends GeneratorAwareMatcher implements JsonFormattableInterface, ExpressionFormattableInterface
{
use JsonFormattableTrait;
use ExpressionFormattableTrait;

public function __construct(private ?int $value = null)
{
Expand All @@ -38,6 +40,6 @@ public function formatExpression(): Expression
if (!is_int($this->value)) {
throw new InvalidValueException(sprintf("Integer matching expression doesn't support value of type %s", gettype($this->value)));
}
return new Expression('matching(integer, %value%)', ['value' => $this->value]);
return $this->mergeExpression(new Expression('matching(integer, %value%)', ['value' => $this->value]));
}
}
4 changes: 3 additions & 1 deletion src/PhpPact/Consumer/Matcher/Matchers/NotEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpPact\Consumer\Matcher\Model\Expression;
use PhpPact\Consumer\Matcher\Model\Matcher\ExpressionFormattableInterface;
use PhpPact\Consumer\Matcher\Model\Matcher\JsonFormattableInterface;
use PhpPact\Consumer\Matcher\Trait\ExpressionFormattableTrait;
use PhpPact\Consumer\Matcher\Trait\JsonFormattableTrait;

/**
Expand All @@ -14,6 +15,7 @@
class NotEmpty extends GeneratorAwareMatcher implements JsonFormattableInterface, ExpressionFormattableInterface
{
use JsonFormattableTrait;
use ExpressionFormattableTrait;

public function __construct(private mixed $value)
{
Expand All @@ -30,6 +32,6 @@ public function formatJson(): Attributes

public function formatExpression(): Expression
{
return new Expression('notEmpty(%value%)', ['value' => $this->value]);
return $this->mergeExpression(new Expression('notEmpty(%value%)', ['value' => $this->value]));
}
}
4 changes: 3 additions & 1 deletion src/PhpPact/Consumer/Matcher/Matchers/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpPact\Consumer\Matcher\Model\Expression;
use PhpPact\Consumer\Matcher\Model\Matcher\ExpressionFormattableInterface;
use PhpPact\Consumer\Matcher\Model\Matcher\JsonFormattableInterface;
use PhpPact\Consumer\Matcher\Trait\ExpressionFormattableTrait;
use PhpPact\Consumer\Matcher\Trait\JsonFormattableTrait;

/**
Expand All @@ -16,6 +17,7 @@
class Number extends GeneratorAwareMatcher implements JsonFormattableInterface, ExpressionFormattableInterface
{
use JsonFormattableTrait;
use ExpressionFormattableTrait;

public function __construct(private int|float|null $value = null)
{
Expand All @@ -38,6 +40,6 @@ public function formatExpression(): Expression
if (null === $this->value) {
throw new InvalidValueException(sprintf("Number matching expression doesn't support value of type %s", gettype($this->value)));
}
return new Expression('matching(number, %value%)', ['value' => $this->value]);
return $this->mergeExpression(new Expression('matching(number, %value%)', ['value' => $this->value]));
}
}
4 changes: 3 additions & 1 deletion src/PhpPact/Consumer/Matcher/Matchers/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpPact\Consumer\Matcher\Model\Expression;
use PhpPact\Consumer\Matcher\Model\Matcher\ExpressionFormattableInterface;
use PhpPact\Consumer\Matcher\Model\Matcher\JsonFormattableInterface;
use PhpPact\Consumer\Matcher\Trait\ExpressionFormattableTrait;
use PhpPact\Consumer\Matcher\Trait\JsonFormattableTrait;

/**
Expand All @@ -14,6 +15,7 @@
class Type extends GeneratorAwareMatcher implements JsonFormattableInterface, ExpressionFormattableInterface
{
use JsonFormattableTrait;
use ExpressionFormattableTrait;

public function __construct(private mixed $value)
{
Expand All @@ -30,6 +32,6 @@ public function formatJson(): Attributes

public function formatExpression(): Expression
{
return new Expression('matching(type, %value%)', ['value' => $this->value]);
return $this->mergeExpression(new Expression('matching(type, %value%)', ['value' => $this->value]));
}
}
13 changes: 13 additions & 0 deletions src/PhpPact/Consumer/Matcher/Model/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ public function __construct(private string $format, private array $values = [])
$this->setValues($values);
}

public function getFormat(): string
{
return $this->format;
}

/**
* @return array<string, mixed>
*/
public function getValues(): array
{
return $this->values;
}

public function jsonSerialize(): string
{
return $this->format();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace PhpPact\Consumer\Matcher\Model\Generator;

use PhpPact\Consumer\Matcher\Model\Expression;

interface ExpressionFormattableInterface
{
public function formatExpression(): Expression;
}
29 changes: 29 additions & 0 deletions src/PhpPact/Consumer/Matcher/Trait/ExpressionFormattableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace PhpPact\Consumer\Matcher\Trait;

use PhpPact\Consumer\Matcher\Model\Expression;
use PhpPact\Consumer\Matcher\Model\GeneratorAwareInterface;
use PhpPact\Consumer\Matcher\Model\Generator\ExpressionFormattableInterface as GeneratorExpressionFormattableInterface;

trait ExpressionFormattableTrait
{
public function mergeExpression(Expression $expression): Expression
{
if ($this instanceof GeneratorAwareInterface) {
$generator = $this->getGenerator();
if ($generator instanceof GeneratorExpressionFormattableInterface) {
$generated = $generator->formatExpression();

return new Expression(
str_replace('%value%', $generated->getFormat(), $expression->getFormat()),
[
...$expression->getValues(),
...$generated->getValues(),
]
);
}
}
return $expression;
}
}
12 changes: 12 additions & 0 deletions tests/PhpPact/Consumer/Matcher/Matchers/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use PhpPact\Consumer\Matcher\Exception\InvalidValueException;
use PhpPact\Consumer\Matcher\Formatters\Expression\ExpressionFormatter;
use PhpPact\Consumer\Matcher\Generators\ProviderState;
use PhpPact\Consumer\Matcher\Matchers\Date;
use PhpPact\Consumer\Matcher\Model\GeneratorInterface;
use PhpPact\Consumer\Matcher\Model\MatcherInterface;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -36,4 +38,14 @@ public function testFormatExpression(MatcherInterface $matcher, string $expressi
$matcher = $matcher->withFormatter(new ExpressionFormatter());
$this->assertSame($expression, json_encode($matcher));
}

#[TestWith([new Date("contains single quote '", '2012-04-12'), new ProviderState('${value}'), '"matching(date, \'contains single quote \\\\\'\', fromProviderState(\'${value}\', \'2012-04-12\'))"'])]
#[TestWith([new Date('yyyy-MM-dd', "contains single quote '"), new ProviderState('${value}'), '"matching(date, \'yyyy-MM-dd\', fromProviderState(\'${value}\', \'contains single quote \\\\\'\'))"'])]
#[TestWith([new Date('yyyy-MM-dd', '2012-04-12'), new ProviderState('${value}'), '"matching(date, \'yyyy-MM-dd\', fromProviderState(\'${value}\', \'2012-04-12\'))"'])]
public function testFormatExpressionWithGenerator(Date $matcher, GeneratorInterface $generator, string $expression): void
{
$matcher = $matcher->withFormatter(new ExpressionFormatter());
$matcher = $matcher->withGenerator($generator);
$this->assertSame($expression, json_encode($matcher));
}
}
12 changes: 12 additions & 0 deletions tests/PhpPact/Consumer/Matcher/Matchers/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use PhpPact\Consumer\Matcher\Exception\InvalidValueException;
use PhpPact\Consumer\Matcher\Formatters\Expression\ExpressionFormatter;
use PhpPact\Consumer\Matcher\Generators\ProviderState;
use PhpPact\Consumer\Matcher\Matchers\DateTime;
use PhpPact\Consumer\Matcher\Model\GeneratorInterface;
use PhpPact\Consumer\Matcher\Model\MatcherInterface;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -37,4 +39,14 @@ public function testFormatExpression(MatcherInterface $matcher, string $expressi
$matcher = $matcher->withFormatter(new ExpressionFormatter());
$this->assertSame($expression, json_encode($matcher));
}

#[TestWith([new DateTime("contains single quote '", '2020-05-21 16:44:32+10:00'), new ProviderState('${value}'), '"matching(datetime, \'contains single quote \\\\\'\', fromProviderState(\'${value}\', \'2020-05-21 16:44:32+10:00\'))"'])]
#[TestWith([new DateTime('yyyy-MM-dd HH:mm:ssZZZZZ', "contains single quote '"), new ProviderState('${value}'), '"matching(datetime, \'yyyy-MM-dd HH:mm:ssZZZZZ\', fromProviderState(\'${value}\', \'contains single quote \\\\\'\'))"'])]
#[TestWith([new DateTime('yyyy-MM-dd HH:mm:ssZZZZZ', '2020-05-21 16:44:32+10:00'), new ProviderState('${value}'), '"matching(datetime, \'yyyy-MM-dd HH:mm:ssZZZZZ\', fromProviderState(\'${value}\', \'2020-05-21 16:44:32+10:00\'))"'])]
public function testFormatExpressionWithGenerator(DateTime $matcher, GeneratorInterface $generator, string $expression): void
{
$matcher = $matcher->withFormatter(new ExpressionFormatter());
$matcher = $matcher->withGenerator($generator);
$this->assertSame($expression, json_encode($matcher));
}
}
13 changes: 13 additions & 0 deletions tests/PhpPact/Consumer/Matcher/Matchers/DecimalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use PhpPact\Consumer\Matcher\Exception\InvalidValueException;
use PhpPact\Consumer\Matcher\Formatters\Expression\ExpressionFormatter;
use PhpPact\Consumer\Matcher\Generators\ProviderState;
use PhpPact\Consumer\Matcher\Matchers\Decimal;
use PhpPact\Consumer\Matcher\Model\GeneratorInterface;
use PhpPact\Consumer\Matcher\Model\MatcherInterface;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -37,4 +39,15 @@ public function testFormatExpression(MatcherInterface $matcher, string $expressi
$matcher = $matcher->withFormatter(new ExpressionFormatter());
$this->assertSame($expression, json_encode($matcher));
}

#[TestWith([new Decimal(-99), new ProviderState('${value}'), '"matching(decimal, fromProviderState(\'${value}\', -99))"'])] // Provider verification will fail on this case
#[TestWith([new Decimal(100), new ProviderState('${value}'), '"matching(decimal, fromProviderState(\'${value}\', 100))"'])] // Provider verification will fail on this case
#[TestWith([new Decimal(100.01), new ProviderState('${value}'), '"matching(decimal, fromProviderState(\'${value}\', 100.01))"'])]
#[TestWith([new Decimal(-100.003), new ProviderState('${value}'), '"matching(decimal, fromProviderState(\'${value}\', -100.003))"'])]
public function testFormatExpressionWithGenerator(Decimal $matcher, GeneratorInterface $generator, string $expression): void
{
$matcher = $matcher->withFormatter(new ExpressionFormatter());
$matcher = $matcher->withGenerator($generator);
$this->assertSame($expression, json_encode($matcher));
}
}
17 changes: 17 additions & 0 deletions tests/PhpPact/Consumer/Matcher/Matchers/EqualityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpPact\Consumer\Matcher\Exception\InvalidValueException;
use PhpPact\Consumer\Matcher\Formatters\Expression\ExpressionFormatter;
use PhpPact\Consumer\Matcher\Generators\ProviderState;
use PhpPact\Consumer\Matcher\Generators\RandomString;
use PhpPact\Consumer\Matcher\Matchers\Equality;
use PhpPact\Consumer\Matcher\Model\GeneratorInterface;
Expand Down Expand Up @@ -47,4 +48,20 @@ public function testFormatExpression(MatcherInterface $matcher, string $expressi
$matcher = $matcher->withFormatter(new ExpressionFormatter());
$this->assertSame($expression, json_encode($matcher));
}

#[TestWith([new Equality("contains single quote '"), new ProviderState('${value}'), '"matching(equalTo, fromProviderState(\'${value}\', \'contains single quote \\\\\'\'))"'])]
#[TestWith([new Equality('example value'), new ProviderState('${value}'), '"matching(equalTo, fromProviderState(\'${value}\', \'example value\'))"'])]
#[TestWith([new Equality(100.09), new ProviderState('${value}'), '"matching(equalTo, fromProviderState(\'${value}\', 100.09))"'])]
#[TestWith([new Equality(-99.99), new ProviderState('${value}'), '"matching(equalTo, fromProviderState(\'${value}\', -99.99))"'])]
#[TestWith([new Equality(100), new ProviderState('${value}'), '"matching(equalTo, fromProviderState(\'${value}\', 100))"'])]
#[TestWith([new Equality(-99), new ProviderState('${value}'), '"matching(equalTo, fromProviderState(\'${value}\', -99))"'])]
#[TestWith([new Equality(true), new ProviderState('${value}'), '"matching(equalTo, fromProviderState(\'${value}\', true))"'])]
#[TestWith([new Equality(false), new ProviderState('${value}'), '"matching(equalTo, fromProviderState(\'${value}\', false))"'])]
#[TestWith([new Equality(null), new ProviderState('${value}'), '"matching(equalTo, fromProviderState(\'${value}\', null))"'])]
public function testFormatExpressionWithGenerator(Equality $matcher, GeneratorInterface $generator, string $expression): void
{
$matcher = $matcher->withFormatter(new ExpressionFormatter());
$matcher = $matcher->withGenerator($generator);
$this->assertSame($expression, json_encode($matcher));
}
}
11 changes: 11 additions & 0 deletions tests/PhpPact/Consumer/Matcher/Matchers/IntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use PhpPact\Consumer\Matcher\Exception\InvalidValueException;
use PhpPact\Consumer\Matcher\Formatters\Expression\ExpressionFormatter;
use PhpPact\Consumer\Matcher\Generators\ProviderState;
use PhpPact\Consumer\Matcher\Matchers\Integer;
use PhpPact\Consumer\Matcher\Model\GeneratorInterface;
use PhpPact\Consumer\Matcher\Model\MatcherInterface;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -36,4 +38,13 @@ public function testFormatExpression(MatcherInterface $matcher, string $expressi
$matcher = $matcher->withFormatter(new ExpressionFormatter());
$this->assertSame($expression, json_encode($matcher));
}

#[TestWith([new Integer(-99), new ProviderState('${value}'), '"matching(integer, fromProviderState(\'${value}\', -99))"'])]
#[TestWith([new Integer(100), new ProviderState('${value}'), '"matching(integer, fromProviderState(\'${value}\', 100))"'])]
public function testFormatExpressionWithGenerator(Integer $matcher, GeneratorInterface $generator, string $expression): void
{
$matcher = $matcher->withFormatter(new ExpressionFormatter());
$matcher = $matcher->withGenerator($generator);
$this->assertSame($expression, json_encode($matcher));
}
}
Loading