Skip to content
Merged
Show file tree
Hide file tree
Changes from 21 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::DEFAULT_FORMAT

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Currently, you are referencing undefined constant, as you changed the name of this constant in FeedFactoryInterface

That's why you have Red Unit and Static tests:

There was 1 error:
1) Magento\Rss\Test\Unit\Model\RssTest::testCreateRssXml
Error: Undefined class constant 'DEFAULT_FORMAT'
/home/travis/build/magento/magento2/app/code/Magento/Rss/Test/Unit/Model/RssTest.php:156

);

return $feed->getFormattedContentAs(
FeedInterface::DEFAULT_FORMAT

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There was 1 error:
1) Magento\Rss\Test\Unit\Model\RssTest::testCreateRssXml
Error: Undefined class constant 'DEFAULT_FORMAT'
/home/travis/build/magento/magento2/app/code/Magento/Rss/Test/Unit/Model/RssTest.php:156

);
}
}
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::DEFAULT_FORMAT)
->will($this->returnValue($this->feedXml));

$this->feedFactoryMock->expects($this->once())
->method('create')
->with($this->feedData, \Magento\Framework\App\FeedFactoryInterface::DEFAULT_FORMAT)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There was 1 error:
1) Magento\Rss\Test\Unit\Model\RssTest::testCreateRssXml
Error: Undefined class constant 'DEFAULT_FORMAT'
/home/travis/build/magento/magento2/app/code/Magento/Rss/Test/Unit/Model/RssTest.php:156

->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
39 changes: 39 additions & 0 deletions lib/internal/Magento/Framework/App/Feed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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
{
/**
* @param Zend_Feed $feed
* @param array $data
*/
public function __construct(
\Zend_Feed $feed,
array $data
) {
$this->feed = $feed;
$this->data = $data;
}

/**
* Returns the formatted feed content
*

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@param should be specified

* @return string
*/
public function getFormattedContentAs(
$format = self::FORMAT_XML
) {
$feed = $this->feed::importArray(
$this->data,
FeedFactoryInterface::FORMAT_RSS
);
return $this->feed->saveXml();
}
}
86 changes: 86 additions & 0 deletions lib/internal/Magento/Framework/App/FeedFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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 FeedProcessorInterface
*/
private $feedProcessor;

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

/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @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;
}

/**
* Get a new \Magento\Framework\App\FeedInterface object from a custom array
*
* @throws \Magento\Framework\Exception\InputException
* @param array $data

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

interitdoc should be used here

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

if (!is_subclass_of($this->formats[$format], '\Magento\Framework\App\FeedInterface')) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

instead of \Magento\Framework\App\FeedInterface you suppose to use \Magento\Framework\App\FeedInterface::class

throw new \Magento\Framework\Exception\InputException(
__('Wrong format handler type'),
$e
);
}

try {
return $this->objectManager->create(
$this->formats[$format],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we need to validate in constructor that
$this->formats[$format] implements interface
\Magento\Framework\App\FeedInterface

actually all $this->formats suppose to be successors of \Magento\Framework\App\FeedInterface

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

Copy link
Copy Markdown
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

interface FeedFactoryInterface
{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please provide a description of this constant.
In PHP Doc Block

const FORMAT_RSS = 'rss';

/**
* Returns FeedInterface object from a custom array
*
* @throws \Magento\Framework\Exception\InputException

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

throws \Magento\Framework\Exception\RuntimeException
should be added to the contract

* @param array $data
* @param string $format
* @return FeedInterface
*/
public function create(
array $data,
$format = self::FORMAT_RSS
);
}
18 changes: 18 additions & 0 deletions lib/internal/Magento/Framework/App/FeedInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\App;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please provide description for the interface

interface FeedInterface
{
const FORMAT_XML = 'xml';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please provide description of the constant


/**

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please provide description of input data in PHP Doc Block

* @return string
*/
public function getFormattedContentAs(
$format = self::FORMAT_XML
);
}