Skip to content

Commit

Permalink
Merge pull request #259 from kesselb/enh/read-lastmodified-from-response
Browse files Browse the repository at this point in the history
Read last modified value from response
  • Loading branch information
alexdebril authored Jan 10, 2020
2 parents 30142fe + de2b081 commit c651b6f
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/FeedIo/FeedIo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
Expand Down Expand Up @@ -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;
}
Expand Down
35 changes: 35 additions & 0 deletions src/FeedIo/Reader/Fixer/HttpLastModified.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);
/*
* This file is part of the feed-io package.
*
* (c) Alexandre Debril <alex.debril@gmail.com>
*
* 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 ($feed->getLastModified() === null && $response->getLastModified() instanceof \DateTime) {
$this->logger->debug("found last modified: " . $response->getLastModified()->format(\DateTime::RSS));
$feed->setLastModified($response->getLastModified());
}

return $this;
}
}
7 changes: 5 additions & 2 deletions src/FeedIo/Reader/Fixer/LastModified.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 5 additions & 3 deletions src/FeedIo/Reader/Fixer/PublicId.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/FeedIo/Reader/FixerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 3 additions & 3 deletions src/FeedIo/Reader/FixerSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
60 changes: 60 additions & 0 deletions tests/FeedIo/Reader/Fixer/HttpLastModifiedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/*
* This file is part of the feed-io package.
*
* (c) Alexandre Debril <alex.debril@gmail.com>
*
* 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\ResultMockFactory;
use Psr\Log\NullLogger;

use \PHPUnit\Framework\TestCase;

class HttpLastModifiedTest extends TestCase
{

/**
* @var HttpLastModified
*/
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->resultMockFactory->make();
$feed = $result->getFeed();

$this->assertNull($feed->getLastModified());
$this->object->correct($result);

$this->assertEquals(new \DateTime('@0'), $feed->getLastModified());
}

public function testSkipCorrectIfLastModifiedNotNull()
{
$result = $this->resultMockFactory->make();
$feed = $result->getFeed();
$feed->setLastModified(new \DateTime('@3'));

$this->assertNotNull($feed->getLastModified());
$this->object->correct($result);

$this->assertEquals(new \DateTime('@3'), $feed->getLastModified());
}
}
15 changes: 11 additions & 4 deletions tests/FeedIo/Reader/Fixer/LastModifiedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use FeedIo\Feed;
use FeedIo\Feed\Item;
use FeedIo\Reader\ResultMockFactory;
use Psr\Log\NullLogger;

use \PHPUnit\Framework\TestCase;
Expand All @@ -20,7 +21,7 @@ class LastModifiedTest extends TestCase
{

/**
* @var FeedIo\Reader\Fixer\LastModified
* @var LastModified
*/
protected $object;

Expand All @@ -29,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()
Expand All @@ -49,10 +55,11 @@ public function testSearchLastModified()

public function testCorrect()
{
$feed = $this->getFeed();
$result = $this->resultMockFactory->makeWithFeed($this->getFeed());
$feed = $result->getFeed();

$this->assertNull($feed->getLastModified());
$this->object->correct($feed);
$this->object->correct($result);

$this->assertEquals($this->newest, $feed->getLastModified());
}
Expand Down
14 changes: 11 additions & 3 deletions tests/FeedIo/Reader/Fixer/PublicIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use FeedIo\Feed;
use FeedIo\Feed\Item;
use FeedIo\Reader\ResultMockFactory;
use Psr\Log\NullLogger;

use \PHPUnit\Framework\TestCase;
Expand All @@ -20,22 +21,29 @@ class PublicIdTest extends TestCase
{

/**
* @var FeedIo\Reader\Fixer\PublicId
* @var PublicId
*/
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()
{
$feed = $this->getFeed();
$result = $this->resultMockFactory->makeWithFeed($this->getFeed());
$feed = $result->getFeed();

$this->assertNull($feed->getPublicId());
$this->object->correct($feed);
$this->object->correct($result);

$this->assertEquals($feed->getLink(), $feed->getPublicId());

Expand Down
5 changes: 3 additions & 2 deletions tests/FeedIo/Reader/FixerMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 14 additions & 4 deletions tests/FeedIo/Reader/FixerSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,30 @@

namespace FeedIo\Reader;

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);

$feed = new Feed();
$fixerSet->correct($feed);
$result = $this->resultMockFactory->make();
$feed = $result->getFeed();

$fixerSet->correct($result);

$this->assertEquals('corrected', $feed->getTitle());
}
Expand Down
33 changes: 33 additions & 0 deletions tests/FeedIo/Reader/ResultMockFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/*
* This file is part of the feed-io package.
*
* (c) Alexandre Debril <alex.debril@gmail.com>
*
* 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');
}
}

0 comments on commit c651b6f

Please sign in to comment.