Skip to content

Zend feed refactoring #9347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ca9e582
Zend_Feed refactoring progress
ldusan84 Apr 20, 2017
ed33b18
Tests refactoring
ldusan84 Apr 21, 2017
c7ba65a
Fixed unit test
ldusan84 Apr 22, 2017
9d0a290
Tidying up files
ldusan84 Apr 22, 2017
109047f
Small CS fix
ldusan84 Apr 22, 2017
4534a2e
Small CS fix
ldusan84 Apr 22, 2017
c413389
More CS fixes
ldusan84 Apr 29, 2017
caa4471
Small exception fix
ldusan84 May 7, 2017
66154e6
Changes after the code review
ldusan84 May 24, 2017
49bcf98
Zend Feed refactorging WIP
ldusan84 Jul 27, 2017
c0b4c7a
Implementing code review requested changes
ldusan84 Aug 7, 2017
411d0fc
Merge branch 'develop' into feature/zend_feed_refactoring
Aug 30, 2017
9779ebe
Zend Feed refactoring WIP
ldusan84 Sep 4, 2017
eba92d0
magento/magento2#9347: Merge branch 'develop' of github.com:magento/m…
ishakhsuvarov Sep 13, 2017
983d74f
magento/magento2#9347: Zend feed refactoring
ishakhsuvarov Sep 13, 2017
0b51e8c
magento/magento2#9347: Zend feed refactoring
ishakhsuvarov Sep 13, 2017
74d431a
magento/magento2#9347: Zend feed refactoring
ishakhsuvarov Sep 13, 2017
7f73d67
Zend Feed refactoring WIP
ldusan84 Sep 25, 2017
2933223
Zend Feed refactoring WIP
ldusan84 Sep 25, 2017
64610d3
Removed the wrong lines commited
ldusan84 Sep 25, 2017
d2d397a
Fixing stuff as requested
ldusan84 Nov 16, 2017
0770227
Some code style fixes
ldusan84 Nov 23, 2017
735c840
RSS model fix
ldusan84 Nov 23, 2017
7e17d20
Unit test fix
ldusan84 Nov 24, 2017
46c51ba
RSS model fix
ldusan84 Nov 24, 2017
3a3683f
Code style fixes
ldusan84 Dec 22, 2017
6311d5c
Code fixes
ldusan84 Dec 22, 2017
4462eaf
Added some missing properties and renamed stuff
ldusan84 Dec 22, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions app/code/Magento/Rss/Model/Rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Rss\DataProviderInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\App\FeedInterface;
use Magento\Framework\App\FeedFactoryInterface;

/**
* Provides functionality to work with RSS feeds
Expand All @@ -27,6 +29,11 @@ class Rss
*/
protected $cache;

/**
* @var \Magento\Framework\App\FeedFactoryInterface
*/
private $feedFactory;

/**
* @var SerializerInterface
*/
Expand All @@ -37,13 +44,16 @@ class Rss
*
* @param \Magento\Framework\App\CacheInterface $cache
* @param SerializerInterface|null $serializer
* @param FeedFactoryInterface|null $feedFactory
*/
public function __construct(
\Magento\Framework\App\CacheInterface $cache,
SerializerInterface $serializer = null
SerializerInterface $serializer = null,
FeedFactoryInterface $feedFactory = null
) {
$this->cache = $cache;
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
$this->feedFactory = $feedFactory ?: ObjectManager::getInstance()->get(FeedFactoryInterface::class);
}

/**
Expand Down Expand Up @@ -92,7 +102,13 @@ public function setDataProvider(DataProviderInterface $dataProvider)
*/
public function createRssXml()
{
$rssFeedFromArray = \Zend_Feed::importArray($this->getFeeds(), 'rss');
return $rssFeedFromArray->saveXML();
$feed = $this->feedFactory->create(
$this->getFeeds(),
FeedFactoryInterface::FORMAT_RSS
);

return $feed->getFormattedContentAs(
FeedInterface::FORMAT_XML
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,22 @@ public function testExecuteWithException()
$dataProvider = $this->createMock(\Magento\Framework\App\Rss\DataProviderInterface::class);
$dataProvider->expects($this->once())->method('isAllowed')->will($this->returnValue(true));

$rssModel = $this->createPartialMock(\Magento\Rss\Model\Rss::class, ['setDataProvider']);
$rssModel = $this->createPartialMock(\Magento\Rss\Model\Rss::class, ['setDataProvider', 'createRssXml']);
$rssModel->expects($this->once())->method('setDataProvider')->will($this->returnSelf());

$exceptionMock = new \Magento\Framework\Exception\RuntimeException(
new \Magento\Framework\Phrase('Any message')
);

$rssModel->expects($this->once())->method('createRssXml')->will(
$this->throwException($exceptionMock)
);

$this->response->expects($this->once())->method('setHeader')->will($this->returnSelf());
$this->rssFactory->expects($this->once())->method('create')->will($this->returnValue($rssModel));
$this->rssManager->expects($this->once())->method('getProvider')->will($this->returnValue($dataProvider));

$this->expectException('\Zend_Feed_Builder_Exception');
$this->expectException(\Magento\Framework\Exception\RuntimeException::class);
$this->controller->execute();
}
}
13 changes: 11 additions & 2 deletions app/code/Magento/Rss/Test/Unit/Controller/Feed/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected function setUp()
->disableOriginalConstructor()->getMock();

$objectManagerHelper = new ObjectManagerHelper($this);

$this->controller = $objectManagerHelper->getObject(
\Magento\Rss\Controller\Feed\Index::class,
[
Expand Down Expand Up @@ -90,14 +91,22 @@ public function testExecuteWithException()
$dataProvider = $this->createMock(\Magento\Framework\App\Rss\DataProviderInterface::class);
$dataProvider->expects($this->once())->method('isAllowed')->will($this->returnValue(true));

$rssModel = $this->createPartialMock(\Magento\Rss\Model\Rss::class, ['setDataProvider']);
$rssModel = $this->createPartialMock(\Magento\Rss\Model\Rss::class, ['setDataProvider', 'createRssXml']);
$rssModel->expects($this->once())->method('setDataProvider')->will($this->returnSelf());

$exceptionMock = new \Magento\Framework\Exception\RuntimeException(
new \Magento\Framework\Phrase('Any message')
);

$rssModel->expects($this->once())->method('createRssXml')->will(
$this->throwException($exceptionMock)
);

$this->response->expects($this->once())->method('setHeader')->will($this->returnSelf());
$this->rssFactory->expects($this->once())->method('create')->will($this->returnValue($rssModel));
$this->rssManager->expects($this->once())->method('getProvider')->will($this->returnValue($dataProvider));

$this->expectException('\Zend_Feed_Builder_Exception');
$this->expectException(\Magento\Framework\Exception\RuntimeException::class);
$this->controller->execute();
}
}
47 changes: 46 additions & 1 deletion app/code/Magento/Rss/Test/Unit/Model/RssTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RssTest extends \PHPUnit\Framework\TestCase
/**
* @var array
*/
protected $feedData = [
private $feedData = [
'title' => 'Feed Title',
'link' => 'http://magento.com/rss/link',
'description' => 'Feed Description',
Expand All @@ -33,6 +33,27 @@ class RssTest extends \PHPUnit\Framework\TestCase
],
];

/**
* @var string
*/
private $feedXml = '<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<channel>
<title><![CDATA[Feed Title]]></title>
<link>http://magento.com/rss/link</link>
<description><![CDATA[Feed Description]]></description>
<pubDate>Sat, 22 Apr 2017 13:21:12 +0200</pubDate>
<generator>Zend_Feed</generator>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<item>
<title><![CDATA[Feed 1 Title]]></title>
<link>http://magento.com/rss/link/id/1</link>
<description><![CDATA[Feed 1 Description]]></description>
<pubDate>Sat, 22 Apr 2017 13:21:12 +0200</pubDate>
</item>
</channel>
</rss>';

/**
* @var ObjectManagerHelper
*/
Expand All @@ -43,6 +64,16 @@ class RssTest extends \PHPUnit\Framework\TestCase
*/
private $cacheMock;

/**
* @var \Magento\Framework\App\FeedFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $feedFactoryMock;

/**
* @var \Magento\Framework\App\FeedInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $feedMock;

/**
* @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
Expand All @@ -52,11 +83,15 @@ protected function setUp()
{
$this->cacheMock = $this->createMock(\Magento\Framework\App\CacheInterface::class);
$this->serializerMock = $this->createMock(SerializerInterface::class);
$this->feedFactoryMock = $this->createMock(\Magento\Framework\App\FeedFactoryInterface::class);
$this->feedMock = $this->createMock(\Magento\Framework\App\FeedInterface::class);

$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->rss = $this->objectManagerHelper->getObject(
\Magento\Rss\Model\Rss::class,
[
'cache' => $this->cacheMock,
'feedFactory' => $this->feedFactoryMock,
'serializer' => $this->serializerMock
]
);
Expand Down Expand Up @@ -116,6 +151,16 @@ public function testCreateRssXml()
$dataProvider->expects($this->any())->method('getCacheLifetime')->will($this->returnValue(100));
$dataProvider->expects($this->any())->method('getRssData')->will($this->returnValue($this->feedData));

$this->feedMock->expects($this->once())
->method('getFormattedContentAs')
->with(\Magento\Framework\App\FeedInterface::FORMAT_XML)
->will($this->returnValue($this->feedXml));

$this->feedFactoryMock->expects($this->once())
->method('create')
->with($this->feedData, \Magento\Framework\App\FeedFactoryInterface::FORMAT_RSS)
->will($this->returnValue($this->feedMock));

$this->rss->setDataProvider($dataProvider);
$result = $this->rss->createRssXml();
$this->assertContains('<?xml version="1.0" encoding="UTF-8"?>', $result);
Expand Down
9 changes: 9 additions & 0 deletions app/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@
<preference for="Magento\Framework\App\View\Deployment\Version\StorageInterface" type="Magento\Framework\App\View\Deployment\Version\Storage\File"/>
<preference for="Magento\Framework\View\Page\FaviconInterface" type="Magento\Theme\Model\Favicon\Favicon" />
<preference for="Magento\Framework\View\Element\Message\InterpretationStrategyInterface" type="Magento\Framework\View\Element\Message\InterpretationMediator" />
<preference for="Magento\Framework\App\FeedInterface" type="Magento\Framework\App\Feed" />
<preference for="Magento\Framework\App\FeedFactoryInterface" type="Magento\Framework\App\FeedFactory" />
<preference for="Magento\Framework\Indexer\Config\DependencyInfoProviderInterface" type="Magento\Framework\Indexer\Config\DependencyInfoProvider" />
<type name="Magento\Framework\Model\ResourceModel\Db\TransactionManager" shared="false" />
<type name="Magento\Framework\Acl\Data\Cache">
Expand Down Expand Up @@ -241,6 +243,13 @@
</argument>
</arguments>
</type>
<type name="Magento\Framework\FeedFactory">
<arguments>
<argument name="formats" xsi:type="array">
<item name="rss" xsi:type="string">Magento\Framework\App\Feed</item>
</argument>
</arguments>
</type>
<type name="Magento\Framework\Session\SaveHandler\Redis">
<arguments>
<argument name="config" xsi:type="object">Cm\RedisSession\Handler\ConfigInterface</argument>
Expand Down
51 changes: 51 additions & 0 deletions lib/internal/Magento/Framework/App/Feed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\App;

/**
* Default XML feed class
*/
class Feed implements FeedInterface
{
/**
* @var \Zend_Feed
*/
private $feedProcessor;

/**
* @var array
*/
private $data;

/**
* @param Zend_Feed $feedProcessor
* @param array $data
*/
public function __construct(
\Zend_Feed $feedProcessor,
array $data
) {
$this->feedProcessor = $feedProcessor;
$this->data = $data;
}

/**
* Returns the formatted feed content
*
* @param string $format
*
* @return string
*/
public function getFormattedContentAs(
$format = self::FORMAT_XML
) {
$feed = $this->feedProcessor::importArray(
$this->data,
FeedFactoryInterface::FORMAT_RSS
);
return $feed->saveXml();
}
}
81 changes: 81 additions & 0 deletions lib/internal/Magento/Framework/App/FeedFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\App;

use Magento\Framework\App\FeedFactoryInterface;
use Magento\Framework\ObjectManagerInterface;
use Psr\Log\LoggerInterface;

/**
* Feed factory
*/
class FeedFactory implements FeedFactoryInterface
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var array
*/
private $formats;

/**
* @param ObjectManagerInterface $objectManger
* @param LoggerInterface $logger
* @param array $formats
*/
public function __construct(
ObjectManagerInterface $objectManger,
LoggerInterface $logger,
array $formats
) {
$this->objectManager = $objectManger;
$this->logger = $logger;
$this->formats = $formats;
}

/**
* {@inheritdoc}
*/
public function create(
array $data,
$format = FeedFactoryInterface::FORMAT_RSS
) {
if (!isset($this->formats[$format])) {
throw new \Magento\Framework\Exception\InputException(
new \Magento\Framework\Phrase('The format is not supported'),
$e
);
}

if (!is_subclass_of($this->formats[$format], \Magento\Framework\App\FeedInterface::class)) {
throw new \Magento\Framework\Exception\InputException(
new \Magento\Framework\Phrase('Wrong format handler type'),
$e
);
}

try {
return $this->objectManager->create(
$this->formats[$format],
$data
);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
throw new \Magento\Framework\Exception\RuntimeException(
new \Magento\Framework\Phrase('There has been an error with import'),
$e
);
}
}
}
31 changes: 31 additions & 0 deletions lib/internal/Magento/Framework/App/FeedFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\App;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interface description should be added

/**
* Feed factory interface
*/
interface FeedFactoryInterface
{
/**
* RSS feed input format
*/
const FORMAT_RSS = 'rss';

/**
* Returns FeedInterface object from a custom array
*
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\RuntimeException
* @param array $data
* @param string $format
* @return FeedInterface
*/
public function create(
array $data,
$format = self::FORMAT_RSS
);
}
Loading