Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
IsAllowedGuestCheckoutObserverTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Downloadable\Model\ResourceModel\Link\Purchased\Item\CollectionFactory;
13 
17 class IsAllowedGuestCheckoutObserverTest extends \PHPUnit\Framework\TestCase
18 {
20  private $isAllowedGuestCheckoutObserver;
21 
25  private $scopeConfig;
26 
30  private $resultMock;
31 
35  private $eventMock;
36 
40  private $observerMock;
41 
45  private $storeMock;
46 
51  protected function setUp()
52  {
53  $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config::class)
54  ->disableOriginalConstructor()
55  ->setMethods(['isSetFlag', 'getValue'])
56  ->getMock();
57 
58  $this->resultMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
59  ->disableOriginalConstructor()
60  ->setMethods(['setIsAllowed'])
61  ->getMock();
62 
63  $this->eventMock = $this->getMockBuilder(\Magento\Framework\Event::class)
64  ->disableOriginalConstructor()
65  ->setMethods(['getStore', 'getResult', 'getQuote', 'getOrder'])
66  ->getMock();
67 
68  $this->observerMock = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
69  ->disableOriginalConstructor()
70  ->setMethods(['getEvent'])
71  ->getMock();
72 
73  $this->storeMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
74  ->disableOriginalConstructor()
75  ->getMock();
76 
77  $this->isAllowedGuestCheckoutObserver = (new ObjectManagerHelper($this))->getObject(
78  \Magento\Downloadable\Observer\IsAllowedGuestCheckoutObserver::class,
79  [
80  'scopeConfig' => $this->scopeConfig,
81  ]
82  );
83  }
84 
93  {
94  if ($isAllowed) {
95  $this->resultMock->expects($this->at(0))
96  ->method('setIsAllowed')
97  ->with(false);
98  }
99 
100  $product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
101  ->disableOriginalConstructor()
102  ->setMethods(['getTypeId'])
103  ->getMock();
104 
105  $product->expects($this->once())
106  ->method('getTypeId')
107  ->willReturn($productType);
108 
109  $item = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
110  ->disableOriginalConstructor()
111  ->setMethods(['getProduct'])
112  ->getMock();
113 
114  $item->expects($this->once())
115  ->method('getProduct')
116  ->willReturn($product);
117 
118  $quote = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
119  ->disableOriginalConstructor()
120  ->setMethods(['getAllItems'])
121  ->getMock();
122 
123  $quote->expects($this->once())
124  ->method('getAllItems')
125  ->willReturn([$item]);
126 
127  $this->eventMock->expects($this->once())
128  ->method('getStore')
129  ->will($this->returnValue($this->storeMock));
130 
131  $this->eventMock->expects($this->once())
132  ->method('getResult')
133  ->will($this->returnValue($this->resultMock));
134 
135  $this->eventMock->expects($this->once())
136  ->method('getQuote')
137  ->will($this->returnValue($quote));
138 
139  $this->scopeConfig->expects($this->once())
140  ->method('isSetFlag')
141  ->with(
144  $this->storeMock
145  )
146  ->willReturn(true);
147 
148  $this->observerMock->expects($this->exactly(3))
149  ->method('getEvent')
150  ->will($this->returnValue($this->eventMock));
151 
152  $this->assertInstanceOf(
153  \Magento\Downloadable\Observer\IsAllowedGuestCheckoutObserver::class,
154  $this->isAllowedGuestCheckoutObserver->execute($this->observerMock)
155  );
156  }
157 
162  {
163  return [
164  1 => [Type::TYPE_DOWNLOADABLE, true],
165  2 => ['unknown', false],
166  ];
167  }
168 
170  {
171  $this->eventMock->expects($this->once())
172  ->method('getStore')
173  ->will($this->returnValue($this->storeMock));
174 
175  $this->eventMock->expects($this->once())
176  ->method('getResult')
177  ->will($this->returnValue($this->resultMock));
178 
179  $this->scopeConfig->expects($this->once())
180  ->method('isSetFlag')
181  ->with(
184  $this->storeMock
185  )
186  ->willReturn(false);
187 
188  $this->observerMock->expects($this->exactly(2))
189  ->method('getEvent')
190  ->will($this->returnValue($this->eventMock));
191 
192  $this->assertInstanceOf(
193  \Magento\Downloadable\Observer\IsAllowedGuestCheckoutObserver::class,
194  $this->isAllowedGuestCheckoutObserver->execute($this->observerMock)
195  );
196  }
197 }
$quote
$isAllowed
Definition: get.php:20