Completed
Branch feature/currentUserRefactoring (c13c1d)
by Schlaefer
04:13
created

AssertTrait::assertFlash()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 6
nop 3
dl 0
loc 22
rs 8.6346
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
It seems like assertEquals() 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...
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));
0 ignored issues
show
Bug introduced by
It seems like assertEquals() 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...
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(
0 ignored issues
show
Bug introduced by
It seems like assertEquals() 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...
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)
0 ignored issues
show
Unused Code introduced by
The call to AssertionError::__construct() has too many arguments starting with sprintf('Flash message "...as not set.', $message).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
128
        );
129
    }
130
}
131