Completed
Push — master ( 2f5cae...701262 )
by Joschi
03:21
created

DOMNodeIteratorTest::iterateDom()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 3
1
<?php
2
3
/**
4
 * rdfa-lite-microdata
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\RdfaLiteMicrodata
8
 * @subpackage Jkphl\RdfaLiteMicrodata\Tests
9
 * @author Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\RdfaLiteMicrodata\Tests\Application;
38
39
use Jkphl\RdfaLiteMicrodata\Application\Contract\ElementProcessorInterface;
40
use Jkphl\RdfaLiteMicrodata\Application\Parser\Context;
41
use Jkphl\RdfaLiteMicrodata\Application\Parser\DOMIterator;
42
use Jkphl\RdfaLiteMicrodata\Infrastructure\Parser\RdfaLiteElementProcessor;
43
44
/**
45
 * DOM node iterator tests
46
 *
47
 * @package Jkphl\RdfaLiteMicrodata
48
 * @subpackage Jkphl\RdfaLiteMicrodata\Tests
49
 */
50
class DOMNodeIteratorTest extends ParserIteratorTestBase
51
{
52
    /**
53
     * Context
54
     *
55
     * @var Context
56
     */
57
    protected $context;
58
    /**
59
     * Element processor mock
60
     *
61
     * @var ElementProcessorInterface
62
     */
63
    protected $elementProcessor;
64
65
    /**
66
     * Test recursive DOM node iteration
67
     */
68
    public function testDomNodeIteration()
69
    {
70
        $dom = new \DOMDocument();
71
        $dom->loadHTML(self::$personRdfa);
72
        $context = new Context();
73
        $elementProcessor = $this->getMock(ElementProcessorInterface::class);
74
        $elementProcessor->method('processElement')->willReturn($context);
75
        $elementProcessor->method('processElementChildren')->willReturn($context);
76
        /** @var $elementProcessor ElementProcessorInterface */
77
        $this->iterateDom($dom, $context, $elementProcessor);
78
    }
79
80
    /**
81
     * Iterate through the dom
82
     *
83
     * @param \DOMDocument $dom DOM
84
     * @param Context $context Context
85
     * @param ElementProcessorInterface $elementProcessor Element processor
86
     */
87
    protected function iterateDom(\DOMDocument $dom, Context $context, ElementProcessorInterface $elementProcessor)
88
    {
89
        $domNodeIterator = new DOMIterator($dom->childNodes, $context, $elementProcessor);
90
        $this->assertInstanceOf(DOMIterator::class, $domNodeIterator);
91
92
        $elements = ['html', 'head', 'title', 'body', 'h1', 'p', 'span', 'span', 'img', 'span'];
93
94
        /**
95
         * Recursively run through all child elements
96
         *
97
         * @var int $nodeIndex Element index
98
         * @var \DOMElement $node Element
99
         */
100
        foreach ($domNodeIterator->getRecursiveIterator() as $element) {
101
            if ($element instanceof \DOMElement) {
102
                $this->assertEquals($element->localName, array_shift($elements));
103
            }
104
        }
105
        $this->assertEquals(0, count($elements));
106
    }
107
108
    /**
109
     * Test the RDFa Lite element processor
110
     */
111
    public function testRdfaLiteProcessor()
112
    {
113
        $dom = new \DOMDocument();
114
        $dom->loadHTML(self::$personRdfa);
115
        $context = new Context();
116
        $this->iterateDom($dom, $context, new RdfaLiteElementProcessor());
117
        $this->validatePersonResult($context->getChildren());
118
    }
119
}
120