From 2136705340b8b4bb0f5f0ec8ad5bc52cdc91a4e7 Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Mon, 6 Jan 2020 23:07:32 +0100 Subject: [PATCH 1/3] Add fixer to set last modified from http last modified header --- src/FeedIo/FeedIo.php | 3 +- src/FeedIo/Reader/Fixer/HttpLastModified.php | 35 +++++++++++ src/FeedIo/Reader/Fixer/LastModified.php | 7 ++- src/FeedIo/Reader/Fixer/PublicId.php | 8 ++- src/FeedIo/Reader/FixerAbstract.php | 4 +- src/FeedIo/Reader/FixerSet.php | 6 +- .../Reader/Fixer/HttpLastModifiedTest.php | 58 +++++++++++++++++++ .../FeedIo/Reader/Fixer/LastModifiedTest.php | 23 +++++++- tests/FeedIo/Reader/Fixer/PublicIdTest.php | 23 +++++++- tests/FeedIo/Reader/FixerMock.php | 5 +- tests/FeedIo/Reader/FixerSetTest.php | 20 ++++++- 11 files changed, 171 insertions(+), 21 deletions(-) create mode 100644 src/FeedIo/Reader/Fixer/HttpLastModified.php create mode 100644 tests/FeedIo/Reader/Fixer/HttpLastModifiedTest.php diff --git a/src/FeedIo/FeedIo.php b/src/FeedIo/FeedIo.php index 79bdeb96..a605ef70 100644 --- a/src/FeedIo/FeedIo.php +++ b/src/FeedIo/FeedIo.php @@ -232,6 +232,7 @@ public function addFixer(FixerAbstract $fixer) : FeedIo public function getBaseFixers() : array { return array( + new Reader\Fixer\HttpLastModified(), new Reader\Fixer\LastModified(), new Reader\Fixer\PublicId(), ); @@ -320,7 +321,7 @@ public function read(string $url, FeedInterface $feed = null, \DateTime $modifie $this->logAction($feed, "read access : $url into a feed instance"); $result = $this->reader->read($url, $feed, $modifiedSince); - $this->fixerSet->correct($result->getFeed()); + $this->fixerSet->correct($result); return $result; } diff --git a/src/FeedIo/Reader/Fixer/HttpLastModified.php b/src/FeedIo/Reader/Fixer/HttpLastModified.php new file mode 100644 index 00000000..2e16f567 --- /dev/null +++ b/src/FeedIo/Reader/Fixer/HttpLastModified.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FeedIo\Reader\Fixer; + +use FeedIo\Reader\FixerAbstract; +use FeedIo\Reader\Result; + +class HttpLastModified extends FixerAbstract +{ + + /** + * @param Result $result + * @return FixerAbstract + */ + public function correct(Result $result): FixerAbstract + { + $feed = $result->getFeed(); + $response = $result->getResponse(); + + if ($response->getLastModified() instanceof \DateTime) { + $this->logger->debug("found last modified: " . $response->getLastModified()->format(\DateTime::RSS)); + $feed->setLastModified($response->getLastModified()); + } + + return $this; + } +} diff --git a/src/FeedIo/Reader/Fixer/LastModified.php b/src/FeedIo/Reader/Fixer/LastModified.php index 2a10dcff..ab2caa51 100644 --- a/src/FeedIo/Reader/Fixer/LastModified.php +++ b/src/FeedIo/Reader/Fixer/LastModified.php @@ -12,17 +12,20 @@ use FeedIo\FeedInterface; use FeedIo\Reader\FixerAbstract; +use FeedIo\Reader\Result; class LastModified extends FixerAbstract { /** - * @param FeedInterface $feed + * @param Result $result * @return FixerAbstract */ - public function correct(FeedInterface $feed) : FixerAbstract + public function correct(Result $result) : FixerAbstract { + $feed = $result->getFeed(); $date = new \DateTime('@0'); + if (is_null($feed->getLastModified()) || $feed->getLastModified() == $date) { $this->logger->notice("correct last modified date for feed {$feed->getTitle()}"); $feed->setLastModified( diff --git a/src/FeedIo/Reader/Fixer/PublicId.php b/src/FeedIo/Reader/Fixer/PublicId.php index 3072d37d..9db9d5b0 100644 --- a/src/FeedIo/Reader/Fixer/PublicId.php +++ b/src/FeedIo/Reader/Fixer/PublicId.php @@ -13,18 +13,20 @@ use FeedIo\FeedInterface; use FeedIo\Feed\NodeInterface; use FeedIo\Reader\FixerAbstract; +use FeedIo\Reader\Result; class PublicId extends FixerAbstract { /** - * @param FeedInterface $feed + * @param Result $result * @return $this */ - public function correct(FeedInterface $feed) : FixerAbstract + public function correct(Result $result) : FixerAbstract { - $this->fixNode($feed); + $feed = $result->getFeed(); + $this->fixNode($feed); $this->fixItems($feed); return $this; diff --git a/src/FeedIo/Reader/FixerAbstract.php b/src/FeedIo/Reader/FixerAbstract.php index 0347506f..90d1b6bb 100644 --- a/src/FeedIo/Reader/FixerAbstract.php +++ b/src/FeedIo/Reader/FixerAbstract.php @@ -33,8 +33,8 @@ public function setLogger(LoggerInterface $logger) : FixerAbstract } /** - * @param FeedInterface $feed + * @param Result $result * @return FixerAbstract */ - abstract public function correct(FeedInterface $feed) : FixerAbstract; + abstract public function correct(Result $result) : FixerAbstract; } diff --git a/src/FeedIo/Reader/FixerSet.php b/src/FeedIo/Reader/FixerSet.php index b25d77b0..1f766bc9 100644 --- a/src/FeedIo/Reader/FixerSet.php +++ b/src/FeedIo/Reader/FixerSet.php @@ -28,13 +28,13 @@ public function add(FixerAbstract $fixer) : FixerSet } /** - * @param FeedInterface $feed + * @param Result $result * @return FixerSet */ - public function correct(FeedInterface $feed) : FixerSet + public function correct(Result $result) : FixerSet { foreach ($this->fixers as $fixer) { - $fixer->correct($feed); + $fixer->correct($result); } return $this; diff --git a/tests/FeedIo/Reader/Fixer/HttpLastModifiedTest.php b/tests/FeedIo/Reader/Fixer/HttpLastModifiedTest.php new file mode 100644 index 00000000..615aa906 --- /dev/null +++ b/tests/FeedIo/Reader/Fixer/HttpLastModifiedTest.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FeedIo\Reader\Fixer; + +use FeedIo\Adapter\NullResponse; +use FeedIo\Adapter\ResponseInterface; +use FeedIo\Feed; +use FeedIo\Reader\Document; +use FeedIo\Reader\Result; +use Psr\Log\NullLogger; + +use \PHPUnit\Framework\TestCase; + +class HttpLastModifiedTest extends TestCase +{ + + /** + * @var HttpLastModified + */ + protected $object; + + protected function setUp() + { + $this->object = new HttpLastModified(); + $this->object->setLogger(new NullLogger()); + } + + public function testCorrect() + { + $result = $this->getResultMock(); + $feed = $result->getFeed(); + + $this->assertNull($feed->getLastModified()); + $this->object->correct($result); + + $this->assertEquals(new \DateTime('@0'), $feed->getLastModified()); + } + + protected function getResultMock(): Result + { + /** @var Document $document */ + $document = $this->createMock(Document::class); + /** @var Feed $feed */ + $feed = new Feed(); + /** @var ResponseInterface $response */ + $response = new NullResponse(); + + return new Result($document, $feed, new \DateTime('@0'), $response, 'http://localhost/test.rss'); + } +} diff --git a/tests/FeedIo/Reader/Fixer/LastModifiedTest.php b/tests/FeedIo/Reader/Fixer/LastModifiedTest.php index 1c2f7a0d..4dfb78af 100644 --- a/tests/FeedIo/Reader/Fixer/LastModifiedTest.php +++ b/tests/FeedIo/Reader/Fixer/LastModifiedTest.php @@ -10,8 +10,12 @@ namespace FeedIo\Reader\Fixer; +use FeedIo\Adapter\NullResponse; +use FeedIo\Adapter\ResponseInterface; use FeedIo\Feed; use FeedIo\Feed\Item; +use FeedIo\Reader\Document; +use FeedIo\Reader\Result; use Psr\Log\NullLogger; use \PHPUnit\Framework\TestCase; @@ -20,7 +24,7 @@ class LastModifiedTest extends TestCase { /** - * @var FeedIo\Reader\Fixer\LastModified + * @var LastModified */ protected $object; @@ -49,10 +53,11 @@ public function testSearchLastModified() public function testCorrect() { - $feed = $this->getFeed(); + $result = $this->getResultMock(); + $feed = $result->getFeed(); $this->assertNull($feed->getLastModified()); - $this->object->correct($feed); + $this->object->correct($result); $this->assertEquals($this->newest, $feed->getLastModified()); } @@ -70,4 +75,16 @@ protected function getFeed() return $feed; } + + protected function getResultMock(): Result + { + /** @var Document $document */ + $document = $this->createMock(Document::class); + /** @var Feed $feed */ + $feed = $this->getFeed(); + /** @var ResponseInterface $response */ + $response = new NullResponse(); + + return new Result($document, $feed, new \DateTime('@0'), $response, 'http://localhost/test.rss'); + } } diff --git a/tests/FeedIo/Reader/Fixer/PublicIdTest.php b/tests/FeedIo/Reader/Fixer/PublicIdTest.php index ad9d3cb6..a052d1c5 100644 --- a/tests/FeedIo/Reader/Fixer/PublicIdTest.php +++ b/tests/FeedIo/Reader/Fixer/PublicIdTest.php @@ -10,8 +10,12 @@ namespace FeedIo\Reader\Fixer; +use FeedIo\Adapter\NullResponse; +use FeedIo\Adapter\ResponseInterface; use FeedIo\Feed; use FeedIo\Feed\Item; +use FeedIo\Reader\Document; +use FeedIo\Reader\Result; use Psr\Log\NullLogger; use \PHPUnit\Framework\TestCase; @@ -20,7 +24,7 @@ class PublicIdTest extends TestCase { /** - * @var FeedIo\Reader\Fixer\PublicId + * @var PublicId */ protected $object; @@ -32,10 +36,11 @@ protected function setUp() public function testCorrect() { - $feed = $this->getFeed(); + $result = $this->getResultMock(); + $feed = $result->getFeed(); $this->assertNull($feed->getPublicId()); - $this->object->correct($feed); + $this->object->correct($result); $this->assertEquals($feed->getLink(), $feed->getPublicId()); @@ -63,4 +68,16 @@ protected function getFeed() return $feed; } + + protected function getResultMock(): Result + { + /** @var Document $document */ + $document = $this->createMock(Document::class); + /** @var Feed $feed */ + $feed = $this->getFeed(); + /** @var ResponseInterface $response */ + $response = new NullResponse(); + + return new Result($document, $feed, new \DateTime('@0'), $response, 'http://localhost/test.rss'); + } } diff --git a/tests/FeedIo/Reader/FixerMock.php b/tests/FeedIo/Reader/FixerMock.php index 3684b3c6..15ac3a84 100644 --- a/tests/FeedIo/Reader/FixerMock.php +++ b/tests/FeedIo/Reader/FixerMock.php @@ -32,11 +32,12 @@ public function setLogger(LoggerInterface $logger) : FixerAbstract } /** - * @param FeedInterface $feed + * @param Result $result * @return $this */ - public function correct(FeedInterface $feed) : FixerAbstract + public function correct(Result $result) : FixerAbstract { + $feed = $result->getFeed(); $feed->setTitle('corrected'); return $this; diff --git a/tests/FeedIo/Reader/FixerSetTest.php b/tests/FeedIo/Reader/FixerSetTest.php index 005aa8d3..7fa1453c 100644 --- a/tests/FeedIo/Reader/FixerSetTest.php +++ b/tests/FeedIo/Reader/FixerSetTest.php @@ -10,6 +10,8 @@ namespace FeedIo\Reader; +use FeedIo\Adapter\NullResponse; +use FeedIo\Adapter\ResponseInterface; use FeedIo\Feed; use \PHPUnit\Framework\TestCase; @@ -22,9 +24,23 @@ public function testCorrect() $fixerSet = new FixerSet(); $fixerSet->add($fixer); - $feed = new Feed(); - $fixerSet->correct($feed); + $result = $this->getResultMock(); + $feed = $result->getFeed(); + + $fixerSet->correct($result); $this->assertEquals('corrected', $feed->getTitle()); } + + protected function getResultMock(): Result + { + /** @var Document $document */ + $document = $this->createMock(Document::class); + /** @var Feed $feed */ + $feed = new Feed(); + /** @var ResponseInterface $response */ + $response = new NullResponse(); + + return new Result($document, $feed, new \DateTime('@0'), $response, 'http://localhost/test.rss'); + } } From 34ed799a6098b70f354a334c417f99dfc7ad8a1f Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Wed, 8 Jan 2020 15:30:38 +0100 Subject: [PATCH 2/3] Make sure to not overwrite lastModified --- src/FeedIo/Reader/Fixer/HttpLastModified.php | 2 +- tests/FeedIo/Reader/Fixer/HttpLastModifiedTest.php | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/FeedIo/Reader/Fixer/HttpLastModified.php b/src/FeedIo/Reader/Fixer/HttpLastModified.php index 2e16f567..2c32f567 100644 --- a/src/FeedIo/Reader/Fixer/HttpLastModified.php +++ b/src/FeedIo/Reader/Fixer/HttpLastModified.php @@ -25,7 +25,7 @@ public function correct(Result $result): FixerAbstract $feed = $result->getFeed(); $response = $result->getResponse(); - if ($response->getLastModified() instanceof \DateTime) { + if ($feed->getLastModified() === null && $response->getLastModified() instanceof \DateTime) { $this->logger->debug("found last modified: " . $response->getLastModified()->format(\DateTime::RSS)); $feed->setLastModified($response->getLastModified()); } diff --git a/tests/FeedIo/Reader/Fixer/HttpLastModifiedTest.php b/tests/FeedIo/Reader/Fixer/HttpLastModifiedTest.php index 615aa906..b9bf25d2 100644 --- a/tests/FeedIo/Reader/Fixer/HttpLastModifiedTest.php +++ b/tests/FeedIo/Reader/Fixer/HttpLastModifiedTest.php @@ -44,6 +44,18 @@ public function testCorrect() $this->assertEquals(new \DateTime('@0'), $feed->getLastModified()); } + public function testSkipCorrectIfLastModifiedNotNull() + { + $result = $this->getResultMock(); + $feed = $result->getFeed(); + $feed->setLastModified(new \DateTime('@3')); + + $this->assertNotNull($feed->getLastModified()); + $this->object->correct($result); + + $this->assertEquals(new \DateTime('@3'), $feed->getLastModified()); + } + protected function getResultMock(): Result { /** @var Document $document */ From de2b081ba855c8cb102f530d146bf51aeb9c08c4 Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Wed, 8 Jan 2020 22:13:24 +0100 Subject: [PATCH 3/3] Add factory for ResultMock --- .../Reader/Fixer/HttpLastModifiedTest.php | 28 +++++----------- .../FeedIo/Reader/Fixer/LastModifiedTest.php | 26 +++++---------- tests/FeedIo/Reader/Fixer/PublicIdTest.php | 25 +++++--------- tests/FeedIo/Reader/FixerSetTest.php | 28 +++++++--------- tests/FeedIo/Reader/ResultMockFactory.php | 33 +++++++++++++++++++ 5 files changed, 69 insertions(+), 71 deletions(-) create mode 100644 tests/FeedIo/Reader/ResultMockFactory.php diff --git a/tests/FeedIo/Reader/Fixer/HttpLastModifiedTest.php b/tests/FeedIo/Reader/Fixer/HttpLastModifiedTest.php index b9bf25d2..1d7d2640 100644 --- a/tests/FeedIo/Reader/Fixer/HttpLastModifiedTest.php +++ b/tests/FeedIo/Reader/Fixer/HttpLastModifiedTest.php @@ -10,11 +10,7 @@ namespace FeedIo\Reader\Fixer; -use FeedIo\Adapter\NullResponse; -use FeedIo\Adapter\ResponseInterface; -use FeedIo\Feed; -use FeedIo\Reader\Document; -use FeedIo\Reader\Result; +use FeedIo\Reader\ResultMockFactory; use Psr\Log\NullLogger; use \PHPUnit\Framework\TestCase; @@ -27,15 +23,21 @@ class HttpLastModifiedTest extends TestCase */ protected $object; + /** + * @var ResultMockFactory + */ + protected $resultMockFactory; + protected function setUp() { $this->object = new HttpLastModified(); $this->object->setLogger(new NullLogger()); + $this->resultMockFactory = new ResultMockFactory(); } public function testCorrect() { - $result = $this->getResultMock(); + $result = $this->resultMockFactory->make(); $feed = $result->getFeed(); $this->assertNull($feed->getLastModified()); @@ -46,7 +48,7 @@ public function testCorrect() public function testSkipCorrectIfLastModifiedNotNull() { - $result = $this->getResultMock(); + $result = $this->resultMockFactory->make(); $feed = $result->getFeed(); $feed->setLastModified(new \DateTime('@3')); @@ -55,16 +57,4 @@ public function testSkipCorrectIfLastModifiedNotNull() $this->assertEquals(new \DateTime('@3'), $feed->getLastModified()); } - - protected function getResultMock(): Result - { - /** @var Document $document */ - $document = $this->createMock(Document::class); - /** @var Feed $feed */ - $feed = new Feed(); - /** @var ResponseInterface $response */ - $response = new NullResponse(); - - return new Result($document, $feed, new \DateTime('@0'), $response, 'http://localhost/test.rss'); - } } diff --git a/tests/FeedIo/Reader/Fixer/LastModifiedTest.php b/tests/FeedIo/Reader/Fixer/LastModifiedTest.php index 4dfb78af..8f97a483 100644 --- a/tests/FeedIo/Reader/Fixer/LastModifiedTest.php +++ b/tests/FeedIo/Reader/Fixer/LastModifiedTest.php @@ -10,12 +10,9 @@ namespace FeedIo\Reader\Fixer; -use FeedIo\Adapter\NullResponse; -use FeedIo\Adapter\ResponseInterface; use FeedIo\Feed; use FeedIo\Feed\Item; -use FeedIo\Reader\Document; -use FeedIo\Reader\Result; +use FeedIo\Reader\ResultMockFactory; use Psr\Log\NullLogger; use \PHPUnit\Framework\TestCase; @@ -33,12 +30,17 @@ class LastModifiedTest extends TestCase */ protected $newest; + /** + * @var ResultMockFactory + */ + protected $resultMockFactory; + protected function setUp() { $this->newest = new \DateTime('2014-01-01'); - $this->object = new LastModified(); $this->object->setLogger(new NullLogger()); + $this->resultMockFactory = new ResultMockFactory(); } public function testSearchLastModified() @@ -53,7 +55,7 @@ public function testSearchLastModified() public function testCorrect() { - $result = $this->getResultMock(); + $result = $this->resultMockFactory->makeWithFeed($this->getFeed()); $feed = $result->getFeed(); $this->assertNull($feed->getLastModified()); @@ -75,16 +77,4 @@ protected function getFeed() return $feed; } - - protected function getResultMock(): Result - { - /** @var Document $document */ - $document = $this->createMock(Document::class); - /** @var Feed $feed */ - $feed = $this->getFeed(); - /** @var ResponseInterface $response */ - $response = new NullResponse(); - - return new Result($document, $feed, new \DateTime('@0'), $response, 'http://localhost/test.rss'); - } } diff --git a/tests/FeedIo/Reader/Fixer/PublicIdTest.php b/tests/FeedIo/Reader/Fixer/PublicIdTest.php index a052d1c5..88362c58 100644 --- a/tests/FeedIo/Reader/Fixer/PublicIdTest.php +++ b/tests/FeedIo/Reader/Fixer/PublicIdTest.php @@ -10,12 +10,9 @@ namespace FeedIo\Reader\Fixer; -use FeedIo\Adapter\NullResponse; -use FeedIo\Adapter\ResponseInterface; use FeedIo\Feed; use FeedIo\Feed\Item; -use FeedIo\Reader\Document; -use FeedIo\Reader\Result; +use FeedIo\Reader\ResultMockFactory; use Psr\Log\NullLogger; use \PHPUnit\Framework\TestCase; @@ -28,15 +25,21 @@ class PublicIdTest extends TestCase */ protected $object; + /** + * @var ResultMockFactory + */ + protected $resultMockFactory; + protected function setUp() { $this->object = new PublicId(); $this->object->setLogger(new NullLogger()); + $this->resultMockFactory = new ResultMockFactory(); } public function testCorrect() { - $result = $this->getResultMock(); + $result = $this->resultMockFactory->makeWithFeed($this->getFeed()); $feed = $result->getFeed(); $this->assertNull($feed->getPublicId()); @@ -68,16 +71,4 @@ protected function getFeed() return $feed; } - - protected function getResultMock(): Result - { - /** @var Document $document */ - $document = $this->createMock(Document::class); - /** @var Feed $feed */ - $feed = $this->getFeed(); - /** @var ResponseInterface $response */ - $response = new NullResponse(); - - return new Result($document, $feed, new \DateTime('@0'), $response, 'http://localhost/test.rss'); - } } diff --git a/tests/FeedIo/Reader/FixerSetTest.php b/tests/FeedIo/Reader/FixerSetTest.php index 7fa1453c..a69ca061 100644 --- a/tests/FeedIo/Reader/FixerSetTest.php +++ b/tests/FeedIo/Reader/FixerSetTest.php @@ -10,37 +10,31 @@ namespace FeedIo\Reader; -use FeedIo\Adapter\NullResponse; -use FeedIo\Adapter\ResponseInterface; -use FeedIo\Feed; - use \PHPUnit\Framework\TestCase; class FixerSetTest extends TestCase { + /** + * @var ResultMockFactory + */ + protected $resultMockFactory; + + protected function setUp() + { + $this->resultMockFactory = new ResultMockFactory(); + } + public function testCorrect() { $fixer = new FixerMock(); $fixerSet = new FixerSet(); $fixerSet->add($fixer); - $result = $this->getResultMock(); + $result = $this->resultMockFactory->make(); $feed = $result->getFeed(); $fixerSet->correct($result); $this->assertEquals('corrected', $feed->getTitle()); } - - protected function getResultMock(): Result - { - /** @var Document $document */ - $document = $this->createMock(Document::class); - /** @var Feed $feed */ - $feed = new Feed(); - /** @var ResponseInterface $response */ - $response = new NullResponse(); - - return new Result($document, $feed, new \DateTime('@0'), $response, 'http://localhost/test.rss'); - } } diff --git a/tests/FeedIo/Reader/ResultMockFactory.php b/tests/FeedIo/Reader/ResultMockFactory.php new file mode 100644 index 00000000..04e2dbf2 --- /dev/null +++ b/tests/FeedIo/Reader/ResultMockFactory.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FeedIo\Reader; + +use FeedIo\Adapter\NullResponse; +use FeedIo\Adapter\ResponseInterface; +use FeedIo\Feed; + +class ResultMockFactory +{ + public function make(): Result + { + return $this->makeWithFeed(new Feed()); + } + + public function makeWithFeed(Feed $feed): Result + { + /** @var Document $document */ + $document = new Document(''); + /** @var ResponseInterface $response */ + $response = new NullResponse(); + + return new Result($document, $feed, new \DateTime('@0'), $response, 'http://localhost/test.rss'); + } +}