Skip to content

Commit

Permalink
extract item parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanlaak committed Oct 24, 2019
1 parent 32ec32c commit a912990
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/Micrometa/Tests/AbstractTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ abstract class AbstractTestBase extends TestCase
*/
private static $logger;

protected static function getLogger() : LoggerInterface
protected static function getLogger(int $threshold = 400) : LoggerInterface
{
return self::$logger ?? self::$logger = new ExceptionLogger();
return self::$logger[$threshold] ?? self::$logger[$threshold] = new ExceptionLogger($threshold);
}

/**
Expand Down
41 changes: 15 additions & 26 deletions src/Micrometa/Tests/Infrastructure/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ class ParserTest extends AbstractTestBase
*/
public function testLanguageJsonLDParser()
{
list($uri, $dom) = $this->getUriFixture('json-ld/jsonld-languages.html');
$parser = new JsonLD($uri, self::getLogger());
$items = $parser->parseDom($dom)->getItems();
$items = $this->parseItems('json-ld/jsonld-languages.html', JsonLD::class);
$this->assertTrue(is_array($items));
$this->assertEquals(1, count($items));
$this->assertInstanceOf(Item::class, $items[0]);
Expand All @@ -84,9 +82,7 @@ public function testLanguageJsonLDParser()
*/
public function testMultipleJsonLDParser()
{
list($uri, $dom) = $this->getUriFixture('json-ld/jsonld-examples.html');
$parser = new JsonLD($uri, new ExceptionLogger(0));
$items = $parser->parseDom($dom)->getItems();
$items = $this->parseItems('json-ld/jsonld-examples.html', JsonLD::class, 0);
$this->assertTrue(is_array($items));
$this->assertEquals(5, count($items));
$this->assertInstanceOf(Item::class, $items[0]);
Expand All @@ -99,9 +95,7 @@ public function testMultipleJsonLDParser()
*/
public function testInvalidJsonLDParser()
{
list($uri, $dom) = $this->getUriFixture('json-ld/jsonld-invalid.html');
$parser = new JsonLD($uri, new ExceptionLogger(0));
$items = $parser->parseDom($dom)->getItems();
$items = $this->parseItems('json-ld/jsonld-invalid.html', JsonLD::class, 0);
$this->assertTrue(is_array($items));
$this->assertEquals(0, count($items));
}
Expand All @@ -111,9 +105,7 @@ public function testInvalidJsonLDParser()
*/
public function testMicroformatsParser()
{
list($uri, $dom) = $this->getUriFixture('microformats/entry.html');
$parser = new Microformats($uri, self::getLogger());
$items = $parser->parseDom($dom)->getItems();
$items = $this->parseItems('microformats/entry.html', Microformats::class);
$this->assertTrue(is_array($items));
$this->assertEquals(1, count($items));
$this->assertInstanceOf(Item::class, $items[0]);
Expand All @@ -125,9 +117,7 @@ public function testMicroformatsParser()
*/
public function testNestedMicroformatsParser()
{
list($uri, $dom) = $this->getUriFixture('microformats/nested-events.html');
$parser = new Microformats($uri, self::getLogger());
$items = $parser->parseDom($dom)->getItems();
$items = $this->parseItems('microformats/nested-events.html', Microformats::class);
$this->assertTrue(is_array($items));
$this->assertEquals(1, count($items));
$this->assertInstanceOf(Item::class, $items[0]);
Expand All @@ -140,10 +130,7 @@ public function testNestedMicroformatsParser()
*/
public function testMicrodataParser()
{
list($uri, $dom) = $this->getUriFixture('html-microdata/article-microdata.html');
$parser = new Microdata($uri, self::getLogger());
$items = $parser->parseDom($dom)->getItems();

$items = $this->parseItems('html-microdata/article-microdata.html', Microdata::class);
$expectedItemFormat = Microdata::FORMAT;
$expectedItemIri = new Iri('http://schema.org/', 'NewsArticle');
$this->assertItemParsedAs($items, $expectedItemFormat, $expectedItemIri);
Expand All @@ -154,10 +141,7 @@ public function testMicrodataParser()
*/
public function testRdfaLiteParser()
{
list($uri, $dom) = $this->getUriFixture('rdfa-lite/article-rdfa-lite.html');
$parser = new RdfaLite($uri, self::getLogger());
$items = $parser->parseDom($dom)->getItems();

$items = $this->parseItems('rdfa-lite/article-rdfa-lite.html', RdfaLite::class);
$expectedItemFormat = RdfaLite::FORMAT;
$expectedItemIri = new Iri('http://schema.org/', 'NewsArticle');
$this->assertItemParsedAs($items, $expectedItemFormat, $expectedItemIri);
Expand All @@ -168,9 +152,7 @@ public function testRdfaLiteParser()
*/
public function testLinkTypeParser()
{
list($uri, $dom) = $this->getUriFixture('link-type/valid-test.html');
$parser = new LinkType($uri, self::getLogger());
$items = $parser->parseDom($dom)->getItems();
$items = $this->parseItems('link-type/valid-test.html', LinkType::class);
$this->assertTrue(is_array($items));
$this->assertEquals(4, count($items));
$this->assertInstanceOf(Item::class, $items[0]);
Expand All @@ -186,4 +168,11 @@ private function assertItemParsedAs(array $items, int $expectedItemFormat, Iri $
$this->assertEquals($expectedItemFormat, $items[0]->getFormat());
$this->assertEquals([$expectedItemIri], $items[0]->getType());
}

private function parseItems(string $fixture, string $parser, int $errorThreshold = 400)
{
list($uri, $dom) = $this->getUriFixture($fixture);
$parser = new $parser($uri, self::getLogger($errorThreshold));
return $parser->parseDom($dom)->getItems();
}
}

0 comments on commit a912990

Please sign in to comment.