Skip to content

Commit

Permalink
chore: Specify type of array
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Apr 25, 2024
1 parent 6285191 commit 769a930
Show file tree
Hide file tree
Showing 28 changed files with 255 additions and 285 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class InteractionBodyDriverTest extends TestCase
private Binary $binary;
private Text $text;
private Multipart $multipart;
/**
* @var Part[]
*/
private array $parts;
private string $boundary = 'abcde12345';
private CData $failed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class InteractionDriverTest extends TestCase
private int $interactionHandle = 123;
private int $pactHandle = 234;
private string $description = 'Sending request receiving response';
/**
* @var array<string, array<string, mixed>>
*/
private array $providerStates = [
'item exist' => [
'id' => 12,
Expand Down Expand Up @@ -167,6 +170,9 @@ public function testSetPending(?bool $pending, bool $success): void
$this->driver->registerInteraction($this->interaction, false);
}

/**
* @param array<string, mixed> $comments
*/
#[TestWith([[], true])]
#[TestWith([['key1' => null], true])]
#[TestWith([['key1' => null], false])]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ class MessageDriverTest extends TestCase
private int $messageHandle = 123;
private int $pactHandle = 234;
private string $description = 'Receiving message';
/**
* @var array<string, array<string, mixed>>
*/
private array $providerStates = [
'item exist' => [
'id' => 12,
'name' => 'abc',
]
];
/**
* @var array<string, string>
*/
private array $metadata = [
'key1' => 'value1',
'key2' => 'value2',
Expand Down Expand Up @@ -164,6 +170,9 @@ public function testSetPending(?bool $pending, bool $success): void
$this->driver->registerMessage($this->message);
}

/**
* @param array<string, mixed> $comments
*/
#[TestWith([[], true])]
#[TestWith([['key1' => null], true])]
#[TestWith([['key1' => null], false])]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ class RequestDriverTest extends TestCase
private int $interactionHandle = 123;
private string $method = 'POST';
private string $path = '/items/item';
/**
* @var array<string, string[]>
*/
private array $query = [
'query1' => ['query-value-1', 'query-value-2'],
'query2' => ['query-value-3'],
];
/**
* @var array<string, string[]>
*/
private array $headers = [
'header1' => ['header-value-1'],
'header2' => ['header-value-2', 'header-value-3'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class ResponseDriverTest extends TestCase
private int $responsePartId = 2;
private int $interactionHandle = 123;
private string $status = '400';
/**
* @var array<string, string[]>
*/
private array $headers = [
'header1' => ['header-value-1'],
'header2' => ['header-value-2', 'header-value-3'],
Expand Down
3 changes: 3 additions & 0 deletions tests/PhpPact/Consumer/InteractionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ public function testSetPending(?bool $pending): void
$this->assertSame($pending, $interaction->getPending());
}

/**
* @param array<string, mixed> $comments
*/
#[TestWith([[]])]
#[TestWith([['key1' => null, 'key2' => 'value', 'key3' => ['value']]])]
public function testSetComments(array $comments): void
Expand Down
97 changes: 31 additions & 66 deletions tests/PhpPact/Consumer/Matcher/Formatters/PluginFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use PhpPact\Consumer\Matcher\Matchers\Values;
use PhpPact\Consumer\Matcher\Model\FormatterInterface;
use PhpPact\Consumer\Matcher\Model\MatcherInterface;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;

class PluginFormatterTest extends TestCase
Expand All @@ -54,95 +55,59 @@ public function testFormatWithGenerator(): void
$this->formatter->format($matcher);
}

/**
* @dataProvider invalidRulesProvider
*/
#[TestWith([new EachKey(["doesn't matter"], [])])]
#[TestWith([new EachValue(["doesn't matter"], [])])]
#[TestWith([new EachKey(["doesn't matter"], [new Type(1), new Type(2)])])]
#[TestWith([new EachValue(["doesn't matter"], [new Type(1), new Type(2), new Type(3)])])]
public function testInvalidRules(EachKey|EachValue $matcher): void
{
$this->expectException(MatchingExpressionException::class);
$this->expectExceptionMessage(sprintf("Matcher '%s' only support 1 rule, %d provided", $matcher->getType(), count($matcher->getRules())));
$this->formatter->format($matcher);
}

public static function invalidRulesProvider(): array
{
return [
[new EachKey(["doesn't matter"], [])],
[new EachValue(["doesn't matter"], [])],
[new EachKey(["doesn't matter"], [new Type(1), new Type(2)])],
[new EachValue(["doesn't matter"], [new Type(1), new Type(2), new Type(3)])],
];
}

/**
* @dataProvider invalidValueProvider
*/
#[TestWith([new Type(new \stdClass()), 'object'])]
#[TestWith([new Type(['key' => 'value']), 'array'])]
#[TestWith([new MinType(['Example value'], 1), 'array'])]
#[TestWith([new MaxType(['Example value'], 2), 'array'])]
#[TestWith([new MinMaxType(['Example value'], 1, 2), 'array'])]
public function testInvalidValue(MatcherInterface $matcher, string $type): void
{
$this->expectException(MatchingExpressionException::class);
$this->expectExceptionMessage(sprintf("Plugin formatter doesn't support value of type %s", $type));
$this->formatter->format($matcher);
}

public static function invalidValueProvider(): array
{
return [
[new Type((object)['key' => 'value']), 'object'],
[new Type(['key' => 'value']), 'array'],
[new MinType(['Example value'], 1), 'array'],
[new MaxType(['Example value'], 2), 'array'],
[new MinMaxType(['Example value'], 1, 2), 'array'],
];
}

/**
* @dataProvider notSupportedMatcherProvider
*/
#[TestWith([new Values([1, 2, 3])])]
#[TestWith([new ArrayContains([new Equality(1)])])]
#[TestWith([new StatusCode('clientError', 405)])]
public function testNotSupportedMatcher(MatcherInterface $matcher): void
{
$this->expectException(MatcherNotSupportedException::class);
$this->expectExceptionMessage(sprintf("Matcher '%s' is not supported by plugin", $matcher->getType()));
$this->formatter->format($matcher);
}

public static function notSupportedMatcherProvider(): array
{
return [
[new Values([1, 2, 3])],
[new ArrayContains([new Equality(1)])],
[new StatusCode('clientError', 405)],
];
}

/**
* @dataProvider matcherProvider
*/
#[TestWith([new MatchingField('product'), '"matching($\'product\')"'])]
#[TestWith([new NotEmpty('test'), '"notEmpty(\'test\')"'])]
#[TestWith([new EachKey(["doesn't matter"], [new Regex('\$(\.\w+)+', '$.test.one')]), '"eachKey(matching(regex, \'\\\\$(\\\\.\\\\w+)+\', \'$.test.one\'))"'])]
#[TestWith([new EachValue(["doesn't matter"], [new Type(100)]), '"eachValue(matching(type, 100))"'])]
#[TestWith([new Equality('Example value'), '"matching(equalTo, \'Example value\')"'])]
#[TestWith([new Type('Example value'), '"matching(type, \'Example value\')"'])]
#[TestWith([new Number(100.09), '"matching(number, 100.09)"'])]
#[TestWith([new Integer(100), '"matching(integer, 100)"'])]
#[TestWith([new Decimal(100.01), '"matching(decimal, 100.01)"'])]
#[TestWith([new Includes('testing'), '"matching(include, \'testing\')"'])]
#[TestWith([new Boolean(true), '"matching(boolean, true)"'])]
#[TestWith([new Semver('1.0.0'), '"matching(semver, \'1.0.0\')"'])]
#[TestWith([new DateTime('yyyy-MM-dd HH:mm:ssZZZZZ', '2020-05-21 16:44:32+10:00'), '"matching(datetime, \'yyyy-MM-dd HH:mm:ssZZZZZ\', \'2020-05-21 16:44:32+10:00\')"'])]
#[TestWith([new Date('yyyy-MM-dd', '2012-04-12'), '"matching(date, \'yyyy-MM-dd\', \'2012-04-12\')"'])]
#[TestWith([new Time('HH:mm', '22:04'), '"matching(time, \'HH:mm\', \'22:04\')"'])]
#[TestWith([new Regex('\\w{3}\\d+', 'abc123'), '"matching(regex, \'\\\\w{3}\\\\d+\', \'abc123\')"'])]
#[TestWith([new ContentType('application/xml'), '"matching(contentType, \'application\/xml\', \'application\/xml\')"'])]
#[TestWith([new NullValue(), '"matching(type, null)"'])]
public function testFormat(MatcherInterface $matcher, string $json): void
{
$this->assertSame($json, json_encode($this->formatter->format($matcher)));
}

public static function matcherProvider(): array
{
return [
[new MatchingField('product'), '"matching($\'product\')"'],
[new NotEmpty('test'), '"notEmpty(\'test\')"'],
[new EachKey(["doesn't matter"], [new Regex('\$(\.\w+)+', '$.test.one')]), '"eachKey(matching(regex, \'\\\\$(\\\\.\\\\w+)+\', \'$.test.one\'))"'],
[new EachValue(["doesn't matter"], [new Type(100)]), '"eachValue(matching(type, 100))"'],
[new Equality('Example value'), '"matching(equalTo, \'Example value\')"'],
[new Type('Example value'), '"matching(type, \'Example value\')"'],
[new Number(100.09), '"matching(number, 100.09)"'],
[new Integer(100), '"matching(integer, 100)"'],
[new Decimal(100.01), '"matching(decimal, 100.01)"'],
[new Includes('testing'), '"matching(include, \'testing\')"'],
[new Boolean(true), '"matching(boolean, true)"'],
[new Semver('1.0.0'), '"matching(semver, \'1.0.0\')"'],
[new DateTime('yyyy-MM-dd HH:mm:ssZZZZZ', '2020-05-21 16:44:32+10:00'), '"matching(datetime, \'yyyy-MM-dd HH:mm:ssZZZZZ\', \'2020-05-21 16:44:32+10:00\')"'],
[new Date('yyyy-MM-dd', '2012-04-12'), '"matching(date, \'yyyy-MM-dd\', \'2012-04-12\')"'],
[new Time('HH:mm', '22:04'), '"matching(time, \'HH:mm\', \'22:04\')"'],
[new Regex('\\w{3}\\d+', 'abc123'), '"matching(regex, \'\\\\w{3}\\\\d+\', \'abc123\')"'],
[new ContentType('application/xml'), '"matching(contentType, \'application\/xml\', \'application\/xml\')"'],
[new NullValue(), '"matching(type, null)"'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
use PhpPact\Consumer\Matcher\Formatters\ValueOptionalFormatter;
use PhpPact\Consumer\Matcher\Generators\RandomString;
use PhpPact\Consumer\Matcher\Matchers\Date;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;

class ValueOptionalFormatterTest extends TestCase
{
/**
* @testWith [true, "2001-01-02", {"pact:matcher:type": "date", "pact:generator:type": "RandomString", "format": "yyyy-MM-dd", "size": 10}]
* [false, "2002-02-03", {"pact:matcher:type": "date", "format": "yyyy-MM-dd", "value": "2002-02-03"}]
* [true, null, {"pact:matcher:type": "date", "pact:generator:type": "RandomString", "format": "yyyy-MM-dd", "size": 10}]
* [false, null, {"pact:matcher:type": "date", "format": "yyyy-MM-dd", "value": null}]
*/
public function testFormat(bool $hasGenerator, ?string $value, array $result): void
#[TestWith([true, '2001-01-02', '{"pact:matcher:type": "date", "pact:generator:type": "RandomString", "format": "yyyy-MM-dd", "size": 10}'])]
#[TestWith([false, '2002-02-03', '{"pact:matcher:type": "date", "format": "yyyy-MM-dd", "value": "2002-02-03"}'])]
#[TestWith([true, null, '{"pact:matcher:type": "date", "pact:generator:type": "RandomString", "format": "yyyy-MM-dd", "size": 10}'])]
#[TestWith([false, null, '{"pact:matcher:type": "date", "format": "yyyy-MM-dd", "value": null}'])]
public function testFormat(bool $hasGenerator, ?string $value, string $result): void
{
$matcher = new Date('yyyy-MM-dd', $value);
$generator = $hasGenerator ? new RandomString(10) : null;
$matcher->setGenerator($generator);
$formatter = new ValueOptionalFormatter();
$this->assertSame($result, $formatter->format($matcher));
$jsonEncoded = json_encode($formatter->format($matcher));
$this->assertIsString($jsonEncoded);
$this->assertJsonStringEqualsJsonString($result, $jsonEncoded);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
use PhpPact\Consumer\Matcher\Formatters\ValueRequiredFormatter;
use PhpPact\Consumer\Matcher\Generators\RandomString;
use PhpPact\Consumer\Matcher\Matchers\Date;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;

class ValueRequiredFormatterTest extends TestCase
{
/**
* @testWith [true, "2001-01-02", {"pact:matcher:type": "date", "pact:generator:type": "RandomString", "format": "yyyy-MM-dd", "size": 10, "value": "2001-01-02"}]
* [false, "2002-02-03", {"pact:matcher:type": "date", "format": "yyyy-MM-dd", "value": "2002-02-03"}]
* [true, null, {"pact:matcher:type": "date", "pact:generator:type": "RandomString", "format": "yyyy-MM-dd", "size": 10, "value": null}]
* [false, null, {"pact:matcher:type": "date", "format": "yyyy-MM-dd", "value": null}]
*/
public function testFormat(bool $hasGenerator, ?string $value, array $result): void
#[TestWith([true, '2001-01-02', '{"pact:matcher:type": "date", "pact:generator:type": "RandomString", "format": "yyyy-MM-dd", "size": 10, "value": "2001-01-02"}'])]
#[TestWith([false, '2002-02-03', '{"pact:matcher:type": "date", "format": "yyyy-MM-dd", "value": "2002-02-03"}'])]
#[TestWith([true, null, '{"pact:matcher:type": "date", "pact:generator:type": "RandomString", "format": "yyyy-MM-dd", "size": 10, "value": null}'])]
#[TestWith([false, null, '{"pact:matcher:type": "date", "format": "yyyy-MM-dd", "value": null}'])]
public function testFormat(bool $hasGenerator, ?string $value, string $result): void
{
$matcher = new Date('yyyy-MM-dd', $value);
$generator = $hasGenerator ? new RandomString(10) : null;
$matcher->setGenerator($generator);
$formatter = new ValueRequiredFormatter();
$this->assertSame($result, $formatter->format($matcher));
$jsonEncoded = json_encode($formatter->format($matcher));
$this->assertIsString($jsonEncoded);
$this->assertJsonStringEqualsJsonString($result, $jsonEncoded);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
use PhpPact\Consumer\Matcher\Formatters\XmlContentFormatter;
use PhpPact\Consumer\Matcher\Generators\RandomString;
use PhpPact\Consumer\Matcher\Matchers\Date;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;

class XmlContentFormatterTest extends TestCase
{
/**
* @testWith [true, "2001-01-02", {"content": "2001-01-02", "matcher": {"pact:matcher:type": "date", "format": "yyyy-MM-dd", "size": 10}, "pact:generator:type": "RandomString"}]
* [false, "2002-02-03", {"content": "2002-02-03", "matcher": {"pact:matcher:type": "date", "format": "yyyy-MM-dd"}}]
*/
public function testFormat(bool $hasGenerator, ?string $value, array $result): void
#[TestWith([true, '2001-01-02', '{"content": "2001-01-02", "matcher": {"pact:matcher:type": "date", "format": "yyyy-MM-dd", "size": 10}, "pact:generator:type": "RandomString"}'])]
#[TestWith([false, '2002-02-03', '{"content": "2002-02-03", "matcher": {"pact:matcher:type": "date", "format": "yyyy-MM-dd"}}'])]
public function testFormat(bool $hasGenerator, ?string $value, string $result): void
{
$matcher = new Date('yyyy-MM-dd', $value);
$generator = $hasGenerator ? new RandomString(10) : null;
$matcher->setGenerator($generator);
$formatter = new XmlContentFormatter();
$this->assertSame($result, $formatter->format($matcher));
$jsonEncoded = json_encode($formatter->format($matcher));
$this->assertIsString($jsonEncoded);
$this->assertJsonStringEqualsJsonString($result, $jsonEncoded);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpPact\Consumer\Matcher\Matchers\StringValue;
use PhpPact\Consumer\Matcher\Matchers\Type;
use PhpPact\Xml\XmlElement;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;

class XmlElementFormatterTest extends TestCase
Expand All @@ -22,18 +23,18 @@ public function testFormatInvalidValue(): void
$formatter->format($matcher);
}

/**
* @testWith [null, {"pact:matcher:type": "type", "value": {"name": "test", "children": [], "attributes": []}}]
* [123, {"pact:matcher:type": "type", "value": {"name": "test", "children": [], "attributes": []}, "examples": 123}]
*/
public function testFormat(?int $examples, array $result): void
#[TestWith([null, '{"pact:matcher:type": "type", "value": {"name": "test", "children": [], "attributes": []}}'])]
#[TestWith([123, '{"pact:matcher:type": "type", "value": {"name": "test", "children": [], "attributes": []}, "examples": 123}'])]
public function testFormat(?int $examples, string $result): void
{
$value = new XmlElement(
fn (XmlElement $element) => $element->setName('test'),
fn (XmlElement $element) => $element->setExamples($examples),
);
$matcher = new Type($value);
$formatter = new XmlElementFormatter();
$this->assertSame(json_encode($result), json_encode($formatter->format($matcher)));
$jsonEncoded = json_encode($formatter->format($matcher));
$this->assertIsString($jsonEncoded);
$this->assertJsonStringEqualsJsonString($result, $jsonEncoded);
}
}
10 changes: 6 additions & 4 deletions tests/PhpPact/Consumer/Matcher/Generators/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpPactTest\Consumer\Matcher\Generators;

use PhpPact\Consumer\Matcher\Generators\Date;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;

class DateTest extends TestCase
Expand All @@ -14,11 +15,12 @@ public function testType(): void
}

/**
* @testWith [null, null, []]
* ["yyyy-MM-dd", null, {"format":"yyyy-MM-dd"}]
* [null, "+1 day", {"expression":"+1 day"}]
* ["yyyy-MM-dd", "+1 day", {"format":"yyyy-MM-dd","expression":"+1 day"}]
* @param array<string, string> $data
*/
#[TestWith([null, null, []])]
#[TestWith(['yyyy-MM-dd', null, ['format' => 'yyyy-MM-dd']])]
#[TestWith([null, '+1 day', ['expression' => '+1 day']])]
#[TestWith(['yyyy-MM-dd', '+1 day', ['format' => 'yyyy-MM-dd', 'expression' => '+1 day']])]
public function testAttributes(?string $format, ?string $expression, array $data): void
{
$generator = new Date($format, $expression);
Expand Down
Loading

0 comments on commit 769a930

Please sign in to comment.