Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreateGuaranteeAbilityTest.php
Go to the documentation of this file.
1 <?php
7 
16 
20 class CreateGuaranteeAbilityTest extends \PHPUnit\Framework\TestCase
21 {
25  private $dateTimeFactory;
26 
30  private $orderRepository;
31 
35  private $caseManagement;
36 
40  private $createGuaranteeAbility;
41 
45  protected function setUp()
46  {
47  $this->dateTimeFactory = new DateTimeFactory();
48  $this->orderRepository = $this->getMockBuilder(OrderRepositoryInterface::class)
49  ->getMockForAbstractClass();
50  $this->caseManagement = $this->getMockBuilder(CaseManagement::class)
51  ->disableOriginalConstructor()
52  ->getMock();
53 
54  $this->createGuaranteeAbility = new CreateGuaranteeAbility(
55  $this->caseManagement,
56  $this->orderRepository,
57  $this->dateTimeFactory
58  );
59  }
60 
61  public function testIsAvailableSuccess()
62  {
63  $orderId = 123;
64  $orderCreatedAt = $this->getDateAgo(6);
65 
67  $case = $this->getMockBuilder(CaseInterface::class)
68  ->disableOriginalConstructor()
69  ->getMock();
70  $case->expects($this->once())
71  ->method('isGuaranteeEligible')
72  ->willReturn(true);
73 
74  $this->caseManagement->expects($this->once())
75  ->method('getByOrderId')
76  ->with($orderId)
77  ->willReturn($case);
78 
80  $order = $this->getMockBuilder(OrderInterface::class)
81  ->getMockForAbstractClass();
82  $order->expects($this->once())
83  ->method('getState')
84  ->willReturn(Order::STATE_COMPLETE);
85  $order->expects($this->once())
86  ->method('getCreatedAt')
87  ->willReturn($orderCreatedAt);
88 
89  $this->orderRepository->expects($this->once())
90  ->method('get')
91  ->with($orderId)
92  ->willReturn($order);
93 
94  $this->assertTrue($this->createGuaranteeAbility->isAvailable($orderId));
95  }
96 
100  public function testIsAvailableWithNullCase()
101  {
102  $orderId = 123;
103 
104  $this->caseManagement->expects($this->once())
105  ->method('getByOrderId')
106  ->with($orderId)
107  ->willReturn(null);
108 
109  $this->assertFalse($this->createGuaranteeAbility->isAvailable($orderId));
110  }
111 
115  public function testIsAvailableWithGuarantyEligibleFalse()
116  {
117  $orderId = 123;
118 
120  $case = $this->getMockBuilder(CaseInterface::class)
121  ->disableOriginalConstructor()
122  ->getMock();
123  $case->expects($this->once())
124  ->method('isGuaranteeEligible')
125  ->willReturn(false);
126 
127  $this->caseManagement->expects($this->once())
128  ->method('getByOrderId')
129  ->with($orderId)
130  ->willReturn($case);
131 
132  $this->assertFalse($this->createGuaranteeAbility->isAvailable($orderId));
133  }
134 
138  public function testIsAvailableWithNullOrder()
139  {
140  $orderId = 123;
141 
143  $case = $this->getMockBuilder(CaseInterface::class)
144  ->disableOriginalConstructor()
145  ->getMock();
146  $case->expects($this->once())
147  ->method('isGuaranteeEligible')
148  ->willReturn(true);
149 
150  $this->caseManagement->expects($this->once())
151  ->method('getByOrderId')
152  ->with($orderId)
153  ->willReturn($case);
154 
155  $this->orderRepository->expects($this->once())
156  ->method('get')
157  ->with($orderId)
158  ->willThrowException(new NoSuchEntityException());
159 
160  $this->assertFalse($this->createGuaranteeAbility->isAvailable($orderId));
161  }
162 
169  public function testIsAvailableWithCanceledOrder($state)
170  {
171  $orderId = 123;
172 
174  $case = $this->getMockBuilder(CaseInterface::class)
175  ->disableOriginalConstructor()
176  ->getMock();
177  $case->expects($this->once())
178  ->method('isGuaranteeEligible')
179  ->willReturn(true);
180 
181  $this->caseManagement->expects($this->once())
182  ->method('getByOrderId')
183  ->with($orderId)
184  ->willReturn($case);
185 
187  $order = $this->getMockBuilder(OrderInterface::class)
188  ->getMockForAbstractClass();
189  $order->expects($this->once())
190  ->method('getState')
191  ->willReturn($state);
192 
193  $this->orderRepository->expects($this->once())
194  ->method('get')
195  ->with($orderId)
196  ->willReturn($order);
197 
198  $this->assertFalse($this->createGuaranteeAbility->isAvailable($orderId));
199  }
200 
205  {
206  return [
208  ];
209  }
210 
211  public function testIsAvailableWithOldOrder()
212  {
213  $orderId = 123;
214  $orderCreatedAt = $this->getDateAgo(8);
215 
217  $case = $this->getMockBuilder(CaseInterface::class)
218  ->disableOriginalConstructor()
219  ->getMock();
220  $case->expects($this->once())
221  ->method('isGuaranteeEligible')
222  ->willReturn(true);
223 
224  $this->caseManagement->expects($this->once())
225  ->method('getByOrderId')
226  ->with($orderId)
227  ->willReturn($case);
228 
230  $order = $this->getMockBuilder(OrderInterface::class)
231  ->getMockForAbstractClass();
232  $order->expects($this->once())
233  ->method('getState')
234  ->willReturn(Order::STATE_COMPLETE);
235  $order->expects($this->once())
236  ->method('getCreatedAt')
237  ->willReturn($orderCreatedAt);
238 
239  $this->orderRepository->expects($this->once())
240  ->method('get')
241  ->with($orderId)
242  ->willReturn($order);
243 
244  $this->assertFalse($this->createGuaranteeAbility->isAvailable($orderId));
245  }
246 
253  private function getDateAgo($days)
254  {
255  $createdAtTime = $this->dateTimeFactory->create('now', new \DateTimeZone('UTC'));
256  $createdAtTime->sub(new \DateInterval('P' . $days . 'D'));
257 
258  return $createdAtTime->format('Y-m-d h:i:s');
259  }
260 }
$case
$order
Definition: order.php:55