GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

XmlUtilTest::setUpBeforeClass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
/**
3
 * @link    https://github.com/old-town/old-town-workflow
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace OldTown\Workflow\PhpUnitTest\Loader;
7
8
use OldTown\Workflow\PhpUnit\Test\Paths;
9
use PHPUnit_Framework_TestCase as TestCase;
10
use OldTown\Workflow\Loader\XmlUtil;
11
12
13
/**
14
 * Class XmlUtilTest
15
 *
16
 * @package OldTown\Workflow\PhpUnitTest\Loader
17
 */
18
class XmlUtilTest extends TestCase
19
{
20
    /**
21
     * @var \DOMXPath
22
     */
23
    protected static $xpath;
24
25
    /**
26
     *
27
     * @return void
28
     */
29
    public static function setUpBeforeClass()
30
    {
31
        if (null !== static::$xpath) {
32
            return;
33
        }
34
35
        $path = Paths::getXmlUtilTest() . DIRECTORY_SEPARATOR . 'test.xml';
36
37
38
        $doc = new \DOMDocument();
39
        $doc->load($path);
40
41
42
        static::$xpath = new \DOMXPath($doc);
43
    }
44
45
    /**
46
     * Проверка корректного поиска элементов
47
     */
48
    public function testGetChildElement()
49
    {
50
        /** @var \DOMElement  $elem */
51
        $elem = static::$xpath->query('//testGetChildElement')->item(0);
52
        $elem = XmlUtil::getChildElement($elem, 'element1');
53
54
        static::assertEquals(\DOMElement::class, get_class($elem));
55
56
        static::assertEquals(true, $elem->hasAttribute('level'));
57
        static::assertEquals(1, $elem->getAttribute('level'));
58
59
        static::assertEquals(null, XmlUtil::getChildElement($elem, 'undefined-element'));
0 ignored issues
show
Bug introduced by
It seems like $elem defined by \OldTown\Workflow\Loader...ment($elem, 'element1') on line 52 can be null; however, OldTown\Workflow\Loader\XmlUtil::getChildElement() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
60
    }
61
62
63
    /**
64
     * Проверка функционала поиска обязательного элемента
65
     */
66
    public function testGetRequiredChildElement()
67
    {
68
        /** @var \DOMElement  $elem */
69
        $elem = static::$xpath->query('//testGetRequiredChildElement')->item(0);
70
        $elem = XmlUtil::getRequiredChildElement($elem, 'element1');
71
72
        static::assertEquals(\DOMElement::class, get_class($elem));
73
74
        static::assertEquals(true, $elem->hasAttribute('level'));
75
        static::assertEquals(1, $elem->getAttribute('level'));
76
    }
77
78
    /**
79
     * Проверка функционала поиска обязательного элемента. Проверка ситуации когда элемент не найден
80
     *
81
     * @expectedException \OldTown\Workflow\Exception\NotExistsRequiredElementException
82
     * @expectedExceptionMessage Отсутствует элемен invalid-element
83
     *
84
     */
85
    public function testGetRequiredChildElementException()
86
    {
87
        /** @var \DOMElement  $elem */
88
        $elem = static::$xpath->query('//testGetRequiredChildElement')->item(0);
89
        XmlUtil::getRequiredChildElement($elem, 'invalid-element');
90
    }
91
92
    /**
93
     * Проверка получения нескольких элементов
94
     */
95
    public function testGetChildElements()
96
    {
97
        /** @var \DOMElement  $elem */
98
        $elem = static::$xpath->query('//testGetChildElements')->item(0);
99
        $elements = XmlUtil::getChildElements($elem, 'element1');
100
101
        static::assertEquals(3, count($elements));
102
103
        $attr = [
104
            '1',
105
            '1_1',
106
            '1_2',
107
        ];
108
109
        foreach ($elements as $elem) {
110
            static::assertEquals(true, $elem->hasAttribute('level'));
111
            $value = $elem->getAttribute('level');
112
113
            static::assertEquals(true, in_array($value, $attr, true));
114
        }
115
    }
116
117
118
    /**
119
     * Проверка получения текста
120
     */
121 View Code Duplication
    public function testGetText()
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...
122
    {
123
        /** @var \DOMElement  $elem */
124
        $elem = static::$xpath->query('//testGetText')->item(0);
125
        $text = XmlUtil::getText($elem);
126
127
        $expected = 'test_text_1test_text_2';
128
129
        static::assertEquals($expected, $text);
130
    }
131
132
133
    /**
134
     * Проверка получения текста
135
     */
136 View Code Duplication
    public function testGetChildText()
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...
137
    {
138
        /** @var \DOMElement  $elem */
139
        $elem = static::$xpath->query('//testGetChildText')->item(0);
140
        $text = XmlUtil::getChildText($elem, 'element1');
141
142
        $expected = 'test_text_1test_text_2';
143
144
        static::assertEquals($expected, $text);
145
    }
146
147
    /**
148
     * Проверка получения текста
149
     */
150 View Code Duplication
    public function testGetChildTextNotFoundElement()
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...
151
    {
152
        /** @var \DOMElement  $elem */
153
        $elem = static::$xpath->query('//testGetChildText')->item(0);
154
        $text = XmlUtil::getChildText($elem, 'element3');
155
156
        static::assertEquals(null, $text);
157
    }
158
159
160
    /**
161
     * Проверка получения значения атрибута
162
     */
163 View Code Duplication
    public function testGetRequiredAttributeValue()
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...
164
    {
165
        /** @var \DOMElement  $elem */
166
        $elem = static::$xpath->query('//testGetRequiredAttributeValue/element1')->item(0);
167
        $text = XmlUtil::getRequiredAttributeValue($elem, 'level');
168
169
        static::assertEquals('1', $text);
170
    }
171
172
173
    /**
174
     * Проверка получения значения атрибута
175
     *
176
     * @expectedException \OldTown\Workflow\Exception\NotExistsRequiredAttributeException
177
     */
178
    public function testGetRequiredAttributeValueAttributeNotExists()
179
    {
180
        /** @var \DOMElement  $elem */
181
        $elem = static::$xpath->query('//testGetRequiredAttributeValue/element1')->item(0);
182
        XmlUtil::getRequiredAttributeValue($elem, 'not exists attribute');
183
    }
184
185
186
    /**
187
     * Тест заглушка. Реализовать в дальнейшем
188
     *
189
     */
190
    public function testEncode()
191
    {
192
        $expected = 'test';
193
        static::assertEquals($expected, XmlUtil::encode($expected));
194
    }
195
}
196