|
1
|
|
|
<?php |
|
2
|
|
|
namespace Behapi\Context; |
|
3
|
|
|
|
|
4
|
|
|
use Behat\Behat\Context\Context; |
|
5
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
|
6
|
|
|
|
|
7
|
|
|
use Coduo\PHPMatcher\Factory; |
|
8
|
|
|
use Coduo\PHPMatcher\Factory\SimpleFactory; |
|
9
|
|
|
|
|
10
|
|
|
use Behapi\Tools\HttpHistory; |
|
11
|
|
|
|
|
12
|
|
|
class PhpMatcher implements Context |
|
13
|
|
|
{ |
|
14
|
|
|
use ApiTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** @var Factory */ |
|
17
|
|
|
private $factory; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(HttpHistory $history) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->history = $history; |
|
22
|
|
|
|
|
23
|
|
|
$this->factory = new SimpleFactory; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** @Then the response content should match :pattern */ |
|
27
|
|
View Code Duplication |
public function response_content_should_match(string $pattern) |
|
28
|
|
|
{ |
|
29
|
|
|
$matcher = $this->factory->createMatcher(); |
|
30
|
|
|
$content = (string) $this->getResponse()->getBody(); |
|
31
|
|
|
|
|
32
|
|
|
if ($matcher->match($content, $pattern)) { |
|
33
|
|
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
throw new InvalidArgumentException( |
|
37
|
|
|
sprintf( |
|
38
|
|
|
'The response does not match with the pattern "%s" (error : %s)', |
|
39
|
|
|
$pattern, |
|
40
|
|
|
$matcher->getError() |
|
41
|
|
|
) |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** @Then the response content should not match :pattern */ |
|
46
|
|
View Code Duplication |
public function response_content_should_not_match(string $pattern) |
|
47
|
|
|
{ |
|
48
|
|
|
$matcher = $this->factory->createMatcher(); |
|
49
|
|
|
$content = (string) $this->getResponse()->getBody(); |
|
50
|
|
|
|
|
51
|
|
|
if (!$matcher->match($content, $pattern)) { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
throw new InvalidArgumentException( |
|
56
|
|
|
sprintf( |
|
57
|
|
|
'The response matches with the pattern "%s" (error : %s)', |
|
58
|
|
|
$pattern, |
|
59
|
|
|
$matcher->getError() |
|
60
|
|
|
) |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @Then the response content should match: */ |
|
65
|
|
View Code Duplication |
public function response_content_should_match_PyString(PyStringNode $pattern) |
|
66
|
|
|
{ |
|
67
|
|
|
$matcher = $this->factory->createMatcher(); |
|
68
|
|
|
$content = (string) $this->getResponse()->getBody(); |
|
69
|
|
|
|
|
70
|
|
|
if ($matcher->match($content, $pattern)) { |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
throw new InvalidArgumentException($matcher->getError()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** @Then the response content should match: */ |
|
78
|
|
View Code Duplication |
public function response_content_should_not_match_PyString(PyStringNode $pattern) |
|
79
|
|
|
{ |
|
80
|
|
|
$matcher = $this->factory->createMatcher(); |
|
81
|
|
|
$content = (string) $this->getResponse()->getBody(); |
|
82
|
|
|
|
|
83
|
|
|
if (!$matcher->match($content, $pattern)) { |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
throw new InvalidArgumentException($matcher->getError()); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|