Skip to content

Commit

Permalink
feat: Support string contains single quote in matching expression
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Oct 2, 2024
1 parent cb1b59a commit ef7e9f3
Show file tree
Hide file tree
Showing 11 changed files with 15 additions and 85 deletions.
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,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', '<?xml?><test/>'), 'matching(contentType, \'application/xml\', \'<?xml?><test/>\')'])]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\')'])]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)'])]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)'])]
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 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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)'])]
Expand Down

0 comments on commit ef7e9f3

Please sign in to comment.