Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RssTest.php
Go to the documentation of this file.
1 <?php
8 
11 
12 class RssTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $rss;
18 
22  private $feedData = [
23  'title' => 'Feed Title',
24  'link' => 'http://magento.com/rss/link',
25  'description' => 'Feed Description',
26  'charset' => 'UTF-8',
27  'entries' => [
28  [
29  'title' => 'Feed 1 Title',
30  'link' => 'http://magento.com/rss/link/id/1',
31  'description' => 'Feed 1 Description',
32  ],
33  ],
34  ];
35 
39  private $feedXml = '<?xml version="1.0" encoding="UTF-8"?>
40 <rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
41  <channel>
42  <title><![CDATA[Feed Title]]></title>
43  <link>http://magento.com/rss/link</link>
44  <description><![CDATA[Feed Description]]></description>
45  <pubDate>Sat, 22 Apr 2017 13:21:12 +0200</pubDate>
46  <generator>Zend\Feed</generator>
47  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
48  <item>
49  <title><![CDATA[Feed 1 Title]]></title>
50  <link>http://magento.com/rss/link/id/1</link>
51  <description><![CDATA[Feed 1 Description]]></description>
52  <pubDate>Sat, 22 Apr 2017 13:21:12 +0200</pubDate>
53  </item>
54  </channel>
55 </rss>';
56 
61 
65  private $cacheMock;
66 
70  private $feedFactoryMock;
71 
75  private $feedMock;
76 
80  private $serializerMock;
81 
82  protected function setUp()
83  {
84  $this->cacheMock = $this->createMock(\Magento\Framework\App\CacheInterface::class);
85  $this->serializerMock = $this->createMock(SerializerInterface::class);
86  $this->feedFactoryMock = $this->createMock(\Magento\Framework\App\FeedFactoryInterface::class);
87  $this->feedMock = $this->createMock(\Magento\Framework\App\FeedInterface::class);
88 
89  $this->objectManagerHelper = new ObjectManagerHelper($this);
90  $this->rss = $this->objectManagerHelper->getObject(
91  \Magento\Rss\Model\Rss::class,
92  [
93  'cache' => $this->cacheMock,
94  'feedFactory' => $this->feedFactoryMock,
95  'serializer' => $this->serializerMock
96  ]
97  );
98  }
99 
100  public function testGetFeeds()
101  {
102  $dataProvider = $this->createMock(\Magento\Framework\App\Rss\DataProviderInterface::class);
103  $dataProvider->expects($this->any())->method('getCacheKey')->will($this->returnValue('cache_key'));
104  $dataProvider->expects($this->any())->method('getCacheLifetime')->will($this->returnValue(100));
105  $dataProvider->expects($this->any())->method('getRssData')->will($this->returnValue($this->feedData));
106 
107  $this->rss->setDataProvider($dataProvider);
108 
109  $this->cacheMock->expects($this->once())
110  ->method('load')
111  ->with('cache_key')
112  ->will($this->returnValue(false));
113  $this->cacheMock->expects($this->once())
114  ->method('save')
115  ->with('serializedData')
116  ->will($this->returnValue(true));
117  $this->serializerMock->expects($this->once())
118  ->method('serialize')
119  ->with($this->feedData)
120  ->willReturn('serializedData');
121 
122  $this->assertEquals($this->feedData, $this->rss->getFeeds());
123  }
124 
125  public function testGetFeedsWithCache()
126  {
127  $dataProvider = $this->createMock(\Magento\Framework\App\Rss\DataProviderInterface::class);
128  $dataProvider->expects($this->any())->method('getCacheKey')->will($this->returnValue('cache_key'));
129  $dataProvider->expects($this->any())->method('getCacheLifetime')->will($this->returnValue(100));
130  $dataProvider->expects($this->never())->method('getRssData');
131 
132  $this->rss->setDataProvider($dataProvider);
133 
134  $this->cacheMock->expects($this->once())
135  ->method('load')
136  ->with('cache_key')
137  ->will($this->returnValue('serializedData'));
138  $this->serializerMock->expects($this->once())
139  ->method('unserialize')
140  ->with('serializedData')
141  ->willReturn($this->feedData);
142  $this->cacheMock->expects($this->never())->method('save');
143 
144  $this->assertEquals($this->feedData, $this->rss->getFeeds());
145  }
146 
147  public function testCreateRssXml()
148  {
149  $dataProvider = $this->createMock(\Magento\Framework\App\Rss\DataProviderInterface::class);
150  $dataProvider->expects($this->any())->method('getCacheKey')->will($this->returnValue('cache_key'));
151  $dataProvider->expects($this->any())->method('getCacheLifetime')->will($this->returnValue(100));
152  $dataProvider->expects($this->any())->method('getRssData')->will($this->returnValue($this->feedData));
153 
154  $this->feedMock->expects($this->once())
155  ->method('getFormattedContent')
156  ->willReturn($this->feedXml);
157 
158  $this->feedFactoryMock->expects($this->once())
159  ->method('create')
160  ->with($this->feedData, \Magento\Framework\App\FeedFactoryInterface::FORMAT_RSS)
161  ->will($this->returnValue($this->feedMock));
162 
163  $this->rss->setDataProvider($dataProvider);
164  $this->assertNotNull($this->rss->createRssXml());
165  }
166 }