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 string contains single quote in matching expression #675

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,11 @@ 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('plain/text'), 'matching(contentType, \'plain/text\', \'\')'])]
#[TestWith([new ContentType('application/json', '{"key":"value"}'), 'matching(contentType, \'application/json\', \'{"key":"value"}\')'])]
#[TestWith([new ContentType('application/xml', '<?xml?><test/>'), 'matching(contentType, \'application/xml\', \'<?xml?><test/>\')'])]
#[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', '<?xml?><test/>'), "matching(contentType, 'application/xml', '<?xml?><test/>')"])]
public function testFormat(MatcherInterface $matcher, string $expression): void
{
$result = $this->formatter->format($matcher);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,11 @@ 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 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,8 +43,9 @@ 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\'))'])]
#[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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ 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("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
{
$result = $this->formatter->format($matcher);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,8 @@ 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('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)'])]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,8 @@ 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('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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,8 +45,9 @@ 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))'])]
#[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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,8 @@ 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('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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ 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)'])]
#[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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ 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\')'])]
#[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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ 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)'])]
#[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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,8 @@ 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('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)'])]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,9 @@ 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('\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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,8 @@ 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('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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,8 @@ 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('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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,8 @@ 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('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)'])]
Expand Down