Completed
Push — feature/6.x ( db50a0...aa9894 )
by Schlaefer
03:28
created

AssertTrait::_getDOMXPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Saito - The Threaded Web Forum
6
 *
7
 * @copyright Copyright (c) the Saito Project Developers
8
 * @link https://github.com/Schlaefer/Saito
9
 * @license http://opensource.org/licenses/MIT
10
 */
11
12
namespace Saito\Test;
13
14
use AssertionError;
15
use Symfony\Component\DomCrawler\Crawler;
16
17
/**
18
 * Non-depended assumptions
19
 */
20
trait AssertTrait
21
{
22
    /**
23
     * assert contains tag
24
     *
25
     * @param string|array $expected expected
26
     * @param string $result HTML
27
     * @param int $count How often the tag is expected.
28
     * @return void
29
     */
30
    public function assertContainsTag($expected, string $result, int $count = 1): void
31
    {
32
        if (is_string($expected)) {
33
            $expected = [$expected => []];
34
        }
35
36
        do {
37
            $crawler = new Crawler();
38
            $crawler->addHtmlContent($result);
39
            $selector = key($expected);
40
            $node = $crawler->filter($selector);
41
            $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

41
            $this->/** @scrutinizer ignore-call */ 
42
                   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...
42
                $count,
43
                $node->count(),
44
                "Selector '$selector' not found."
45
            );
46
47
            if (isset($expected[$selector]['attributes'])) {
48
                foreach ($expected[$selector]['attributes'] as $attribute => $value) {
49
                    $this->assertEquals($value, $node->attr($attribute));
50
                }
51
            }
52
        } while (next($expected));
53
    }
54
55
    /**
56
     * Assert result does not contain tag
57
     * @param string $selector Tag as CSS selector query
58
     * @param string $result HTML result to check
59
     * @return void
60
     */
61
    public function assertNotContainsTag(string $selector, string $result): void
62
    {
63
        $crawler = new Crawler();
64
        $crawler->addHtmlContent($result);
65
        $node = $crawler->filter($selector);
66
        $this->assertEquals(
67
            0,
68
            $node->count(),
69
            "Selector '$selector' was not expected to be found."
70
        );
71
    }
72
73
    /**
74
     * Assert Flash message was set
75
     *
76
     * @param string $message message
77
     * @param string $element element
78
     * @param bool $debug debugging
79
     * @return void
80
     */
81
    protected function assertFlash(string $message, ?string $element = null, $debug = false): void
82
    {
83
        if ($debug) {
84
            debug($_SESSION['Flash']['flash']);
85
        }
86
        if (!empty($_SESSION['Flash']['flash'])) {
87
            foreach ($_SESSION['Flash']['flash'] as $flash) {
88
                if ($flash['message'] !== $message) {
89
                    continue;
90
                }
91
                if ($element !== null && $flash['element'] !== 'flash/' . $element) {
92
                    continue;
93
                }
94
95
                return;
96
            }
97
        }
98
99
        throw new AssertionError(
100
            sprintf('Flash message "%s" was not set.', $message)
101
        );
102
    }
103
}
104