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

PhpMatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
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: */
28 View Code Duplication
    public function response_content_should_match(PyStringNode $pattern)
29
    {
30
        $matcher = $this->factory->createMatcher();
31
        $content = (string) $this->getResponse()->getBody();
32
33
        if ($matcher->match($content, $pattern->getRaw())) {
34
            return;
35
        }
36
37
        throw new InvalidArgumentException($matcher->getError());
38
    }
39
40
    /** @Then the response content should match: */
41 View Code Duplication
    public function response_content_should_not_match(PyStringNode $pattern)
42
    {
43
        $matcher = $this->factory->createMatcher();
44
        $content = (string) $this->getResponse()->getBody();
45
46
        if (!$matcher->match($content, $pattern->getRaw())) {
47
            return;
48
        }
49
50
        throw new InvalidArgumentException(sprintf(
51
            'The response matches with the pattern "%s"',
52
            $pattern->getRaw()
53
        ));
54
    }
55
}
56