Skip to content

Commit f3a7813

Browse files
Make it work
1 parent 0f7c257 commit f3a7813

File tree

5 files changed

+101
-36
lines changed

5 files changed

+101
-36
lines changed

src/Constraint/HasQueryParameterConstraint.php

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,19 @@
33
namespace Helmich\Psr7Assert\Constraint;
44

55
use PHPUnit\Framework\Constraint\Constraint;
6-
use PHPUnit\Framework\Constraint\IsAnything;
7-
use PHPUnit\Framework\Constraint\IsEqual;
86
use Psr\Http\Message\RequestInterface;
97
use Psr\Http\Message\UriInterface;
108

119
class HasQueryParameterConstraint extends Constraint
1210
{
13-
private $nameMatcher;
14-
private $valueMatcher;
11+
/** @var UrlEncodedMatches */
12+
private $inner;
1513

1614
public function __construct($nameMatcher, $valueMatcher = null)
1715
{
1816
parent::__construct();
1917

20-
if (!($nameMatcher instanceof Constraint)) {
21-
$nameMatcher = new IsEqual($nameMatcher);
22-
}
23-
24-
if ($valueMatcher === null) {
25-
$valueMatcher = new IsAnything();
26-
} else if (!($valueMatcher instanceof Constraint)) {
27-
$valueMatcher = new IsEqual($valueMatcher);
28-
}
29-
30-
$this->nameMatcher = $nameMatcher;
31-
$this->valueMatcher = $valueMatcher;
18+
$this->inner = new UrlEncodedMatches($nameMatcher, $valueMatcher);
3219
}
3320

3421
protected function matches($other): bool
@@ -66,26 +53,12 @@ private function matchesString(string $other): bool
6653

6754
private function matchesQueryString(string $query): bool
6855
{
69-
parse_str($query, $parsedQuery);
70-
71-
foreach ($parsedQuery as $key => $value) {
72-
if (!$this->nameMatcher->evaluate($key, "", true)) {
73-
continue;
74-
}
75-
76-
if (!$this->valueMatcher->evaluate($value, "", true)) {
77-
continue;
78-
}
79-
80-
return true;
81-
}
82-
83-
return false;
56+
return $this->inner->evaluate($query, "", true);
8457
}
8558

8659

8760
public function toString(): string
8861
{
89-
return 'has a query parameter with a name matching ' . $this->nameMatcher->toString() . ' and value matching ' . $this->valueMatcher->toString();
62+
return 'query string ' . $this->inner->toString();
9063
}
9164
}

src/Constraint/UrlEncodedMatches.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
namespace Helmich\Psr7Assert\Constraint;
3+
4+
use PHPUnit\Framework\Constraint\Constraint;
5+
use PHPUnit\Framework\Constraint\IsAnything;
6+
use PHPUnit\Framework\Constraint\IsEqual;
7+
8+
class UrlEncodedMatches extends Constraint
9+
{
10+
private $nameMatcher;
11+
private $valueMatcher;
12+
13+
public function __construct($nameMatcher, $valueMatcher = null)
14+
{
15+
parent::__construct();
16+
17+
if (!($nameMatcher instanceof Constraint)) {
18+
$nameMatcher = new IsEqual($nameMatcher);
19+
}
20+
21+
if ($valueMatcher === null) {
22+
$valueMatcher = new IsAnything();
23+
} else if (!($valueMatcher instanceof Constraint)) {
24+
$valueMatcher = new IsEqual($valueMatcher);
25+
}
26+
27+
$this->nameMatcher = $nameMatcher;
28+
$this->valueMatcher = $valueMatcher;
29+
}
30+
31+
protected function matches($other): bool
32+
{
33+
parse_str($other, $parsedQuery);
34+
35+
foreach ($parsedQuery as $key => $value) {
36+
if (!$this->nameMatcher->evaluate($key, "", true)) {
37+
continue;
38+
}
39+
40+
if (!$this->valueMatcher->evaluate($value, "", true)) {
41+
continue;
42+
}
43+
44+
return true;
45+
}
46+
47+
return false;
48+
}
49+
50+
public function toString(): string
51+
{
52+
return 'contains a name matching ' . $this->nameMatcher->toString() . ' and value matching ' . $this->valueMatcher->toString();
53+
}
54+
55+
56+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace Helmich\Psr7Assert\Constraint;
3+
4+
use PHPUnit\Framework\Constraint\Constraint;
5+
6+
class UrlEncodedMatchesMany extends Constraint
7+
{
8+
/** @var UrlEncodedMatches[] */
9+
private $constraints = [];
10+
11+
public function __construct(array $constraints)
12+
{
13+
parent::__construct();
14+
15+
foreach ($constraints as $key => $value) {
16+
$this->constraints[] = new UrlEncodedMatches($key, $value);
17+
}
18+
}
19+
20+
protected function matches($other): bool
21+
{
22+
foreach ($this->constraints as $constraint) {
23+
if (!$constraint->evaluate($other, "", true)) {
24+
return false;
25+
}
26+
}
27+
28+
return true;
29+
}
30+
31+
32+
public function toString(): string
33+
{
34+
return join(" and ", array_map(function(HasQueryParameterConstraint $c) {
35+
return $c->toString();
36+
}, $this->constraints));
37+
}
38+
}

src/Psr7Assertions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Helmich\Psr7Assert\Constraint\HasStatusConstraint;
1212
use Helmich\Psr7Assert\Constraint\HasUriConstraint;
1313
use Helmich\Psr7Assert\Constraint\IsAbsoluteUriConstraint;
14+
use Helmich\Psr7Assert\Constraint\UrlEncodedMatchesMany;
1415
use PHPUnit\Framework\Assert;
1516
use PHPUnit\Framework\Constraint\Constraint;
1617
use PHPUnit\Framework\Constraint\IsEqual;
@@ -230,7 +231,7 @@ public static function bodyMatchesForm(array $constraints): Constraint
230231
{
231232
return Assert::logicalAnd(
232233
self::hasHeader('content-type', Assert::matchesRegularExpression(',^application/x-www-form-urlencoded(;.+)?$,')),
233-
self::bodyMatches(new HasQueryParametersConstraint($constraints))
234+
self::bodyMatches(new UrlEncodedMatchesMany($constraints))
234235
);
235236
}
236237

tests/Unit/Constraint/HasQueryParametersConstraintTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
declare(strict_types = 1);
33
namespace Helmich\Psr7Assert\Tests\Unit\Constraint;
44

5-
use GuzzleHttp\Psr7\Request;
6-
use Helmich\Psr7Assert\Constraint\HasQueryParameterConstraint;
75
use Helmich\Psr7Assert\Constraint\HasQueryParametersConstraint;
86
use PHPUnit\Framework\TestCase;
9-
use function GuzzleHttp\Psr7\uri_for;
107

118
class HasQueryParametersConstraintTest extends TestCase
129
{

0 commit comments

Comments
 (0)