1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Rss\Test\Unit\Model;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
class RssTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Rss\Model\Rss
*/
protected $rss;
/**
* @var array
*/
private $feedData = [
'title' => 'Feed Title',
'link' => 'http://magento.com/rss/link',
'description' => 'Feed Description',
'charset' => 'UTF-8',
'entries' => [
[
'title' => 'Feed 1 Title',
'link' => 'http://magento.com/rss/link/id/1',
'description' => 'Feed 1 Description',
],
],
];
/**
* @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
*/
protected $objectManagerHelper;
/**
* @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
*/
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
*/
private $serializerMock;
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
]
);
}
public function testGetFeeds()
{
$dataProvider = $this->createMock(\Magento\Framework\App\Rss\DataProviderInterface::class);
$dataProvider->expects($this->any())->method('getCacheKey')->will($this->returnValue('cache_key'));
$dataProvider->expects($this->any())->method('getCacheLifetime')->will($this->returnValue(100));
$dataProvider->expects($this->any())->method('getRssData')->will($this->returnValue($this->feedData));
$this->rss->setDataProvider($dataProvider);
$this->cacheMock->expects($this->once())
->method('load')
->with('cache_key')
->will($this->returnValue(false));
$this->cacheMock->expects($this->once())
->method('save')
->with('serializedData')
->will($this->returnValue(true));
$this->serializerMock->expects($this->once())
->method('serialize')
->with($this->feedData)
->willReturn('serializedData');
$this->assertEquals($this->feedData, $this->rss->getFeeds());
}
public function testGetFeedsWithCache()
{
$dataProvider = $this->createMock(\Magento\Framework\App\Rss\DataProviderInterface::class);
$dataProvider->expects($this->any())->method('getCacheKey')->will($this->returnValue('cache_key'));
$dataProvider->expects($this->any())->method('getCacheLifetime')->will($this->returnValue(100));
$dataProvider->expects($this->never())->method('getRssData');
$this->rss->setDataProvider($dataProvider);
$this->cacheMock->expects($this->once())
->method('load')
->with('cache_key')
->will($this->returnValue('serializedData'));
$this->serializerMock->expects($this->once())
->method('unserialize')
->with('serializedData')
->willReturn($this->feedData);
$this->cacheMock->expects($this->never())->method('save');
$this->assertEquals($this->feedData, $this->rss->getFeeds());
}
public function testCreateRssXml()
{
$dataProvider = $this->createMock(\Magento\Framework\App\Rss\DataProviderInterface::class);
$dataProvider->expects($this->any())->method('getCacheKey')->will($this->returnValue('cache_key'));
$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('getFormattedContent')
->willReturn($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);
$this->assertNotNull($this->rss->createRssXml());
}
}