Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DiscountsTest.php
Go to the documentation of this file.
1 <?php
7 
9 
14 class DiscountsTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $block;
20 
25 
30 
34  protected $storeModel;
35 
39  protected $discounts;
40 
45 
50 
54  protected $requestInterface;
55 
60 
64  protected $rssModel;
65 
69  protected $timezoneInterface;
70 
71  protected function setUp()
72  {
73  $this->storeManagerInterface = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
74  $this->requestInterface = $this->createMock(\Magento\Framework\App\RequestInterface::class);
75  $this->rssBuilderInterface = $this->createMock(\Magento\Framework\App\Rss\UrlBuilderInterface::class);
76  $this->urlBuilderInterface = $this->createMock(\Magento\Framework\UrlInterface::class);
77  $this->scopeConfigInterface = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
78  $this->timezoneInterface = $this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
79  $this->discounts = $this->createMock(\Magento\SalesRule\Model\Rss\Discounts::class);
80  $this->rssModel = $this->createPartialMock(\Magento\SalesRule\Model\Rss\Discounts::class, [
81  '__wakeup',
82  'getDiscountCollection'
83  ]);
84  $this->storeModel = $this->createPartialMock(\Magento\Store\Model\Store::class, [
85  '__wakeUp',
86  'getId',
87  'getWebsiteId',
88  'getName',
89  'getFrontendName'
90  ]);
91 
92  $this->storeManagerInterface->expects($this->any())->method('getStore')
93  ->will($this->returnValue($this->storeModel));
94  $this->storeModel->expects($this->any())->method('getId')->will($this->returnValue(1));
95 
96  $this->objectManagerHelper = new ObjectManagerHelper($this);
97  $this->block = $this->objectManagerHelper->getObject(
98  \Magento\SalesRule\Block\Rss\Discounts::class,
99  [
100  'storeManager' => $this->storeManagerInterface,
101  'rssModel' => $this->discounts,
102  'rssUrlBuilder' => $this->rssBuilderInterface,
103  'urlBuilder' => $this->urlBuilderInterface,
104  'request' => $this->requestInterface,
105  'scopeConfig' => $this->scopeConfigInterface,
106  'rssModel' => $this->rssModel,
107  'localeDate' => $this->timezoneInterface
108  ]
109  );
110  }
111 
112  public function testGetRssData()
113  {
114  $ruleData = [
115  'to_date' => '12/12/14',
116  'from_date' => '12/12/14',
117  'coupon_code' => '1234567',
118  'description' => 'Rule Description',
119  'name' => 'Rule Name',
120  ];
121  $rssData = [
122  'title' => 'Frontend Name - Discounts and Coupons',
123  'description' => 'Frontend Name - Discounts and Coupons',
124  'link' => 'http://rss.magento.com/discount',
125  'charset' => 'UTF-8',
126  'language' => 'en_US',
127  'entries' => [
128  'title' => 'Rule Name',
129  'link' => 'http://rss.magento.com',
130  'description' => [
131  'description' => 'Rule Description',
132  'start_date' => '12/12/14',
133  'end_date' => '12/12/14',
134  'coupon_code' => '1234567',
135  ],
136  ],
137  ];
138  $rssUrl = 'http://rss.magento.com/discount';
139  $url = 'http://rss.magento.com';
140 
141  $ruleModel = $this->createPartialMock(\Magento\SalesRule\Model\Rule::class, [
142  '__wakeup',
143  'getCouponCode',
144  'getToDate',
145  'getFromDate',
146  'getDescription',
147  'getName'
148  ]);
149 
150  $this->storeModel->expects($this->once())->method('getWebsiteId')->will($this->returnValue(1));
151  $this->storeModel->expects($this->never())->method('getName');
152  $this->storeModel->expects($this->atLeastOnce())->method('getFrontendName')->willReturn('Frontend Name');
153 
154  $this->requestInterface->expects($this->any())->method('getParam')->will($this->returnValue(1));
155  $this->urlBuilderInterface->expects($this->any())->method('getUrl')->will($this->returnValue($url));
156  $this->rssBuilderInterface->expects($this->any())->method('getUrl')->will($this->returnValue($rssUrl));
157  $this->scopeConfigInterface->expects($this->any())->method('getValue')->will($this->returnValue('en_US'));
158  $ruleModel->expects($this->any())->method('getCouponCode')->will($this->returnValue($ruleData['coupon_code']));
159  $ruleModel->expects($this->any())->method('getToDate')->will($this->returnValue($ruleData['to_date']));
160  $ruleModel->expects($this->once())->method('getFromDate')->will($this->returnValue($ruleData['from_date']));
161  $ruleModel->expects($this->once())->method('getDescription')
162  ->will($this->returnValue($ruleData['description']));
163  $ruleModel->expects($this->once())->method('getName')->will($this->returnValue($ruleData['name']));
164  $this->rssModel->expects($this->any())->method('getDiscountCollection')
165  ->will($this->returnValue([$ruleModel]));
166  $this->timezoneInterface->expects($this->any())->method('formatDateTime')->will($this->returnValue('12/12/14'));
167 
168  $data = $this->block->getRssData();
169 
170  $this->assertEquals($rssData['title'], $data['title']);
171  $this->assertEquals($rssData['description'], $data['description']);
172  $this->assertEquals($rssData['link'], $data['link']);
173  $this->assertEquals($rssData['charset'], $data['charset']);
174  $this->assertEquals($rssData['language'], $data['language']);
175  $this->assertEquals($rssData['entries']['title'], $data['entries'][0]['title']);
176  $this->assertEquals($rssData['entries']['link'], $data['entries'][0]['link']);
177  $this->assertContains($rssData['entries']['description']['description'], $data['entries'][0]['description']);
178  $this->assertContains($rssData['entries']['description']['start_date'], $data['entries'][0]['description']);
179  $this->assertContains($rssData['entries']['description']['end_date'], $data['entries'][0]['description']);
180  $this->assertContains($rssData['entries']['description']['coupon_code'], $data['entries'][0]['description']);
181  }
182 
183  public function testGetCacheLifetime()
184  {
185  $this->assertEquals(0, $this->block->getCacheLifetime());
186  }
187 
192  public function testIsAllowed($isAllowed)
193  {
194  $this->scopeConfigInterface->expects($this->once())->method('isSetFlag')->will($this->returnValue($isAllowed));
195  $this->assertEquals($isAllowed, $this->block->isAllowed());
196  }
197 
201  public function isAllowedDataProvider()
202  {
203  return [
204  [true],
205  [false]
206  ];
207  }
208 
209  public function testGetFeeds()
210  {
211  $feedData = [
212  'label' => 'Coupons/Discounts',
213  'link' => 'http://rss.magento.com/discount',
214  ];
215  $this->rssBuilderInterface->expects($this->any())
216  ->method('getUrl')
217  ->will($this->returnValue($feedData['link']));
218 
219  $this->scopeConfigInterface->expects($this->once())->method('isSetFlag')->will($this->returnValue(true));
220  $this->assertEquals($feedData, $this->block->getFeeds());
221  }
222 }
$ruleData
Definition: tax_rule.php:26
$isAllowed
Definition: get.php:20