Completed
Push — v2 ( e198fb...470733 )
by Joschi
04:46
created

ExtractorTest::testNestedMicroformatsExtraction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 23
rs 9.0856
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * micrometa
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Jkphl\Micrometa\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\Micrometa\Tests\Application;
38
39
use Jkphl\Micrometa\Application\Contract\ParsingResultInterface;
40
use Jkphl\Micrometa\Application\Item\Item;
41
use Jkphl\Micrometa\Application\Service\ExtractorService;
42
use Jkphl\Micrometa\Infrastructure\Factory\DocumentFactory;
43
use Jkphl\Micrometa\Infrastructure\Parser\Microdata;
44
use Jkphl\Micrometa\Infrastructure\Parser\Microformats;
45
use Jkphl\Micrometa\Infrastructure\Parser\RdfaLite;
46
use League\Uri\Schemes\Http;
47
48
/**
49
 * Extractor tests
50
 *
51
 * @package Jkphl\Micrometa
52
 * @subpackage Jkphl\Micrometa\Tests
53
 */
54
class ExtractorTest extends \PHPUnit_Framework_TestCase
55
{
56
    /**
57
     * RDFa Lite 1.1 HTML document
58
     *
59
     * @var string
60
     */
61
    const RDFA_LITE_HTML_URL = 'http://localhost:1349/article-rdfa-lite.html';
62
    /**
63
     * Microdata HTML document
64
     *
65
     * @var string
66
     */
67
    const MICRODATA_HTML_URL = 'http://localhost:1349/article-microdata.html';
68
    /**
69
     * Microformats HTML document
70
     *
71
     * @var string
72
     */
73
    const MICROFORMATS_HTML_URL = 'http://localhost:1349/aggregate.html';
74
    /**
75
     * Microformats tests root path
76
     *
77
     * @var string
78
     */
79
    protected static $microformatsTests;
80
81
    /**
82
     * Setup before all tests
83
     */
84
    public static function setUpBeforeClass()
85
    {
86
        parent::setUpBeforeClass();
87
        self::$microformatsTests = \ComposerLocator::getPath('microformats/test').DIRECTORY_SEPARATOR.'tests'.
1 ignored issue
show
Bug introduced by
The method getPath() does not seem to exist on object<ComposerLocator>.

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...
88
            DIRECTORY_SEPARATOR.'microformats-v2'.DIRECTORY_SEPARATOR;
89
    }
90
91
    /**
92
     * Test the RDFa Lite 1.1 extraction
93
     */
94
    public function testRdfaLiteExtraction()
95
    {
96
        // Create a DOM with RDFa Lite 1.1 markup
97
        $rdfaLite = file_get_contents(
98
            dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'article-rdfa-lite.html'
99
        );
100
        $rdfaLiteDom = DocumentFactory::createFromString($rdfaLite);
101
        $this->assertInstanceOf(\DOMDocument::class, $rdfaLiteDom);
102
103
        // Create an RDFa Lite 1.1 parser
104
        $rdfaLiteUri = Http::createFromString(self::RDFA_LITE_HTML_URL);
105
        $rdfaLiteParser = new RdfaLite($rdfaLiteUri);
106
        $this->assertEquals($rdfaLiteUri, $rdfaLiteParser->getUri());
107
108
        // Create an extractor service
109
        $extractorService = new ExtractorService();
110
        $rdfaLiteItems = $extractorService->extract($rdfaLiteDom, $rdfaLiteParser);
111
        $this->assertInstanceOf(ParsingResultInterface::class, $rdfaLiteItems);
112
        $this->assertEquals(1, count($rdfaLiteItems->getItems()));
113
        $this->assertInstanceOf(Item::class, $rdfaLiteItems->getItems()[0]);
114
        $this->assertEquals(RdfaLite::FORMAT, $rdfaLiteItems->getItems()[0]->getFormat());
115
    }
116
117
    /**
118
     * Test the HTML Microdata extraction
119
     */
120
    public function testMicrodataExtraction()
121
    {
122
        // Create a DOM with HTML Microdata markup
123
        $microdata = file_get_contents(
124
            dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'article-microdata.html'
125
        );
126
        $microdataDom = DocumentFactory::createFromString($microdata);
127
        $this->assertInstanceOf(\DOMDocument::class, $microdataDom);
128
129
        // Create an RDFa Lite 1.1 parser
130
        $microdataUri = Http::createFromString(self::MICRODATA_HTML_URL);
131
        $microdataParser = new Microdata($microdataUri);
132
        $this->assertEquals($microdataUri, $microdataParser->getUri());
133
134
        // Create an extractor service
135
        $extractorService = new ExtractorService();
136
        $microdataItems = $extractorService->extract($microdataDom, $microdataParser);
137
        $this->assertInstanceOf(ParsingResultInterface::class, $microdataItems);
138
        $this->assertEquals(1, count($microdataItems->getItems()));
139
        $this->assertInstanceOf(Item::class, $microdataItems->getItems()[0]);
140
        $this->assertEquals(microdata::FORMAT, $microdataItems->getItems()[0]->getFormat());
141
    }
142
143
    /**
144
     * Test the Microformats extraction
145
     */
146
    public function testMicroformatsExtraction()
147
    {
148
        // Create a DOM with Microformats markup
149
        $microformats = file_get_contents(
150
            self::$microformatsTests.'h-product'.DIRECTORY_SEPARATOR.'aggregate.html'
151
        );
152
        $microformatsDom = DocumentFactory::createFromString($microformats);
153
        $this->assertInstanceOf(\DOMDocument::class, $microformatsDom);
154
155
        // Create a Microformats 2 parser
156
        $microformatsUri = Http::createFromString(self::MICROFORMATS_HTML_URL);
157
        $microformatsParser = new Microformats($microformatsUri);
158
        $this->assertEquals($microformatsUri, $microformatsParser->getUri());
159
160
        // Create an extractor service
161
        $extractorService = new ExtractorService();
162
        $microformatsItems = $extractorService->extract($microformatsDom, $microformatsParser);
163
        $this->assertInstanceOf(ParsingResultInterface::class, $microformatsItems);
164
        $this->assertEquals(1, count($microformatsItems->getItems()));
165
        $this->assertInstanceOf(Item::class, $microformatsItems->getItems()[0]);
166
        $this->assertEquals(Microformats::FORMAT, $microformatsItems->getItems()[0]->getFormat());
167
    }
168
169
    /**
170
     * Test the Microformats extraction
171
     */
172
    public function testNestedMicroformatsExtraction()
173
    {
174
        // Create a DOM with Microformats markup
175
        $microformats = file_get_contents(
176
            dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'nested-events.html'
177
        );
178
        $microformatsDom = DocumentFactory::createFromString($microformats);
179
        $this->assertInstanceOf(\DOMDocument::class, $microformatsDom);
180
181
        // Create a Microformats 2 parser
182
        $microformatsUri = Http::createFromString(self::MICROFORMATS_HTML_URL);
183
        $microformatsParser = new Microformats($microformatsUri);
184
        $this->assertEquals($microformatsUri, $microformatsParser->getUri());
185
186
        // Create an extractor service
187
        $extractorService = new ExtractorService();
188
        $microformatsItems = $extractorService->extract($microformatsDom, $microformatsParser);
189
        $this->assertInstanceOf(ParsingResultInterface::class, $microformatsItems);
190
        $this->assertEquals(1, count($microformatsItems->getItems()));
191
        $this->assertInstanceOf(Item::class, $microformatsItems->getItems()[0]);
192
        $this->assertEquals(Microformats::FORMAT, $microformatsItems->getItems()[0]->getFormat());
193
        $this->assertEquals(2, count($microformatsItems->getItems()[0]->getChildren()));
194
    }
195
}
196