Completed
Branch feature/currentUserRefactoring (c13c1d)
by Schlaefer
09:08
created

AssertTrait::assertFlash()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 11
nc 6
nop 3
dl 0
loc 20
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Saito\Test;
14
15
use AssertionError;
16
use Symfony\Component\DomCrawler\Crawler;
17
18
/**
19
 * Non-depended assumptions
20
 */
21
trait AssertTrait
22
{
23
    /**
24
     * assert contains tag
25
     *
26
     * @param array $expected expected
27
     * @param string $result HTML
28
     * @return void
29
     */
30
    public function assertContainsTag($expected, $result)
31
    {
32
        do {
33
            $crawler = new Crawler;
34
            $crawler->addHtmlContent($result);
35
            $selector = key($expected);
36
            $node = $crawler->filter($selector);
37
            $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not exist on Saito\Test\AssertTrait. Did you maybe mean assertFlash()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
            $this->/** @scrutinizer ignore-call */ 
38
                   assertEquals(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38
                1,
39
                $node->count(),
40
                "Selector '$selector' not found."
41
            );
42
43
            if (isset($expected[$selector]['attributes'])) {
44
                foreach ($expected[$selector]['attributes'] as $attribute => $value) {
45
                    $this->assertEquals($value, $node->attr($attribute));
46
                }
47
            }
48
        } while (next($expected));
49
    }
50
51
    /**
52
     * tests if XPath exists in HTML Source
53
     *
54
     * @param string $html HTML
55
     * @param string $path XPath
56
     * @param int $count how many times should XPath exist in HTML
57
     * @return mixed
58
     */
59
    public function assertXPath($html, $path, $count = 1)
60
    {
61
        $xpath = $this->_getDOMXPath($html);
62
        $length = $xpath->query($path)->length;
63
64
        return $this->assertEquals(
65
            $count,
66
            $length,
67
            "Failed XPath. Expected '$path' to be found $count times instead of $length."
68
        );
69
    }
70
71
    /**
72
     * assert not xpath
73
     *
74
     * @param string $html path
75
     * @param string $path path
76
     * @return bool
77
     */
78
    public function assertNotXPath($html, $path)
79
    {
80
        return !$this->assertXPath($html, $path, 0);
81
    }
82
83
    /**
84
     * get dom xpath
85
     *
86
     * @param string $html HTML
87
     * @return \DOMXPath
88
     */
89
    protected function _getDOMXPath($html)
90
    {
91
        $document = new \DOMDocument;
92
        libxml_use_internal_errors(true);
93
        $document->loadHTML('<!DOCTYPE html>' . $html);
94
        $xpath = new \DOMXPath($document);
95
        libxml_clear_errors();
96
97
        return $xpath;
98
    }
99
100
    /**
101
     * Assert Flash message was set
102
     *
103
     * @param string $message message
104
     * @param string $element element
105
     * @param bool $debug debugging
106
     * @return void
107
     */
108
    protected function assertFlash(string $message, string $element = null, $debug = false): void
109
    {
110
        if ($debug) {
111
            debug($_SESSION['Flash']['flash']);
112
        }
113
        if (!empty($_SESSION['Flash']['flash'])) {
114
            foreach ($_SESSION['Flash']['flash'] as $flash) {
115
                if ($flash['message'] !== $message) {
116
                    continue;
117
                }
118
                if ($element !== null && $flash['element'] !== 'Flash/' . $element) {
119
                    continue;
120
                }
121
122
                return;
123
            }
124
        }
125
126
        throw new AssertionError(
127
            sprintf('Flash message "%s" was not set.', $message)
128
        );
129
    }
130
}
131