CarrierTest::mockWeightUnit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Meanbee\MagentoRoyalmail\Test\Unit\Model;
4
5
use Meanbee\MagentoRoyalmail\Model\Carrier;
6
use Meanbee\MagentoRoyalmail\Model\Config\Source\ParcelSize;
7
use Meanbee\Royalmail\Method;
8
9
/**
10
 * Class CarrierTest
11
 */
12
class CarrierTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @var Carrier
16
     */
17
    protected $model;
18
19
    /**
20
     * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
21
     */
22
    protected $scopeConfig;
23
24
    /**
25
     * @var \Magento\Shipping\Model\Rate\Result|\PHPUnit_Framework_MockObject_MockObject
26
     */
27
    protected $rateResultFactory;
28
29
    /**
30
     * @var \Magento\Shipping\Model\Rate\Result|\PHPUnit_Framework_MockObject_MockObject
31
     */
32
    protected $rateResult;
33
34
    /**
35
     * @var \Magento\Quote\Model\Quote\Address\RateResult\Method|\PHPUnit_Framework_MockObject_MockObject
36
     */
37
    protected $rateMethodFactory;
38
39
    /**
40
     * @var \Magento\Quote\Model\Quote\Address\RateResult\Method|\PHPUnit_Framework_MockObject_MockObject
41
     */
42
    protected $rateResultMethod;
43
44
    /**
45
     * @var \Meanbee\MagentoRoyalmail\Model\Rounder
46
     */
47
    protected $rounder;
48
49
    /**
50
     * @var \Meanbee\Royalmail\Carrier
51
     */
52
    protected $libCarrier;
53
54
    /**
55
     * @inheritdoc
56
     */
57
    protected function setUp()
58
    {
59
        $this->scopeConfig = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface')
60
            ->getMockForAbstractClass();
61
        $rateErrorFactory = $this->getMockBuilder('Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory')
62
            ->disableOriginalConstructor()
63
            ->setMethods(['create'])
64
            ->getMock();
65
        $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')
66
            ->getMockForAbstractClass();
67
        $this->rateResultFactory = $this->getMockBuilder('Magento\Shipping\Model\Rate\ResultFactory')
68
            ->disableOriginalConstructor()
69
            ->setMethods(['create'])
70
            ->getMock();
71
        $this->rateResult = $this->getMockBuilder('Magento\Shipping\Model\Rate\Result')
72
            ->disableOriginalConstructor()
73
            ->getMock();
74
        $this->rateMethodFactory = $this->getMockBuilder('Magento\Quote\Model\Quote\Address\RateResult\MethodFactory')
75
            ->disableOriginalConstructor()
76
            ->setMethods(['create'])
77
            ->getMock();
78
        $this->rateResultMethod = $this->getMockBuilder('Magento\Quote\Model\Quote\Address\RateResult\Method')
79
            ->disableOriginalConstructor()
80
            ->getMock();
81
        $this->rounder = $this->getMockBuilder('Meanbee\MagentoRoyalmail\Model\Rounder')
82
            ->disableOriginalConstructor()
83
            ->setMethods(['round'])
84
            ->getMock();
85
        $this->libCarrier = $this->getMockBuilder('Meanbee\Royalmail\Carrier')
86
            ->disableOriginalConstructor()
87
            ->getMock();
88
89
        $this->rateResultFactory->expects($this->any())->method('create')->willReturn($this->rateResult);
90
        $this->rateMethodFactory->expects($this->any())->method('create')->willReturn($this->rateResultMethod);
91
92
        $this->model = new Carrier(
93
            $this->scopeConfig,
94
            $rateErrorFactory,
95
            $logger,
96
            $this->rateResultFactory,
97
            $this->rateMethodFactory,
98
            $this->rounder
99
        );
100
        $this->model->setCarrier($this->libCarrier);
0 ignored issues
show
Deprecated Code introduced by
The method Meanbee\MagentoRoyalmail...l\Carrier::setCarrier() has been deprecated.

This method has been deprecated.

Loading history...
101
    }
102
103
    public function testGetAllowedMethods()
104
    {
105
        $methods = "i11lstdltr,i11lstdlrgltr";
106
        $code = 'i11lstdltr';
107
        $name = "International Standard Letter";
108
109
        $this->scopeConfig
110
            ->expects($this->at(0))
111
            ->method('getValue')
112
            ->with()
113
            ->willReturn($methods);
114
        $this->mockAllMethods([$code => $name]);
115
116
        $methods = $this->model->getAllowedMethods();
117
118
        $this->assertArrayHasKey($code, $methods);
119
        $this->assertEquals($name, $methods[$code]);
120
    }
121
122
    public function testGetMethods()
123
    {
124
        $methods = ['i11lstdltr' => "International Standard Letter"];
125
        $this->mockAllMethods($methods);
126
        $this->assertEquals($methods, $this->model->getMethods());
127
    }
128
129
    public function testCollectRatesNotActive()
130
    {
131
        $this->mockIsActive(false);
132
        $this->assertFalse($this->model->collectRates($this->getRateRequest()));
133
    }
134
135 View Code Duplication
    public function testCollectRatesNoAllowedMethods()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        $this->mockIsActive(true);
138
        $rate = $this->getMethod('International Standard Small Parcel');
139
140
        $this->mockMethodsConfigured(false);
141
        $this->mockLibraryRates([$rate]);
142
        $this->mockAllMethods(['i11lstdsmprcl'
143
            => "International Standard Small Parcel"
144
        ]);
145
        $this->mockHasResults(false);
146
147
        $result = $this->model->collectRates($this->getRateRequest());
148
        $this->assertInstanceOf('Magento\Shipping\Model\Rate\Result', $result);
149
150
    }
151
152 View Code Duplication
    public function testCollectRatesDisabledMethods()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154
        $this->mockIsActive(true);
155
156
        $rate = $this->getMethod('International Standard Medium Parcel');
157
158
        $this->mockMethodsConfigured('i11lstdsmprcl');
159
        $this->mockLibraryRates([$rate]);
160
        $this->mockAllMethods(['i11lstdsmprcl'
161
            => "International Standard Small Parcel"
162
        ]);
163
        $this->mockHasResults(false);
164
165
        $result = $this->model->collectRates($this->getRateRequest());
166
        $this->assertInstanceOf('Magento\Shipping\Model\Rate\Result', $result);
167
    }
168
169 View Code Duplication
    public function testCollectRatesMediumParcels()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
    {
171
        $this->mockIsActive(true);
172
        $rate = $this->getMethod('International Standard Small Parcel');
173
        $this->mockMethodsConfigured('i11lstdsmprcl');
174
        $this->mockLibraryRates([$rate]);
175
        $this->mockParcelSize(ParcelSize::MEDIUM);
176
        $this->mockAllMethods(['i11lstdsmprcl'
177
            => "International Standard Small Parcel"
178
        ]);
179
        $this->mockHasResults(false);
180
181
        $result = $this->model->collectRates($this->getRateRequest());
182
        $this->assertInstanceOf('Magento\Shipping\Model\Rate\Result', $result);
183
    }
184
185
    public function testCollectRatesInLbs()
186
    {
187
        $this->mockIsActive(true);
188
        $this->mockWeightUnit('lbs');
189
190
        $this->mockMethodsConfigured('internationalstandardsmallparcel');
191
192
        $rateRequest = $this->getRateRequest();
193
        $rateRequest->expects($this->once())
194
            ->method('getPackageWeight')
195
            ->willReturn(1);
196
197
        $this->libCarrier
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Meanbee\Royalmail\Carrier>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
198
            ->expects($this->once())
199
            ->method('getRates')
200
            ->with(null, null, 0.453592, false)
201
            ->willReturn($this->getMethod('International Standard Small Parcel'));
202
203
        $this->mockAllMethods(['internationalstandardsmallparcel'
204
            => "International Standard Small Parcel"
205
        ]);
206
207
        $this->mockHasResults(false);
208
209
        $this->model->collectRates($rateRequest);
210
211
    }
212
213 View Code Duplication
    public function testCollectRates()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
214
    {
215
        $this->mockIsActive(true);
216
        $rate = $this->getMethod('International Standard Small Parcel');
217
        $this->mockMethodsConfigured('i11lstdsmprcl');
218
        $this->mockLibraryRates([$rate]);
219
        $this->mockAllMethods(['i11lstdsmprcl'
220
            => "International Standard Small Parcel"
221
        ]);
222
        $this->mockHasResults(true);
223
224
        $result = $this->model->collectRates($this->getRateRequest());
225
        $this->assertInstanceOf('Magento\Shipping\Model\Rate\Result', $result);
226
    }
227
228
    /**
229
     * Get a shipping Method
230
     *
231
     * @param $name
232
     * @return Method
233
     */
234
    protected function getMethod($name)
235
    {
236
        return new Method(
237
            str_replace(' ', '_', strtoupper($name)),
238
            str_replace(' ', '', strtolower($name)),
239
            $name,
240
            'US',
241
            13.05,
242
            20,
243
            0.751,
244
            1,
245
            ParcelSize::SMALL
246
        );
247
    }
248
249
    /**
250
     * Get rate request
251
     *
252
     * @return \Magento\Quote\Model\Quote\Address\RateRequest
253
     */
254
    protected function getRateRequest()
255
    {
256
        /** @var \Magento\Quote\Model\Quote\Address\RateRequest $request */
257
        return $this->getMockBuilder('Magento\Quote\Model\Quote\Address\RateRequest')
258
            ->disableOriginalConstructor()
259
            ->setMethods(['getPackageWeight'])
260
            ->getMock();
261
    }
262
263
    /**
264
     * @param $result
265
     */
266
    protected function mockIsActive($result)
267
    {
268
        $this->scopeConfig
269
            ->expects($this->at(0))
270
            ->method('getValue')
271
            ->with('carriers/rm/active')
272
            ->willReturn($result);
273
    }
274
275
    protected function mockWeightUnit($unit)
276
    {
277
        $this->scopeConfig
278
            ->expects($this->at(1))
279
            ->method('getValue')
280
            ->with(\Magento\Directory\Helper\Data::XML_PATH_WEIGHT_UNIT)
281
            ->willReturn($unit);
282
    }
283
284
    /**
285
     * @param $parcelSize
286
     */
287
    protected function mockParcelSize($parcelSize)
288
    {
289
        $this->scopeConfig
290
            ->expects($this->at(2))
291
            ->method('getValue')
292
            ->with('carriers/rm/parcel_size')
293
            ->willReturn($parcelSize);
294
    }
295
296
    /**
297
     * @param $methods
298
     */
299
    protected function mockMethodsConfigured($methods)
300
    {
301
        $this->scopeConfig
302
            ->expects($this->at(3))
303
            ->method('getValue')
304
            ->with()
305
            ->willReturn($methods);
306
    }
307
308
    /**
309
     * @param $rates
310
     */
311
    protected function mockLibraryRates($rates)
312
    {
313
        $this->libCarrier
314
            ->expects($this->once())
315
            ->method('getRates')
316
            ->willReturn($rates);
317
    }
318
319
    /**
320
     * @param $allMethods
321
     */
322
    protected function mockAllMethods($allMethods)
323
    {
324
        $this->libCarrier
325
            ->expects($this->once())
326
            ->method('getAllMethods')
327
            ->willReturn($allMethods);
328
    }
329
330
    /**
331
     * @param bool $result
332
     */
333
    protected function mockHasResults($result = true)
334
    {
335
        $matcher = $this->atLeastOnce();
336
        if (!$result) {
337
            $matcher = $this->never();
338
        }
339
340
        $this->rateResult
341
            ->expects($matcher)
342
            ->method('append')
343
            ->with($this->rateResultMethod);
344
    }
345
}
346