Completed
Branch master (6c7c3c)
by Joschi
02:33
created

ExtractorTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
eloc 4
nc 1
nop 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\Domfactory\Ports\Dom;
40
use Jkphl\Micrometa\Application\Contract\ParsingResultInterface;
41
use Jkphl\Micrometa\Application\Item\Item;
42
use Jkphl\Micrometa\Application\Service\ExtractorService;
43
use Jkphl\Micrometa\Infrastructure\Logger\ExceptionLogger;
44
use Jkphl\Micrometa\Infrastructure\Parser\Microdata;
45
use Jkphl\Micrometa\Infrastructure\Parser\Microformats;
46
use Jkphl\Micrometa\Infrastructure\Parser\RdfaLite;
47
use Jkphl\Micrometa\Tests\AbstractTestBase;
48
use League\Uri\Schemes\Http;
49
50
/**
51
 * Extractor tests
52
 *
53
 * @package Jkphl\Micrometa
54
 * @subpackage Jkphl\Micrometa\Tests
55
 */
56
class ExtractorTest extends AbstractTestBase
57
{
58
    /**
59
     * Microformats tests root path
60
     *
61
     * @var string
62
     */
63
    protected static $microformatsTests;
64
65
    /**
66
     * Setup before all tests
67
     */
68
    public static function setUpBeforeClass()
69
    {
70
        parent::setUpBeforeClass();
71
        self::$microformatsTests = \ComposerLocator::getPath('mf2/tests').DIRECTORY_SEPARATOR.'tests'.
72
            DIRECTORY_SEPARATOR.'microformats-v2'.DIRECTORY_SEPARATOR;
73
    }
74
75
    /**
76
     * Test the RDFa Lite 1.1 extraction
77
     */
78
    public function testRdfaLiteExtraction()
79
    {
80
        // Create a DOM with RDFa Lite 1.1 markup
81
        list($rdfaLiteUri, $rdfaLiteDom) = $this->getUriFixture('rdfa-lite/article-rdfa-lite.html');
82
        $this->assertInstanceOf(\DOMDocument::class, $rdfaLiteDom);
83
84
        // Create an RDFa Lite 1.1 parser
85
        $rdfaLiteParser = new RdfaLite($rdfaLiteUri, self::$logger);
86
        $this->assertEquals($rdfaLiteUri, $rdfaLiteParser->getUri());
87
88
        // Create an extractor service
89
        $extractorService = new ExtractorService();
90
        $rdfaLiteItems = $extractorService->extract($rdfaLiteDom, $rdfaLiteParser);
91
        $this->assertInstanceOf(ParsingResultInterface::class, $rdfaLiteItems);
92
        $this->assertEquals(1, count($rdfaLiteItems->getItems()));
93
        $this->assertInstanceOf(Item::class, $rdfaLiteItems->getItems()[0]);
94
        $this->assertEquals(RdfaLite::FORMAT, $rdfaLiteItems->getItems()[0]->getFormat());
95
    }
96
97
    /**
98
     * Test the HTML Microdata extraction
99
     */
100
    public function testMicrodataExtraction()
101
    {
102
        // Create a DOM with HTML Microdata markup
103
        list($microdataUri, $microdataDom) = $this->getUriFixture('html-microdata/article-microdata.html');
104
        $this->assertInstanceOf(\DOMDocument::class, $microdataDom);
105
106
        // Create an HTML microdata parser
107
        $microdataParser = new Microdata($microdataUri, new ExceptionLogger());
108
        $this->assertEquals($microdataUri, $microdataParser->getUri());
109
110
        // Create an extractor service
111
        $extractorService = new ExtractorService();
112
        $microdataItems = $extractorService->extract($microdataDom, $microdataParser);
113
        $this->assertInstanceOf(ParsingResultInterface::class, $microdataItems);
114
        $this->assertEquals(1, count($microdataItems->getItems()));
115
        $this->assertInstanceOf(Item::class, $microdataItems->getItems()[0]);
116
        $this->assertEquals(microdata::FORMAT, $microdataItems->getItems()[0]->getFormat());
117
    }
118
119
    /**
120
     * Test the Microformats extraction
121
     */
122
    public function testMicroformatsExtraction()
123
    {
124
        $this->getAndTestMicroformatsExtractionBase(
125
            self::$microformatsTests.'h-product'.DIRECTORY_SEPARATOR.'aggregate.html'
126
        );
127
    }
128
129
    /**
130
     * Test the Microformats extraction
131
     */
132
    public function testNestedMicroformatsExtraction()
133
    {
134
        $microformatsItems = $this->getAndTestMicroformatsExtractionBase(
135
            dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.
136
            'microformats'.DIRECTORY_SEPARATOR.'nested-events.html'
137
        );
138
        $this->assertEquals(2, count($microformatsItems->getItems()[0]->getChildren()));
139
    }
140
141
    /**
142
     * Run a microformats base test on a file and return the items
143
     *
144
     * @param string $file File name
145
     * @return ParsingResultInterface
146
     */
147
    protected function getAndTestMicroformatsExtractionBase($file)
148
    {
149
        // Create a DOM with Microformats markup
150
        $microformats = file_get_contents($file);
151
        $microformatsDom = Dom::createFromString($microformats);
152
        $this->assertInstanceOf(\DOMDocument::class, $microformatsDom);
153
154
        // Create a Microformats 2 parser
155
        $microformatsUri = Http::createFromString('http://localhost:1349/aggregate.html');
156
        $microformatsParser = new Microformats($microformatsUri, new ExceptionLogger());
157
        $this->assertEquals($microformatsUri, $microformatsParser->getUri());
158
159
        // Create and run an extractor service
160
        $extractorService = new ExtractorService();
161
        $microformatsItems = $extractorService->extract($microformatsDom, $microformatsParser);
162
        $this->assertInstanceOf(ParsingResultInterface::class, $microformatsItems);
163
        $this->assertEquals(1, count($microformatsItems->getItems()));
164
        $this->assertInstanceOf(Item::class, $microformatsItems->getItems()[0]);
165
        $this->assertEquals(Microformats::FORMAT, $microformatsItems->getItems()[0]->getFormat());
166
167
        return $microformatsItems;
168
    }
169
}
170