Test Setup Failed
Pull Request — master (#249)
by benjamin
02:29
created

Asserter::assertMatch()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 3
1
<?php
2
3
namespace Behatch;
4
5
use Behat\Mink\Exception\ExpectationException;
6
use Coduo\PHPMatcher\PHPMatcher;
7
8
trait Asserter
9
{
10
    protected function not(callable $callbable, $errorMessage)
11
    {
12
        try {
13
            $callbable();
14
        }
15
        catch (\Exception $e) {
16
            return;
17
        }
18
19
        throw new ExpectationException($errorMessage, $this->getSession()->getDriver());
0 ignored issues
show
Bug introduced by
It seems like getSession() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
20
    }
21
22
    protected function assert($test, $message)
23
    {
24
        if ($test === false) {
25
            throw new ExpectationException($message, $this->getSession()->getDriver());
0 ignored issues
show
Bug introduced by
It seems like getSession() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
26
        }
27
    }
28
29
    protected function assertContains($expected, $actual, $message = null)
30
    {
31
        $regex   = '/' . preg_quote($expected, '/') . '/ui';
32
33
        $this->assert(
34
            preg_match($regex, $actual) > 0,
35
            $message ?: "The string '$expected' was not found."
36
        );
37
    }
38
39
    protected function assertNotContains($expected, $actual, $message = null)
40
    {
41
        $message = $message ?: "The string '$expected' was found.";
42
43
        $this->not(function () use($expected, $actual) {
44
                $this->assertContains($expected, $actual);
45
        }, $message);
46
    }
47
48
    protected function assertCount($expected, array $elements, $message = null)
49
    {
50
        $this->assert(
51
            intval($expected) === count($elements),
52
            $message ?: sprintf('%d elements found, but should be %d.', count($elements), $expected)
53
        );
54
    }
55
56
    protected function assertEquals($expected, $actual, $message = null)
57
    {
58
        $this->assert(
59
            $expected == $actual,
60
            $message ?: "The element '$actual' is not equal to '$expected'"
61
        );
62
    }
63
64
    protected function assertSame($expected, $actual, $message = null)
65
    {
66
        $this->assert(
67
            $expected === $actual,
68
            $message ?: "The element '$actual' is not equal to '$expected'"
69
        );
70
    }
71
72
    protected function assertMatch($pattern, $actual, $message = null)
73
    {
74
        if (version_compare(PHP_VERSION, '7.0.0', '<')) {
75
            throw new PHPVersionNotSupported();
76
        }
77
78
        $this->assert(
79
            PHPMatcher::match($actual, $pattern, $error),
80
            $message ?: "The element '$actual' do not match '$pattern'"
81
        );
82
    }
83
84
    protected function assertArrayHasKey($key, $array, $message = null)
85
    {
86
        $this->assert(
87
            isset($array[$key]),
88
            $message ?: "The array has no key '$key'"
89
        );
90
    }
91
92
    protected function assertArrayNotHasKey($key, $array, $message = null)
93
    {
94
        $message = $message ?: "The array has key '$key'";
95
96
        $this->not(function () use($key, $array) {
97
            $this->assertArrayHasKey($key, $array);
98
        }, $message);
99
    }
100
101
    protected function assertTrue($value, $message = 'The value is false')
102
    {
103
        $this->assert($value, $message);
104
    }
105
106
    protected function assertFalse($value, $message = 'The value is true')
107
    {
108
        $this->not(function () use($value) {
109
            $this->assertTrue($value);
110
        }, $message);
111
    }
112
}
113