-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Zend feed refactoring #9347
Changes from 7 commits
ca9e582
ed33b18
c7ba65a
9d0a290
109047f
4534a2e
c413389
caa4471
66154e6
49bcf98
c0b4c7a
411d0fc
9779ebe
eba92d0
983d74f
0b51e8c
74d431a
7f73d67
2933223
64610d3
d2d397a
0770227
735c840
7e17d20
46c51ba
3a3683f
6311d5c
4462eaf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,27 @@ class RssTest extends \PHPUnit_Framework_TestCase | |
], | ||
]; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $feedXml = '<?xml version="1.0" encoding="UTF-8"?> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better to make variable as private There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, fixed. |
||
<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 | ||
*/ | ||
|
@@ -43,6 +64,16 @@ class RssTest extends \PHPUnit_Framework_TestCase | |
*/ | ||
private $cacheMock; | ||
|
||
/** | ||
* @var \Magento\Framework\App\FeedImporterInterface|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $feedImporterMock; | ||
|
||
/** | ||
* @var \Magento\Framework\App\FeedInterface|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $feedMock; | ||
|
||
/** | ||
* @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
|
@@ -52,11 +83,15 @@ protected function setUp() | |
{ | ||
$this->cacheMock = $this->getMock(\Magento\Framework\App\CacheInterface::class); | ||
$this->serializerMock = $this->getMock(SerializerInterface::class); | ||
$this->feedImporterMock = $this->getMock(\Magento\Framework\App\FeedImporterInterface::class); | ||
$this->feedMock = $this->getMock(\Magento\Framework\App\FeedInterface::class); | ||
|
||
$this->objectManagerHelper = new ObjectManagerHelper($this); | ||
$this->rss = $this->objectManagerHelper->getObject( | ||
\Magento\Rss\Model\Rss::class, | ||
[ | ||
'cache' => $this->cacheMock, | ||
'feedImporter' => $this->feedImporterMock, | ||
'serializer' => $this->serializerMock | ||
] | ||
); | ||
|
@@ -116,6 +151,15 @@ 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('asXml') | ||
->will($this->returnValue($this->feedXml)); | ||
|
||
$this->feedImporterMock->expects($this->once()) | ||
->method('importArray') | ||
->with($this->feedData) | ||
->will($this->returnValue($this->feedMock)); | ||
|
||
$this->rss->setDataProvider($dataProvider); | ||
$result = $this->rss->createRssXml(); | ||
$this->assertContains('<?xml version="1.0" encoding="UTF-8"?>', $result); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Framework\App; | ||
|
||
class Feed implements \Magento\Framework\App\FeedInterface | ||
{ | ||
/** | ||
* @var \Magento\Framework\App\FeedImporterInterface | ||
*/ | ||
private $feed; | ||
|
||
/** | ||
* @param \Zend_Feed_Abstract $feed | ||
*/ | ||
public function __construct(\Zend_Feed_Abstract $feed) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide supported formats argument, configurable through DI. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. |
||
{ | ||
$this->feed = $feed; | ||
} | ||
|
||
/** | ||
* Get the xml from Zend_Feed_Abstract object | ||
* | ||
* @return string | ||
*/ | ||
public function asXml() | ||
{ | ||
return $this->feed->saveXml(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Framework\App\Feed; | ||
|
||
/** | ||
* Feed importer | ||
*/ | ||
class Importer implements \Magento\Framework\App\FeedImporterInterface | ||
{ | ||
/** | ||
* @var \Zend_Feed | ||
*/ | ||
private $feedProcessor; | ||
|
||
/** | ||
* @var \Magento\Framework\App\FeedFactory | ||
*/ | ||
private $feedFactory; | ||
|
||
/** | ||
* @param \Zend_Feed $feedProcessor | ||
* @param \Magento\Framework\App\FeedFactory $feedFactory | ||
*/ | ||
public function __construct( | ||
\Zend_Feed $feedProcessor, | ||
\Magento\Framework\App\FeedFactory $feedFactory | ||
) { | ||
$this->feedProcessor = $feedProcessor; | ||
$this->feedFactory = $feedFactory; | ||
} | ||
|
||
/** | ||
* Get a new \Magento\Framework\App\Feed object from a custom array | ||
* | ||
* @throws \Magento\Framework\Exception\FeedImporterException | ||
* @param array $data | ||
* @param string $format | ||
* @return \Magento\Framework\App\FeedInterface | ||
*/ | ||
public function importArray(array $data, $format = 'atom') | ||
{ | ||
|
||
try { | ||
$feed = $this->feedProcessor->importArray($data, $format); | ||
return $this->feedFactory->create(['feed' => $feed]); | ||
} catch (\Zend_Feed_Exception $e) { | ||
throw new \Magento\Framework\Exception\FeedImporterException( | ||
new \Magento\Framework\Phrase($e->getMessage()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice one, makes sense, thanks 👍 |
||
$e | ||
); | ||
} | ||
} | ||
} |
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; | ||
|
||
interface FeedImporterInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe make sense to rename this interface to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure about this, why do you think that it should be renamed to factory, it doesn't create new objects? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like it creates. Description from the importArray method says: $feed = $this->feedProcessor->importArray($data, $format);
return $this->feedFactory->create(['feed' => $feed]); for now it's not clear why it's called importer? From where it imports data? For me it's not clear why we need to keep both objects Because both of them responsible for \Magento\Framework\App\FeedInterface creation. I propose to get rid of \Magento\Framework\App\Feed\Importer and move it's logic to FeedFactory. Thus, FeedFactoryInterface would look like: class FeedFactoryInterface
{
public function importArray(array $data, $format = SupportedFeedFormatsInterface::DEFAULT_FORMAT)
} |
||
{ | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please provide a precise description what this method is actually do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
* @throws \Magento\Framework\Exception\FeedImporterException | ||
* @param array $data | ||
* @param string $format | ||
* @return FeedInterface | ||
*/ | ||
public function importArray(array $data, $format = 'atom'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's not good from interface point of view, that 'atom' passed as a default parameter. What other formats does this Importer support? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see your point but I did it because I wanted to replicate the original importArray method from Zend_Feed which has those arguments. What would be the best thing to do in this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not always a good Idea to replicate contracts from Zend. Because sometimes we would provide a wider contract than Zend does, and sometimes their contracts are not so ideal. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Framework\App; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please provide description for the interface |
||
interface FeedInterface | ||
{ | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 asXml(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to see this method as public function getFormatedContentAs($format = SupportedFeedFormatsInterface::DEFAULT_FORMAT); Naming could be changed, but main idea is to make FeedInteface agnostic to the possible representation formats. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
/** | ||
* Feed importer exception | ||
* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Framework\Exception; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't think that we need to introduce new exception type. It's better to re-use some of the existing ones. Or at least to introduce new exception for ImportException There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you look at the Zend_Feed module Zend_Feed_Exception can be thrown for a lot of things. The only common exception for all those cases would be RuntimeException, so I picked that one. Let me know what you think. |
||
|
||
class FeedImporterException extends LocalizedException | ||
{ | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's backward incompatible, new parameter should be added last into the list, and that parameter should be optional. You can read more about it here - http://devdocs.magento.com/guides/v2.1/ext-best-practices/extension-coding/backwards-compatible-development/index.html "Adding a constructor parameter" section
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, it's fixed now.