1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
*/
namespace Temando\Shipping\Controller\Adminhtml\Dispatch;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\AbstractBackendController;
use Temando\Shipping\Model\Dispatch;
use Temando\Shipping\Model\DispatchProvider;
use Temando\Shipping\Model\ResourceModel\Dispatch\DispatchRepository;
/**
* @magentoAppArea adminhtml
*/
class ViewTest extends AbstractBackendController
{
/**
* The resource used to authorize action
*
* @var string
*/
protected $resource = 'Temando_Shipping::dispatches';
/**
* The uri at which to access the controller
*
* @var string
*/
protected $uri = 'backend/temando/dispatch/view';
/**
* @var DispatchRepository|\PHPUnit_Framework_MockObject_MockObject
*/
private $dispatchRepo;
protected function setUp()
{
parent::setUp();
$this->dispatchRepo = $this->getMockBuilder(DispatchRepository::class)
->setMethods(['getById'])
->disableOriginalConstructor()
->getMock();
Bootstrap::getObjectManager()->addSharedInstance($this->dispatchRepo, DispatchRepository::class);
}
protected function tearDown()
{
Bootstrap::getObjectManager()->removeSharedInstance(DispatchProvider::class);
parent::tearDown();
}
/**
* @param string $dispatchId
* @return Dispatch
*/
private function createDispatch($dispatchId)
{
$status = 'processed';
$carrierName = 'Foo';
$carrierMessages = ['Message Foo', 'Message Bar'];
$createdAtDate = '1999-01-19T03:03:33.000Z';
$readyAtDate = '2099-01-19T03:03:33.000Z';
$pickupNumbers = ['pnum 123', 'pnum 987'];
$pickupCharges = [
new Dispatch\PickupCharge([
Dispatch\PickupChargeInterface::DESCRIPTION => 'Treats',
Dispatch\PickupChargeInterface::AMOUNT => 0.99,
Dispatch\PickupChargeInterface::CURRENCY => 'AUD'
]),
new Dispatch\PickupCharge([
Dispatch\PickupChargeInterface::DESCRIPTION => 'Sweets',
Dispatch\PickupChargeInterface::AMOUNT => 3.03,
Dispatch\PickupChargeInterface::CURRENCY => 'AUD'
]),
];
$includedShipments = [
new Dispatch\Shipment([
Dispatch\ShipmentInterface::SHIPMENT_ID => '1234-ship',
Dispatch\ShipmentInterface::STATUS => 'fulfilled',
]),
];
$failedShipments = [];
$documentation = [];
$dispatch = new Dispatch([
Dispatch::DISPATCH_ID => $dispatchId,
Dispatch::STATUS => $status,
Dispatch::CARRIER_NAME => $carrierName,
Dispatch::CARRIER_MESSAGES => $carrierMessages,
Dispatch::CREATED_AT_DATE => $createdAtDate,
Dispatch::READY_AT_DATE => $readyAtDate,
Dispatch::PICKUP_NUMBERS => $pickupNumbers,
Dispatch::PICKUP_CHARGES => $pickupCharges,
Dispatch::INCLUDED_SHIPMENTS => $includedShipments,
Dispatch::FAILED_SHIPMENTS => $failedShipments,
Dispatch::DOCUMENTATION => $documentation,
]);
return $dispatch;
}
/**
* @test
* @magentoConfigFixture default/carriers/temando/account_id 23
* @magentoConfigFixture default/carriers/temando/bearer_token 808
*/
public function dispatchLoadSuccess()
{
$dispatchId = '1234-abcd';
$dispatch = $this->createDispatch($dispatchId);
$this->dispatchRepo
->expects($this->once())
->method('getById')
->with($dispatchId)
->willReturn($dispatch);
$this->getRequest()->setParam('dispatch_id', $dispatchId);
$this->dispatch($this->uri);
$this->assertContains($dispatch->getCarrierName(), $this->getResponse()->getBody());
$this->assertContains('Documentation', $this->getResponse()->getBody());
foreach ($dispatch->getPickupCharges() as $pickupCharge) {
$this->assertContains($pickupCharge->getDescription(), $this->getResponse()->getBody());
}
foreach ($dispatch->getCarrierMessages() as $carrierMessage) {
$this->assertContains($carrierMessage, $this->getResponse()->getBody());
}
}
/**
* @test
* @magentoConfigFixture default/carriers/temando/account_id 23
* @magentoConfigFixture default/carriers/temando/bearer_token 808
*/
public function dispatchLoadError()
{
$dispatchId = '1234-abcd';
$this->dispatchRepo
->expects($this->once())
->method('getById')
->with($dispatchId)
->willThrowException(new NoSuchEntityException(__('Not found.')));
$this->getRequest()->setParam('dispatch_id', $dispatchId);
$this->dispatch($this->uri);
$this->assertNotContains('Documentation', $this->getResponse()->getBody());
}
/**
* @test
* @magentoConfigFixture default/carriers/temando/account_id 23
* @magentoConfigFixture default/carriers/temando/bearer_token 808
*/
public function testAclHasAccess()
{
$dispatchId = '1234-abcd';
$dispatch = $this->createDispatch($dispatchId);
$this->dispatchRepo
->expects($this->once())
->method('getById')
->with($dispatchId)
->willReturn($dispatch);
$this->getRequest()->setParam('dispatch_id', $dispatchId);
parent::testAclHasAccess();
}
/**
* @test
* @magentoConfigFixture default/carriers/temando/account_id 23
* @magentoConfigFixture default/carriers/temando/bearer_token 808
*/
public function testAclNoAccess()
{
$dispatchId = '1234-abcd';
$dispatch = $this->createDispatch($dispatchId);
$this->dispatchRepo
->expects($this->once())
->method('getById')
->with($dispatchId)
->willReturn($dispatch);
$this->getRequest()->setParam('dispatch_id', $dispatchId);
parent::testAclNoAccess();
}
}