Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SpecialTest.php
Go to the documentation of this file.
1 <?php
7 
9 
14 class SpecialTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $block;
20 
24  protected $httpContext;
25 
29  protected $imageHelper;
30 
34  protected $outputHelper;
35 
39  protected $msrpHelper;
40 
44  protected $priceCurrency;
45 
49  protected $rssModel;
50 
54  protected $rssUrlBuilder;
55 
59  protected $storeManager;
60 
64  protected $scopeConfig;
65 
69  protected $localeDate;
70 
74  protected $request;
75 
76  protected function setUp()
77  {
78  $this->request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
79  $this->request->expects($this->at(0))->method('getParam')->with('store_id')->will($this->returnValue(null));
80  $this->request->expects($this->at(1))->method('getParam')->with('cid')->will($this->returnValue(null));
81 
82  $this->httpContext = $this->getMockBuilder(\Magento\Framework\App\Http\Context::class)
83  ->setMethods(['getValue'])->disableOriginalConstructor()->getMock();
84  $this->httpContext->expects($this->any())->method('getValue')->will($this->returnValue(1));
85 
86  $this->imageHelper = $this->createMock(\Magento\Catalog\Helper\Image::class);
87  $this->outputHelper = $this->createPartialMock(\Magento\Catalog\Helper\Output::class, ['productAttribute']);
88  $this->msrpHelper = $this->createPartialMock(\Magento\Msrp\Helper\Data::class, ['canApplyMsrp']);
89  $this->priceCurrency = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
90  $this->rssModel = $this->createMock(\Magento\Catalog\Model\Rss\Product\Special::class);
91  $this->rssUrlBuilder = $this->createMock(\Magento\Framework\App\Rss\UrlBuilderInterface::class);
92 
93  $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
94  $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
95  ->setMethods(['getId', 'getFrontendName', '__wakeup'])->disableOriginalConstructor()->getMock();
96  $store->expects($this->any())->method('getId')->will($this->returnValue(1));
97  $store->expects($this->any())->method('getFrontendName')->will($this->returnValue('Store 1'));
98  $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
99 
100  $this->scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
101  $this->scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue('en_US'));
102 
103  $this->localeDate = $this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
104 
105  $objectManagerHelper = new ObjectManagerHelper($this);
106  $this->block = $objectManagerHelper->getObject(
107  \Magento\Catalog\Block\Rss\Product\Special::class,
108  [
109  'request' => $this->request,
110  'httpContext' => $this->httpContext,
111  'imageHelper' => $this->imageHelper,
112  'outputHelper' => $this->outputHelper,
113  'msrpHelper' => $this->msrpHelper,
114  'priceCurrency' => $this->priceCurrency,
115  'rssModel' => $this->rssModel,
116  'rssUrlBuilder' => $this->rssUrlBuilder,
117  'storeManager' => $this->storeManager,
118  'scopeConfig' => $this->scopeConfig,
119  'localeDate' => $this->localeDate,
120  ]
121  );
122  }
123 
124  public function testGetRssData()
125  {
126  $this->rssUrlBuilder->expects($this->once())->method('getUrl')
127  ->with(['type' => 'special_products', 'store_id' => 1])
128  ->will($this->returnValue('http://magento.com/rss/feed/index/type/special_products/store_id/1'));
129  $item = $this->getItemMock();
130  $this->rssModel->expects($this->once())->method('getProductsCollection')
131  ->will($this->returnValue([$item]));
132  $this->msrpHelper->expects($this->once())->method('canApplyMsrp')->will($this->returnValue(false));
133  $this->localeDate->expects($this->once())->method('formatDateTime')->will($this->returnValue(date('Y-m-d')));
134 
135  $this->priceCurrency->expects($this->any())->method('convertAndFormat')->will($this->returnArgument(0));
136 
137  $this->imageHelper->expects($this->once())->method('init')->with($item, 'rss_thumbnail')
138  ->will($this->returnSelf());
139  $this->imageHelper->expects($this->once())->method('getUrl')
140  ->will($this->returnValue('image_link'));
141  $this->outputHelper->expects($this->once())->method('productAttribute')->will($this->returnValue(''));
142  $data = [
143  'title' => 'Store 1 - Special Products',
144  'description' => 'Store 1 - Special Products',
145  'link' => 'http://magento.com/rss/feed/index/type/special_products/store_id/1',
146  'charset' => 'UTF-8',
147  'language' => 'en_US',
148  'entries' => [
149  [
150  'title' => 'Product Name',
151  'link' => 'http://magento.com/product-name.html',
152  ],
153  ],
154  ];
155  $rssData = $this->block->getRssData();
156  $description = $rssData['entries'][0]['description'];
157  unset($rssData['entries'][0]['description']);
158  $this->assertEquals($data, $rssData);
159  $this->assertContains('<a href="http://magento.com/product-name.html"><', $description);
160  $this->assertContains(
161  sprintf('<p>Price: Special Price: 10<br />Special Expires On: %s</p>', date('Y-m-d')),
163  );
164  $this->assertContains(
165  '<img src="image_link" alt="" border="0" align="left" height="75" width="75" />',
167  );
168  }
169 
173  protected function getItemMock()
174  {
175  $item = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
176  ->setMethods([
177  '__wakeup',
178  '__sleep',
179  'getName',
180  'getProductUrl',
181  'getDescription',
182  'getAllowedInRss',
183  'getAllowedPriceInRss',
184  'getSpecialToDate',
185  'getSpecialPrice',
186  'getFinalPrice',
187  'getPrice',
188  'getUseSpecial',
189  ])->disableOriginalConstructor()->getMock();
190  $item->expects($this->once())->method('getAllowedInRss')->will($this->returnValue(true));
191  $item->expects($this->any())->method('getSpecialToDate')->will($this->returnValue(date('Y-m-d')));
192  $item->expects($this->exactly(2))->method('getFinalPrice')->will($this->returnValue(10));
193  $item->expects($this->once())->method('getSpecialPrice')->will($this->returnValue(15));
194  $item->expects($this->exactly(2))->method('getAllowedPriceInRss')->will($this->returnValue(true));
195  $item->expects($this->once())->method('getUseSpecial')->will($this->returnValue(true));
196  $item->expects($this->once())->method('getDescription')->will($this->returnValue('Product Description'));
197  $item->expects($this->once())->method('getName')->will($this->returnValue('Product Name'));
198  $item->expects($this->exactly(2))->method('getProductUrl')
199  ->will($this->returnValue('http://magento.com/product-name.html'));
200 
201  return $item;
202  }
203 
204  public function testIsAllowed()
205  {
206  $this->scopeConfig->expects($this->once())->method('isSetFlag')
207  ->with('rss/catalog/special', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
208  ->will($this->returnValue(true));
209  $this->assertEquals(true, $this->block->isAllowed());
210  }
211 
212  public function testGetCacheLifetime()
213  {
214  $this->assertEquals(600, $this->block->getCacheLifetime());
215  }
216 
217  public function testGetFeeds()
218  {
219  $this->scopeConfig->expects($this->once())->method('isSetFlag')
220  ->with('rss/catalog/special', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
221  ->will($this->returnValue(true));
222  $this->rssUrlBuilder->expects($this->once())->method('getUrl')
223  ->with(['type' => 'special_products'])
224  ->will($this->returnValue('http://magento.com/rss/feed/index/type/special_products/store_id/1'));
225  $expected = [
226  'label' => 'Special Products',
227  'link' => 'http://magento.com/rss/feed/index/type/special_products/store_id/1',
228  ];
229  $this->assertEquals($expected, $this->block->getFeeds());
230  }
231 }