From b38b3f0b0f915972fa37115dcdaf05915cb795cc Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Wed, 2 Oct 2024 14:25:20 +0700 Subject: [PATCH 1/3] feat: Support string contains single quote in matching expression --- .../Expression/AbstractExpressionFormatter.php | 4 ++-- .../Expression/ContentTypeFormatterTest.php | 11 ++--------- .../Formatters/Expression/DateTimeFormatterTest.php | 11 ++--------- .../Formatters/Expression/EqualityFormatterTest.php | 9 +-------- .../Formatters/Expression/IncludesFormatterTest.php | 9 +-------- .../Expression/MatchingFieldFormatterTest.php | 9 +-------- .../Formatters/Expression/NotEmptyFormatterTest.php | 9 +-------- .../Formatters/Expression/RegexFormatterTest.php | 11 ++--------- .../Formatters/Expression/SemverFormatterTest.php | 9 +-------- .../Expression/StringValueFormatterTest.php | 9 +-------- .../Formatters/Expression/TypeFormatterTest.php | 9 +-------- 11 files changed, 15 insertions(+), 85 deletions(-) diff --git a/src/PhpPact/Consumer/Matcher/Formatters/Expression/AbstractExpressionFormatter.php b/src/PhpPact/Consumer/Matcher/Formatters/Expression/AbstractExpressionFormatter.php index d15ea96b..e407e3cb 100644 --- a/src/PhpPact/Consumer/Matcher/Formatters/Expression/AbstractExpressionFormatter.php +++ b/src/PhpPact/Consumer/Matcher/Formatters/Expression/AbstractExpressionFormatter.php @@ -11,8 +11,8 @@ abstract class AbstractExpressionFormatter implements ExpressionFormatterInterfa { protected function normalize(mixed $value): string { - if (is_string($value) && str_contains($value, "'")) { - throw new InvalidValueException(sprintf('String value "%s" should not contains single quote', $value)); + if (is_string($value)) { + $value = addcslashes($value, "'"); } return match (gettype($value)) { 'string' => sprintf("'%s'", $value), diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/ContentTypeFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/ContentTypeFormatterTest.php index 3f1ed6ad..2673a866 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/ContentTypeFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/ContentTypeFormatterTest.php @@ -29,15 +29,8 @@ public function testNotSupportedMatcher(): void $this->formatter->format($matcher); } - #[TestWith([new ContentType("it's invalid type", 'testing'), "it's invalid type"])] - #[TestWith([new ContentType('plain/text', "it's invalid text"), "it's invalid text"])] - public function testInvalidString(MatcherInterface $matcher, string $value): void - { - $this->expectException(InvalidValueException::class); - $this->expectExceptionMessage(sprintf('String value "%s" should not contains single quote', $value)); - $this->formatter->format($matcher); - } - + #[TestWith([new ContentType("contains single quote '", 'testing'), 'matching(contentType, \'contains single quote \\\'\', \'testing\')'])] + #[TestWith([new ContentType('plain/text', "contains single quote '"), 'matching(contentType, \'plain/text\', \'contains single quote \\\'\')'])] #[TestWith([new ContentType('plain/text'), 'matching(contentType, \'plain/text\', \'\')'])] #[TestWith([new ContentType('application/json', '{"key":"value"}'), 'matching(contentType, \'application/json\', \'{"key":"value"}\')'])] #[TestWith([new ContentType('application/xml', ''), 'matching(contentType, \'application/xml\', \'\')'])] diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/DateTimeFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/DateTimeFormatterTest.php index ad91b638..e819d713 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/DateTimeFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/DateTimeFormatterTest.php @@ -39,15 +39,8 @@ public function testInvalidValue(): void $this->formatter->format($matcher); } - #[TestWith([new Time("it's invalid format", 'testing'), "it's invalid format"])] - #[TestWith([new Time('HH:mm', "it's invalid value"), "it's invalid value"])] - public function testInvalidString(MatcherInterface $matcher, string $value): void - { - $this->expectException(InvalidValueException::class); - $this->expectExceptionMessage(sprintf('String value "%s" should not contains single quote', $value)); - $this->formatter->format($matcher); - } - + #[TestWith([new Time("contains single quote '", '22:04'), 'matching(time, \'contains single quote \\\'\', \'22:04\')'])] + #[TestWith([new Time('HH:mm', "contains single quote '"), 'matching(time, \'HH:mm\', \'contains single quote \\\'\')'])] #[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\')'])] diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EqualityFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EqualityFormatterTest.php index 687df249..5c47035b 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EqualityFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EqualityFormatterTest.php @@ -38,14 +38,7 @@ public function testInvalidValue(MatcherInterface $matcher, string $type): void $this->formatter->format($matcher); } - #[TestWith([new Equality("it's invalid value"), "it's invalid value"])] - public function testInvalidString(MatcherInterface $matcher, string $value): void - { - $this->expectException(InvalidValueException::class); - $this->expectExceptionMessage(sprintf('String value "%s" should not contains single quote', $value)); - $this->formatter->format($matcher); - } - + #[TestWith([new Equality("contains single quote '"), 'matching(equalTo, \'contains single quote \\\'\')'])] #[TestWith([new Equality('example value'), 'matching(equalTo, \'example value\')'])] #[TestWith([new Equality(100.09), 'matching(equalTo, 100.09)'])] #[TestWith([new Equality(-99.99), 'matching(equalTo, -99.99)'])] diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/IncludesFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/IncludesFormatterTest.php index aa93106d..3966af7c 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/IncludesFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/IncludesFormatterTest.php @@ -29,14 +29,7 @@ public function testNotSupportedMatcher(): void $this->formatter->format($matcher); } - #[TestWith([new Includes("it's invalid value"), "it's invalid value"])] - public function testInvalidString(MatcherInterface $matcher, string $value): void - { - $this->expectException(InvalidValueException::class); - $this->expectExceptionMessage(sprintf('String value "%s" should not contains single quote', $value)); - $this->formatter->format($matcher); - } - + #[TestWith([new Includes("contains single quote '"), 'matching(include, \'contains single quote \\\'\')'])] #[TestWith([new Includes('example value'), 'matching(include, \'example value\')'])] public function testFormat(MatcherInterface $matcher, string $expression): void { diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MatchingFieldFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MatchingFieldFormatterTest.php index 43b545d6..36e9f704 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MatchingFieldFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MatchingFieldFormatterTest.php @@ -29,14 +29,7 @@ public function testNotSupportedMatcher(): void $this->formatter->format($matcher); } - #[TestWith([new MatchingField("it's invalid field"), "it's invalid field"])] - public function testInvalidString(MatcherInterface $matcher, string $value): void - { - $this->expectException(InvalidValueException::class); - $this->expectExceptionMessage(sprintf('String value "%s" should not contains single quote', $value)); - $this->formatter->format($matcher); - } - + #[TestWith([new MatchingField("contains single quote '"), 'matching($\'contains single quote \\\'\')'])] #[TestWith([new MatchingField('product'), 'matching($\'product\')'])] public function testFormat(MatcherInterface $matcher, string $expression): void { diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/NotEmptyFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/NotEmptyFormatterTest.php index a4c00092..9e828d25 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/NotEmptyFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/NotEmptyFormatterTest.php @@ -38,14 +38,7 @@ public function testInvalidValue(MatcherInterface $matcher, string $type): void $this->formatter->format($matcher); } - #[TestWith([new NotEmpty("it's invalid value"), "it's invalid value"])] - public function testInvalidString(MatcherInterface $matcher, string $value): void - { - $this->expectException(InvalidValueException::class); - $this->expectExceptionMessage(sprintf('String value "%s" should not contains single quote', $value)); - $this->formatter->format($matcher); - } - + #[TestWith([new NotEmpty("contains single quote '"), 'notEmpty(\'contains single quote \\\'\')'])] #[TestWith([new NotEmpty('example value'), 'notEmpty(\'example value\')'])] #[TestWith([new NotEmpty(100.09), 'notEmpty(100.09)'])] #[TestWith([new NotEmpty(100), 'notEmpty(100)'])] diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/RegexFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/RegexFormatterTest.php index 652a8948..bd3c8163 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/RegexFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/RegexFormatterTest.php @@ -38,15 +38,8 @@ public function testInvalidValue(MatcherInterface $matcher, string $type): void $this->formatter->format($matcher); } - #[TestWith([new Regex("it's invalid regex", 'value'), "it's invalid regex"])] - #[TestWith([new Regex('\w \d', "it's invalid value"), "it's invalid value"])] - public function testInvalidString(MatcherInterface $matcher, string $value): void - { - $this->expectException(InvalidValueException::class); - $this->expectExceptionMessage(sprintf('String value "%s" should not contains single quote', $value)); - $this->formatter->format($matcher); - } - + #[TestWith([new Regex("contains single quote '", 'value'), 'matching(regex, \'contains single quote \\\'\', \'value\')'])] + #[TestWith([new Regex('\w \d', "contains single quote '"), 'matching(regex, \'\w \d\', \'contains single quote \\\'\')'])] #[TestWith([new Regex('\w{3}\d+', 'abc123'), 'matching(regex, \'\w{3}\d+\', \'abc123\')'])] public function testFormat(MatcherInterface $matcher, string $expression): void { diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/SemverFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/SemverFormatterTest.php index 43a215d2..97fe085e 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/SemverFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/SemverFormatterTest.php @@ -37,14 +37,7 @@ public function testInvalidValue(MatcherInterface $matcher, string $type): void $this->formatter->format($matcher); } - #[TestWith([new Semver("it's invalid value"), "it's invalid value"])] - public function testInvalidString(MatcherInterface $matcher, string $value): void - { - $this->expectException(InvalidValueException::class); - $this->expectExceptionMessage(sprintf('String value "%s" should not contains single quote', $value)); - $this->formatter->format($matcher); - } - + #[TestWith([new Semver("contains single quote '"), 'matching(semver, \'contains single quote \\\'\')'])] #[TestWith([new Semver('1.0.0'), 'matching(semver, \'1.0.0\')'])] public function testFormat(MatcherInterface $matcher, string $expression): void { diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/StringValueFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/StringValueFormatterTest.php index 9ada08f1..45e59e70 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/StringValueFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/StringValueFormatterTest.php @@ -29,14 +29,7 @@ public function testNotSupportedMatcher(): void $this->formatter->format($matcher); } - #[TestWith([new StringValue("it's invalid value"), "it's invalid value"])] - public function testInvalidString(MatcherInterface $matcher, string $value): void - { - $this->expectException(InvalidValueException::class); - $this->expectExceptionMessage(sprintf('String value "%s" should not contains single quote', $value)); - $this->formatter->format($matcher); - } - + #[TestWith([new StringValue("contains single quote '"), 'matching(type, \'contains single quote \\\'\')'])] #[TestWith([new StringValue('value'), 'matching(type, \'value\')'])] public function testFormat(MatcherInterface $matcher, string $expression): void { diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/TypeFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/TypeFormatterTest.php index 91afe549..9277baad 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/TypeFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/TypeFormatterTest.php @@ -40,14 +40,7 @@ public function testInvalidValue(MatcherInterface $matcher, string $type): void $this->formatter->format($matcher); } - #[TestWith([new Type("it's invalid value"), "it's invalid value"])] - public function testInvalidString(MatcherInterface $matcher, string $value): void - { - $this->expectException(InvalidValueException::class); - $this->expectExceptionMessage(sprintf('String value "%s" should not contains single quote', $value)); - $this->formatter->format($matcher); - } - + #[TestWith([new Type("contains single quote '"), 'matching(type, \'contains single quote \\\'\')'])] #[TestWith([new Type('example value'), 'matching(type, \'example value\')'])] #[TestWith([new Type(100.09), 'matching(type, 100.09)'])] #[TestWith([new Type(-99.99), 'matching(type, -99.99)'])] From d2cdcfe7ee457d98b6f0441619ff4a9e195b9b61 Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Thu, 3 Oct 2024 14:38:20 +0700 Subject: [PATCH 2/3] refactor(test): Use double quote to remove unneccessary single quote escaping --- .../Formatters/Expression/ContentTypeFormatterTest.php | 10 +++++----- .../Formatters/Expression/DateTimeFormatterTest.php | 10 +++++----- .../Formatters/Expression/EachKeyFormatterTest.php | 2 +- .../Formatters/Expression/EachValueFormatterTest.php | 4 ++-- .../Formatters/Expression/EqualityFormatterTest.php | 4 ++-- .../Formatters/Expression/IncludesFormatterTest.php | 4 ++-- .../Formatters/Expression/MatchAllFormatterTest.php | 2 +- .../Expression/MatchingFieldFormatterTest.php | 4 ++-- .../Formatters/Expression/MaxTypeFormatterTest.php | 2 +- .../Formatters/Expression/MinMaxTypeFormatterTest.php | 2 +- .../Formatters/Expression/MinTypeFormatterTest.php | 2 +- .../Formatters/Expression/NotEmptyFormatterTest.php | 4 ++-- .../Formatters/Expression/RegexFormatterTest.php | 6 +++--- .../Formatters/Expression/SemverFormatterTest.php | 4 ++-- .../Formatters/Expression/StringValueFormatterTest.php | 4 ++-- .../Formatters/Expression/TypeFormatterTest.php | 4 ++-- 16 files changed, 34 insertions(+), 34 deletions(-) diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/ContentTypeFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/ContentTypeFormatterTest.php index 2673a866..83a4c44d 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/ContentTypeFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/ContentTypeFormatterTest.php @@ -29,11 +29,11 @@ public function testNotSupportedMatcher(): void $this->formatter->format($matcher); } - #[TestWith([new ContentType("contains single quote '", 'testing'), 'matching(contentType, \'contains single quote \\\'\', \'testing\')'])] - #[TestWith([new ContentType('plain/text', "contains single quote '"), 'matching(contentType, \'plain/text\', \'contains single quote \\\'\')'])] - #[TestWith([new ContentType('plain/text'), 'matching(contentType, \'plain/text\', \'\')'])] - #[TestWith([new ContentType('application/json', '{"key":"value"}'), 'matching(contentType, \'application/json\', \'{"key":"value"}\')'])] - #[TestWith([new ContentType('application/xml', ''), 'matching(contentType, \'application/xml\', \'\')'])] + #[TestWith([new ContentType("contains single quote '", 'testing'), "matching(contentType, 'contains single quote \'', 'testing')"])] + #[TestWith([new ContentType('plain/text', "contains single quote '"), "matching(contentType, 'plain/text', 'contains single quote \'')"])] + #[TestWith([new ContentType('plain/text'), "matching(contentType, 'plain/text', '')"])] + #[TestWith([new ContentType('application/json', '{"key":"value"}'), "matching(contentType, 'application/json', '{\"key\":\"value\"}')"])] + #[TestWith([new ContentType('application/xml', ''), "matching(contentType, 'application/xml', '')"])] public function testFormat(MatcherInterface $matcher, string $expression): void { $result = $this->formatter->format($matcher); diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/DateTimeFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/DateTimeFormatterTest.php index e819d713..d13ab4ff 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/DateTimeFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/DateTimeFormatterTest.php @@ -39,11 +39,11 @@ public function testInvalidValue(): void $this->formatter->format($matcher); } - #[TestWith([new Time("contains single quote '", '22:04'), 'matching(time, \'contains single quote \\\'\', \'22:04\')'])] - #[TestWith([new Time('HH:mm', "contains single quote '"), 'matching(time, \'HH:mm\', \'contains single quote \\\'\')'])] - #[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 Time("contains single quote '", '22:04'), "matching(time, 'contains single quote \'', '22:04')"])] + #[TestWith([new Time('HH:mm', "contains single quote '"), "matching(time, 'HH:mm', 'contains single quote \'')"])] + #[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')"])] public function testFormat(MatcherInterface $matcher, string $expression): void { $result = $this->formatter->format($matcher); diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EachKeyFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EachKeyFormatterTest.php index 55bac77c..c7cf46d8 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EachKeyFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EachKeyFormatterTest.php @@ -43,7 +43,7 @@ public function testInvalidRules(EachKey $matcher): void } #[TestWith([new EachKey(['value'], [new Integer(123)]), 'eachKey(matching(integer, 123))'])] - #[TestWith([new EachKey(new stdClass(), [new Regex('\w+', 'example value')]), 'eachKey(matching(regex, \'\w+\', \'example value\'))'])] + #[TestWith([new EachKey(new stdClass(), [new Regex('\w+', 'example value')]), "eachKey(matching(regex, '\w+', 'example value'))"])] public function testFormat(MatcherInterface $matcher, string $expression): void { $result = $this->formatter->format($matcher); diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EachValueFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EachValueFormatterTest.php index 450324f5..91aae515 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EachValueFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EachValueFormatterTest.php @@ -42,8 +42,8 @@ public function testInvalidRules(EachValue $matcher): void $this->formatter->format($matcher); } - #[TestWith([new EachValue(['value'], [new StringValue('example value')]), 'eachValue(matching(type, \'example value\'))'])] - #[TestWith([new EachValue(new stdClass(), [new Regex('\w \d', 'a 1')]), 'eachValue(matching(regex, \'\w \d\', \'a 1\'))'])] + #[TestWith([new EachValue(['value'], [new StringValue('example value')]), "eachValue(matching(type, 'example value'))"])] + #[TestWith([new EachValue(new stdClass(), [new Regex('\w \d', 'a 1')]), "eachValue(matching(regex, '\w \d', 'a 1'))"])] public function testFormat(MatcherInterface $matcher, string $expression): void { $result = $this->formatter->format($matcher); diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EqualityFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EqualityFormatterTest.php index 5c47035b..ed618f0c 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EqualityFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EqualityFormatterTest.php @@ -38,8 +38,8 @@ public function testInvalidValue(MatcherInterface $matcher, string $type): void $this->formatter->format($matcher); } - #[TestWith([new Equality("contains single quote '"), 'matching(equalTo, \'contains single quote \\\'\')'])] - #[TestWith([new Equality('example value'), 'matching(equalTo, \'example value\')'])] + #[TestWith([new Equality("contains single quote '"), "matching(equalTo, 'contains single quote \'')"])] + #[TestWith([new Equality('example value'), "matching(equalTo, 'example value')"])] #[TestWith([new Equality(100.09), 'matching(equalTo, 100.09)'])] #[TestWith([new Equality(-99.99), 'matching(equalTo, -99.99)'])] #[TestWith([new Equality(100), 'matching(equalTo, 100)'])] diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/IncludesFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/IncludesFormatterTest.php index 3966af7c..f0e304f7 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/IncludesFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/IncludesFormatterTest.php @@ -29,8 +29,8 @@ public function testNotSupportedMatcher(): void $this->formatter->format($matcher); } - #[TestWith([new Includes("contains single quote '"), 'matching(include, \'contains single quote \\\'\')'])] - #[TestWith([new Includes('example value'), 'matching(include, \'example value\')'])] + #[TestWith([new Includes("contains single quote '"), "matching(include, 'contains single quote \'')"])] + #[TestWith([new Includes('example value'), "matching(include, 'example value')"])] public function testFormat(MatcherInterface $matcher, string $expression): void { $result = $this->formatter->format($matcher); diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MatchAllFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MatchAllFormatterTest.php index e0cafeba..7e265596 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MatchAllFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MatchAllFormatterTest.php @@ -45,7 +45,7 @@ public function testInvalidMatchers(MatchAll $matcher): void } #[TestWith([new MatchAll(['abc' => 1, 'def' => 234], [new MinType(null, 2, false)]), 'atLeast(2)'])] - #[TestWith([new MatchAll(['abc' => 1, 'def' => 234], [new MinType(null, 1, false), new MaxType(null, 2, false), new EachKey(["doesn't matter"], [new Regex('\w+', 'abc')]), new EachValue(["doesn't matter"], [new Type(100)])]), 'atLeast(1), atMost(2), eachKey(matching(regex, \'\w+\', \'abc\')), eachValue(matching(type, 100))'])] + #[TestWith([new MatchAll(['abc' => 1, 'def' => 234], [new MinType(null, 1, false), new MaxType(null, 2, false), new EachKey(["doesn't matter"], [new Regex('\w+', 'abc')]), new EachValue(["doesn't matter"], [new Type(100)])]), "atLeast(1), atMost(2), eachKey(matching(regex, '\w+', 'abc')), eachValue(matching(type, 100))"])] public function testFormat(MatcherInterface $matcher, string $expression): void { $result = $this->formatter->format($matcher); diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MatchingFieldFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MatchingFieldFormatterTest.php index 36e9f704..dbf1712b 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MatchingFieldFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MatchingFieldFormatterTest.php @@ -29,8 +29,8 @@ public function testNotSupportedMatcher(): void $this->formatter->format($matcher); } - #[TestWith([new MatchingField("contains single quote '"), 'matching($\'contains single quote \\\'\')'])] - #[TestWith([new MatchingField('product'), 'matching($\'product\')'])] + #[TestWith([new MatchingField("contains single quote '"), "matching($'contains single quote \'')"])] + #[TestWith([new MatchingField('product'), "matching($'product')"])] public function testFormat(MatcherInterface $matcher, string $expression): void { $result = $this->formatter->format($matcher); diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MaxTypeFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MaxTypeFormatterTest.php index 3b2a911a..73b2fea2 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MaxTypeFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MaxTypeFormatterTest.php @@ -31,7 +31,7 @@ public function testNotSupportedMatcher(): void #[TestWith([new MaxType(null, 2, false), 'atMost(2)'])] #[TestWith([new MaxType('example value', 2, false), 'atMost(2)'])] #[TestWith([new MaxType(null, 2), 'atMost(2), eachValue(matching(type, null)'])] - #[TestWith([new MaxType('example value', 2), 'atMost(2), eachValue(matching(type, \'example value\')'])] + #[TestWith([new MaxType('example value', 2), "atMost(2), eachValue(matching(type, 'example value')"])] public function testFormat(MatcherInterface $matcher, string $expression): void { $result = $this->formatter->format($matcher); diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MinMaxTypeFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MinMaxTypeFormatterTest.php index f1c29eb8..5c82daeb 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MinMaxTypeFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MinMaxTypeFormatterTest.php @@ -29,7 +29,7 @@ public function testNotSupportedMatcher(): void } #[TestWith([new MinMaxType(null, 2, 3), 'atLeast(2), atMost(3), eachValue(matching(type, null)'])] - #[TestWith([new MinMaxType('example value', 2, 3), 'atLeast(2), atMost(3), eachValue(matching(type, \'example value\')'])] + #[TestWith([new MinMaxType('example value', 2, 3), "atLeast(2), atMost(3), eachValue(matching(type, 'example value')"])] public function testFormat(MatcherInterface $matcher, string $expression): void { $result = $this->formatter->format($matcher); diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MinTypeFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MinTypeFormatterTest.php index c7240400..7300b7b3 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MinTypeFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MinTypeFormatterTest.php @@ -31,7 +31,7 @@ public function testNotSupportedMatcher(): void #[TestWith([new MinType(null, 1, false), 'atLeast(1)'])] #[TestWith([new MinType('example value', 1, false), 'atLeast(1)'])] #[TestWith([new MinType(null, 1), 'atLeast(1), eachValue(matching(type, null)'])] - #[TestWith([new MinType('example value', 1), 'atLeast(1), eachValue(matching(type, \'example value\')'])] + #[TestWith([new MinType('example value', 1), "atLeast(1), eachValue(matching(type, 'example value')"])] public function testFormat(MatcherInterface $matcher, string $expression): void { $result = $this->formatter->format($matcher); diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/NotEmptyFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/NotEmptyFormatterTest.php index 9e828d25..ad0d5a33 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/NotEmptyFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/NotEmptyFormatterTest.php @@ -38,8 +38,8 @@ public function testInvalidValue(MatcherInterface $matcher, string $type): void $this->formatter->format($matcher); } - #[TestWith([new NotEmpty("contains single quote '"), 'notEmpty(\'contains single quote \\\'\')'])] - #[TestWith([new NotEmpty('example value'), 'notEmpty(\'example value\')'])] + #[TestWith([new NotEmpty("contains single quote '"), "notEmpty('contains single quote \'')"])] + #[TestWith([new NotEmpty('example value'), "notEmpty('example value')"])] #[TestWith([new NotEmpty(100.09), 'notEmpty(100.09)'])] #[TestWith([new NotEmpty(100), 'notEmpty(100)'])] #[TestWith([new NotEmpty(true), 'notEmpty(true)'])] diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/RegexFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/RegexFormatterTest.php index bd3c8163..771ca406 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/RegexFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/RegexFormatterTest.php @@ -38,9 +38,9 @@ public function testInvalidValue(MatcherInterface $matcher, string $type): void $this->formatter->format($matcher); } - #[TestWith([new Regex("contains single quote '", 'value'), 'matching(regex, \'contains single quote \\\'\', \'value\')'])] - #[TestWith([new Regex('\w \d', "contains single quote '"), 'matching(regex, \'\w \d\', \'contains single quote \\\'\')'])] - #[TestWith([new Regex('\w{3}\d+', 'abc123'), 'matching(regex, \'\w{3}\d+\', \'abc123\')'])] + #[TestWith([new Regex("contains single quote '", 'value'), "matching(regex, 'contains single quote \'', 'value')"])] + #[TestWith([new Regex('\w \d', "contains single quote '"), "matching(regex, '\w \d', 'contains single quote \'')"])] + #[TestWith([new Regex('\w{3}\d+', 'abc123'), "matching(regex, '\w{3}\d+', 'abc123')"])] public function testFormat(MatcherInterface $matcher, string $expression): void { $result = $this->formatter->format($matcher); diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/SemverFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/SemverFormatterTest.php index 97fe085e..d8833e25 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/SemverFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/SemverFormatterTest.php @@ -37,8 +37,8 @@ public function testInvalidValue(MatcherInterface $matcher, string $type): void $this->formatter->format($matcher); } - #[TestWith([new Semver("contains single quote '"), 'matching(semver, \'contains single quote \\\'\')'])] - #[TestWith([new Semver('1.0.0'), 'matching(semver, \'1.0.0\')'])] + #[TestWith([new Semver("contains single quote '"), "matching(semver, 'contains single quote \'')"])] + #[TestWith([new Semver('1.0.0'), "matching(semver, '1.0.0')"])] public function testFormat(MatcherInterface $matcher, string $expression): void { $result = $this->formatter->format($matcher); diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/StringValueFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/StringValueFormatterTest.php index 45e59e70..7cf53408 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/StringValueFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/StringValueFormatterTest.php @@ -29,8 +29,8 @@ public function testNotSupportedMatcher(): void $this->formatter->format($matcher); } - #[TestWith([new StringValue("contains single quote '"), 'matching(type, \'contains single quote \\\'\')'])] - #[TestWith([new StringValue('value'), 'matching(type, \'value\')'])] + #[TestWith([new StringValue("contains single quote '"), "matching(type, 'contains single quote \'')"])] + #[TestWith([new StringValue('value'), "matching(type, 'value')"])] public function testFormat(MatcherInterface $matcher, string $expression): void { $result = $this->formatter->format($matcher); diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/TypeFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/TypeFormatterTest.php index 9277baad..3eda0df3 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/TypeFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/TypeFormatterTest.php @@ -40,8 +40,8 @@ public function testInvalidValue(MatcherInterface $matcher, string $type): void $this->formatter->format($matcher); } - #[TestWith([new Type("contains single quote '"), 'matching(type, \'contains single quote \\\'\')'])] - #[TestWith([new Type('example value'), 'matching(type, \'example value\')'])] + #[TestWith([new Type("contains single quote '"), "matching(type, 'contains single quote \'')"])] + #[TestWith([new Type('example value'), "matching(type, 'example value')"])] #[TestWith([new Type(100.09), 'matching(type, 100.09)'])] #[TestWith([new Type(-99.99), 'matching(type, -99.99)'])] #[TestWith([new Type(100), 'matching(type, 100)'])] From 79299c77ba6762f7c5a3b6a9df52ac835a16d50f Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Thu, 3 Oct 2024 14:51:00 +0700 Subject: [PATCH 3/3] test: Add more single quote handling test cases --- .../Matcher/Formatters/Expression/EachKeyFormatterTest.php | 2 ++ .../Matcher/Formatters/Expression/EachValueFormatterTest.php | 1 + .../Matcher/Formatters/Expression/MatchAllFormatterTest.php | 2 ++ .../Matcher/Formatters/Expression/MaxTypeFormatterTest.php | 1 + .../Matcher/Formatters/Expression/MinMaxTypeFormatterTest.php | 1 + .../Matcher/Formatters/Expression/MinTypeFormatterTest.php | 1 + 6 files changed, 8 insertions(+) diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EachKeyFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EachKeyFormatterTest.php index c7cf46d8..f16b9144 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EachKeyFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EachKeyFormatterTest.php @@ -9,6 +9,7 @@ use PhpPact\Consumer\Matcher\Matchers\Integer; use PhpPact\Consumer\Matcher\Matchers\NullValue; use PhpPact\Consumer\Matcher\Matchers\Regex; +use PhpPact\Consumer\Matcher\Matchers\StringValue; use PhpPact\Consumer\Matcher\Matchers\Type; use PhpPact\Consumer\Matcher\Model\FormatterInterface; use PhpPact\Consumer\Matcher\Model\MatcherInterface; @@ -42,6 +43,7 @@ public function testInvalidRules(EachKey $matcher): void $this->formatter->format($matcher); } + #[TestWith([new EachKey(['value'], [new StringValue("contains single quote '")]), "eachKey(matching(type, 'contains single quote \''))"])] #[TestWith([new EachKey(['value'], [new Integer(123)]), 'eachKey(matching(integer, 123))'])] #[TestWith([new EachKey(new stdClass(), [new Regex('\w+', 'example value')]), "eachKey(matching(regex, '\w+', 'example value'))"])] public function testFormat(MatcherInterface $matcher, string $expression): void diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EachValueFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EachValueFormatterTest.php index 91aae515..094e09d4 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EachValueFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/EachValueFormatterTest.php @@ -42,6 +42,7 @@ public function testInvalidRules(EachValue $matcher): void $this->formatter->format($matcher); } + #[TestWith([new EachValue(['value'], [new StringValue("contains single quote '")]), "eachValue(matching(type, 'contains single quote \''))"])] #[TestWith([new EachValue(['value'], [new StringValue('example value')]), "eachValue(matching(type, 'example value'))"])] #[TestWith([new EachValue(new stdClass(), [new Regex('\w \d', 'a 1')]), "eachValue(matching(regex, '\w \d', 'a 1'))"])] public function testFormat(MatcherInterface $matcher, string $expression): void diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MatchAllFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MatchAllFormatterTest.php index 7e265596..d359fbe0 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MatchAllFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MatchAllFormatterTest.php @@ -12,6 +12,7 @@ use PhpPact\Consumer\Matcher\Matchers\MinType; use PhpPact\Consumer\Matcher\Matchers\NullValue; use PhpPact\Consumer\Matcher\Matchers\Regex; +use PhpPact\Consumer\Matcher\Matchers\StringValue; use PhpPact\Consumer\Matcher\Matchers\Type; use PhpPact\Consumer\Matcher\Model\FormatterInterface; use PhpPact\Consumer\Matcher\Model\MatcherInterface; @@ -44,6 +45,7 @@ public function testInvalidMatchers(MatchAll $matcher): void $this->formatter->format($matcher); } + #[TestWith([new MatchAll(['abc' => 'xyz'], [new EachKey(["doesn't matter"], [new StringValue("contains single quote '")]), new EachValue(["doesn't matter"], [new StringValue("contains single quote '")])]), "eachKey(matching(type, 'contains single quote \'')), eachValue(matching(type, 'contains single quote \''))"])] #[TestWith([new MatchAll(['abc' => 1, 'def' => 234], [new MinType(null, 2, false)]), 'atLeast(2)'])] #[TestWith([new MatchAll(['abc' => 1, 'def' => 234], [new MinType(null, 1, false), new MaxType(null, 2, false), new EachKey(["doesn't matter"], [new Regex('\w+', 'abc')]), new EachValue(["doesn't matter"], [new Type(100)])]), "atLeast(1), atMost(2), eachKey(matching(regex, '\w+', 'abc')), eachValue(matching(type, 100))"])] public function testFormat(MatcherInterface $matcher, string $expression): void diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MaxTypeFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MaxTypeFormatterTest.php index 73b2fea2..ad5d06b1 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MaxTypeFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MaxTypeFormatterTest.php @@ -28,6 +28,7 @@ public function testNotSupportedMatcher(): void $this->formatter->format($matcher); } + #[TestWith([new MaxType("contains single quote '", 2), "atMost(2), eachValue(matching(type, 'contains single quote \'')"])] #[TestWith([new MaxType(null, 2, false), 'atMost(2)'])] #[TestWith([new MaxType('example value', 2, false), 'atMost(2)'])] #[TestWith([new MaxType(null, 2), 'atMost(2), eachValue(matching(type, null)'])] diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MinMaxTypeFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MinMaxTypeFormatterTest.php index 5c82daeb..c931b879 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MinMaxTypeFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MinMaxTypeFormatterTest.php @@ -28,6 +28,7 @@ public function testNotSupportedMatcher(): void $this->formatter->format($matcher); } + #[TestWith([new MinMaxType("contains single quote '", 2, 3), "atLeast(2), atMost(3), eachValue(matching(type, 'contains single quote \'')"])] #[TestWith([new MinMaxType(null, 2, 3), 'atLeast(2), atMost(3), eachValue(matching(type, null)'])] #[TestWith([new MinMaxType('example value', 2, 3), "atLeast(2), atMost(3), eachValue(matching(type, 'example value')"])] public function testFormat(MatcherInterface $matcher, string $expression): void diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MinTypeFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MinTypeFormatterTest.php index 7300b7b3..b4d29dcc 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MinTypeFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/Expression/MinTypeFormatterTest.php @@ -28,6 +28,7 @@ public function testNotSupportedMatcher(): void $this->formatter->format($matcher); } + #[TestWith([new MinType("contains single quote '", 1), "atLeast(1), eachValue(matching(type, 'contains single quote \'')"])] #[TestWith([new MinType(null, 1, false), 'atLeast(1)'])] #[TestWith([new MinType('example value', 1, false), 'atLeast(1)'])] #[TestWith([new MinType(null, 1), 'atLeast(1), eachValue(matching(type, null)'])]