Skip to content

Commit

Permalink
Fixes 32 - Method getDateModified of RSS reader doesn't iterate over …
Browse files Browse the repository at this point in the history
…different formats

Signed-off-by: Frank Brückner <[email protected]>
  • Loading branch information
froschdesign committed Dec 20, 2020
1 parent 9d127b9 commit ff28d69
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/Reader/Feed/Rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,22 @@ public function getDateModified()
DateTime::RSS,
DateTime::RFC822,
DateTime::RFC2822,
null,
];
foreach ($dateStandards as $standard) {
try {
$date = DateTime::createFromFormat($standard, $dateModified);
$date = DateTime::createFromFormat(
$standard,
$dateModified
);
if ($date instanceof DateTime) {
break;
} catch (\Exception $e) {
if ($standard === null) {
throw new Exception\RuntimeException(
'Could not load date due to unrecognised format'
. ' (should follow RFC 822 or 2822): ' . $e->getMessage(),
0,
$e
);
}
}
}
if (! $date) {
throw new Exception\RuntimeException(
'Could not load date due to unrecognised'
. ' format (should follow RFC 822 or 2822).'
);
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/Reader/Feed/RssTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
class RssTest extends TestCase
{
/** @var string */
protected $feedSamplePath;

protected $expectedCats = [];
Expand Down Expand Up @@ -2177,6 +2178,22 @@ public function dateModifiedProvider(): array
['/datemodified/plain/none/rss090.xml', null],
];
}

public function testGetDateModifiedShouldThrowExceptionForInvalidDate(): void
{
$feed = Reader\Reader::importString(
file_get_contents(
$this->feedSamplePath . '/datemodified/plain/invalid.xml'
)
);

$this->expectException(Reader\Exception\RuntimeException::class);
$this->expectExceptionMessage(
'Could not load date due to unrecognised format (should follow RFC 822 or 2822).'
);

$feed->getDateModified();
}

/**
* Get Hubs (Unencoded Text)
Expand Down
7 changes: 7 additions & 0 deletions test/Reader/Feed/_files/Rss/datemodified/plain/invalid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<channel>
<!-- missing year -->
<lastBuildDate>Sat, 07 Mar 08:03:50 +0000</lastBuildDate>
</channel>
</rss>

0 comments on commit ff28d69

Please sign in to comment.