Passed
Push — master ( 4ee9ec...849354 )
by San
04:18 queued 03:37
created

BaseContext::assertArrayNotHasKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
1
<?php
2
3
namespace Behatch\Context;
4
5
use Behat\Behat\Context\TranslatableContext;
6
use Behat\MinkExtension\Context\RawMinkContext;
7
use Behat\Mink\Exception\ExpectationException;
8
9
abstract class BaseContext extends RawMinkContext implements TranslatableContext
10
{
11
    public static function getTranslationResources()
12
    {
13
        return glob(__DIR__ . '/../../i18n/*.xliff');
14
    }
15
16
    /**
17
     * @transform /^(0|[1-9]\d*)(?:st|nd|rd|th)?$/
18
     */
19
    public function castToInt($count)
20
    {
21
        return intval($count);
22
    }
23
24
    protected function not(Callable $callbable, $errorMessage)
25
    {
26
        try {
27
            $callbable();
28
        }
29
        catch (\Exception $e) {
30
            return;
31
        }
32
33
        throw new ExpectationException($errorMessage, $this->getSession());
34
    }
35
36
    protected function assert($test, $message)
37
    {
38
        if ($test === false) {
39
            throw new ExpectationException($message, $this->getSession());
40
        }
41
    }
42
43
    protected function assertContains($expected, $actual, $message = null)
44
    {
45
        $regex   = '/' . preg_quote($expected, '/') . '/ui';
46
47
        $this->assert(
48
            preg_match($regex, $actual) > 0,
49
            $message ?: "The string '$expected' was not found."
50
        );
51
    }
52
53
    protected function assertNotContains($expected, $actual, $message = null)
54
    {
55
        $message = $message ?: "The string '$expected' was found.";
56
57
        $this->not(function () use($expected, $actual) {
58
                $this->assertContains($expected, $actual);
59
        }, $message);
60
    }
61
62
    protected function assertCount($expected, array $elements, $message = null)
63
    {
64
        $this->assert(
65
            intval($expected) === count($elements),
66
            $message ?: sprintf('%d elements found, but should be %d.', count($elements), $expected)
67
        );
68
    }
69
70
    protected function assertEquals($expected, $actual, $message = null)
71
    {
72
        $this->assert(
73
            $expected == $actual,
74
            $message ?: "The element '$actual' is not equal to '$expected'"
75
        );
76
    }
77
78
    protected function assertSame($expected, $actual, $message = null)
79
    {
80
        $this->assert(
81
            $expected === $actual,
82
            $message ?: "The element '$actual' is not equal to '$expected'"
83
        );
84
    }
85
86
    protected function assertArrayHasKey($key, $array, $message = null)
87
    {
88
        $this->assert(
89
            isset($array[$key]),
90
            $message ?: "The array has no key '$key'"
91
        );
92
    }
93
94
    protected function assertArrayNotHasKey($key, $array, $message = null)
95
    {
96
        $message = $message ?: "The array has key '$key'";
97
98
        $this->not(function () use($key, $array) {
99
            $this->assertArrayHasKey($key, $array);
100
        }, $message);
101
    }
102
103
    protected function assertTrue($value, $message = 'The value is false')
104
    {
105
        $this->assert($value, $message);
106
    }
107
108
    protected function assertFalse($value, $message = 'The value is true')
109
    {
110
        $this->not(function () use($value) {
111
            $this->assertTrue($value);
112
        }, $message);
113
    }
114
115
    protected function getMinkContext()
116
    {
117
        $context = new \Behat\MinkExtension\Context\MinkContext();
118
        $context->setMink($this->getMink());
119
        $context->setMinkParameters($this->getMinkParameters());
120
121
        return $context;
122
    }
123
124 View Code Duplication
    protected function countElements($element, $index, $parent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126
        $page = $this->getSession()->getPage();
127
128
        $parents = $page->findAll('css', $parent);
129
        if (!isset($parents[$index - 1])) {
130
            throw new \Exception("The $index element '$parent' was not found anywhere in the page");
131
        }
132
133
        $elements = $parents[$index - 1]->findAll('css', $element);
134
135
        return count($elements);
136
    }
137
138 View Code Duplication
    protected function findElement($selector, $locator, $index)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        $page = $this->getSession()->getPage();
141
142
        $nodes = $page->findAll($selector, $locator);
143
144
        if (!isset($nodes[$index - 1])) {
145
            throw new \Exception("The $index $selector '$locator' was not found anywhere in the page");
146
        }
147
148
        return $nodes[$index - 1];
149
    }
150
}
151