Passed
Pull Request — master (#32)
by Baptiste
02:27
created

PhpMatcher::response_content_should_match()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Importance

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