1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Muzzle\Assertions; |
4
|
|
|
|
5
|
|
|
use MultipleIterator; |
6
|
|
|
use Muzzle\Messages\Transaction; |
7
|
|
|
use Muzzle\Muzzle; |
8
|
|
|
|
9
|
|
|
class AssertionRules |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
protected static $assertions = [ |
13
|
|
|
ExpectedRequestWasMade::class, |
14
|
|
|
UriPathMatches::class, |
15
|
|
|
MethodMatches::class, |
16
|
|
|
QueryContains::class, |
17
|
|
|
BodyMatches::class, |
18
|
|
|
HeadersMatch::class, |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Muzzle |
23
|
|
|
*/ |
24
|
|
|
private $muzzle; |
25
|
|
|
|
26
|
|
|
public function __construct(Muzzle $muzzle) |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
$this->muzzle = $muzzle; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function new(Muzzle $muzzle) : self |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
return new self($muzzle); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function runAssertions() : void |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
$assertions = $this->build(); |
42
|
|
|
/** |
43
|
|
|
* @var Transaction $actual |
44
|
|
|
* @var Transaction $expected |
45
|
|
|
*/ |
46
|
|
|
foreach ($this->iterator() as [$actual, $expected]) { |
47
|
|
|
foreach ($assertions as $assertion) { |
48
|
|
|
$assertion->assert($actual, $expected); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function push(string $assertion) : void |
54
|
|
|
{ |
55
|
|
|
|
56
|
|
|
static::assertAssertionClass($assertion); |
57
|
|
|
static::$assertions[] = $assertion; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function unshift(string $assertion) : void |
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
static::assertAssertionClass($assertion); |
64
|
|
|
array_unshift(static::$assertions, $assertion); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return Assertion[] |
69
|
|
|
*/ |
70
|
|
|
public static function assertions() : array |
71
|
|
|
{ |
72
|
|
|
|
73
|
|
|
return static::$assertions; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public static function setAssertions(string ...$assertions) : void |
77
|
|
|
{ |
78
|
|
|
|
79
|
|
|
foreach ($assertions as $assertion) { |
80
|
|
|
static::assertAssertionClass($assertion); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
static::$assertions = $assertions; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return Assertion[] |
88
|
|
|
*/ |
89
|
|
|
private function build() : array |
90
|
|
|
{ |
91
|
|
|
|
92
|
|
|
return array_map(function ($assertion) { |
93
|
|
|
|
94
|
|
|
return new $assertion($this->muzzle); |
95
|
|
|
}, static::$assertions); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function iterator() : MultipleIterator |
99
|
|
|
{ |
100
|
|
|
|
101
|
|
|
$iterator = new MultipleIterator(MultipleIterator::MIT_NEED_ANY); |
102
|
|
|
$iterator->attachIterator($this->muzzle->history()->getIterator()); |
103
|
|
|
$iterator->attachIterator($this->muzzle->expectations()->getIterator()); |
104
|
|
|
return $iterator; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private static function assertAssertionClass(string $class) : void |
108
|
|
|
{ |
109
|
|
|
|
110
|
|
|
if (! in_array(Assertion::class, class_implements($class))) { |
111
|
|
|
throw new \InvalidArgumentException('Must be an Assertion.'); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|