Passed
Push — master ( 864d96...a8acdf )
by Marc
01:03 queued 11s
created

OrangePaymentSlipDataTest::testGetCodeLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 69
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 39
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 69
rs 9.296

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Swiss Payment Slip
4
 *
5
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
6
 * @copyright 2012-2016 Some nice Swiss guys
7
 * @author Marc Würth [email protected]
8
 * @author Manuel Reinhard <[email protected]>
9
 * @author Peter Siska <[email protected]>
10
 * @link https://github.com/ravage84/SwissPaymentSlip/
11
 */
12
13
namespace SwissPaymentSlip\SwissPaymentSlip\Tests;
14
15
use SwissPaymentSlip\SwissPaymentSlip\OrangePaymentSlipData;
16
17
/**
18
 * Tests for the OrangePaymentSlipData class
19
 *
20
 * @coversDefaultClass SwissPaymentSlip\SwissPaymentSlip\OrangePaymentSlipData
21
 */
22
class OrangePaymentSlipDataTest extends \PHPUnit_Framework_TestCase
23
{
24
    /**
25
     * The object under test
26
     *
27
     * @var OrangePaymentSlipData
28
     */
29
    protected $slipData;
30
31
    /**
32
     * Setup the object under test
33
     *
34
     * @return void
35
     */
36
    protected function setUp()
37
    {
38
        $this->slipData = new OrangePaymentSlipData();
39
    }
40
41
    /**
42
     * Tests the getWithReferenceNumber and setWithReferenceNumber methods
43
     *
44
     * @return void
45
     * @covers ::setWithReferenceNumber
46
     * @covers ::getWithReferenceNumber
47
     * @covers ::isBool
48
     * @covers ::setReferenceNumber
49
     * @covers ::getReferenceNumber
50
     */
51
    public function testSetWithReferenceNumber()
52
    {
53
        // Test default values
54
        $this->assertEquals('', $this->slipData->getReferenceNumber());
55
        $this->assertTrue($this->slipData->getWithReferenceNumber());
56
57
        // Set data when enabled, also check for returned instance
58
        $returned = $this->slipData->setReferenceNumber('0123456789');
59
        $this->assertInstanceOf('SwissPaymentSlip\SwissPaymentSlip\OrangePaymentSlipData', $returned);
60
        $this->assertEquals('0123456789', $this->slipData->getReferenceNumber());
61
62
        // Disable feature, also check for returned instance
63
        $returned = $this->slipData->setWithReferenceNumber(false);
64
        $this->assertInstanceOf('SwissPaymentSlip\SwissPaymentSlip\OrangePaymentSlipData', $returned);
65
        $this->assertFalse($this->slipData->getWithReferenceNumber());
66
67
        // Re-enable feature, using no parameter
68
        $this->slipData->setWithReferenceNumber();
69
        $this->assertTrue($this->slipData->getWithReferenceNumber());
70
        $this->assertEquals('', $this->slipData->getReferenceNumber());
71
    }
72
73
    /**
74
     * Tests the setReferenceNumber method when disabled
75
     *
76
     * @return void
77
     * @expectedException \SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException
78
     * @expectedExceptionMessage You are accessing the disabled reference number. You need to re-enable it first.
79
     * @covers ::setReferenceNumber
80
     */
81
    public function testSetReferenceNumberWhenDisabled()
82
    {
83
        $this->slipData->setWithReferenceNumber(false);
84
        $this->slipData->setReferenceNumber('');
85
    }
86
87
    /**
88
     * Tests the getReferenceNumber method when disabled
89
     *
90
     * @return void
91
     * @expectedException \SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException
92
     * @expectedExceptionMessage You are accessing the disabled reference number. You need to re-enable it first.
93
     * @covers ::getReferenceNumber
94
     */
95
    public function testGetReferenceNumberWhenDisabled()
96
    {
97
        $this->slipData->setWithReferenceNumber(false);
98
        $this->slipData->getReferenceNumber();
99
    }
100
101
    /**
102
     * Tests the setWithReferenceNumber method with an invalid parameter
103
     *
104
     * @return void
105
     * @expectedException \InvalidArgumentException
106
     * @expectedExceptionMessage $withReferenceNumber is not a boolean.
107
     * @covers ::setWithReferenceNumber
108
     * @covers ::isBool
109
     */
110
    public function testSetWithReferenceNumberInvalidParameter()
111
    {
112
        $this->slipData->setWithReferenceNumber(1);
113
    }
114
115
    /**
116
     * Tests the getWithBankingCustomerId and setWithBankingCustomerId methods
117
     *
118
     * @return void
119
     * @covers ::setWithBankingCustomerId
120
     * @covers ::getWithBankingCustomerId
121
     * @covers ::isBool
122
     * @covers ::setBankingCustomerId
123
     * @covers ::getBankingCustomerId
124
     */
125
    public function testSetWithBankingCustomerId()
126
    {
127
        // Test default values
128
        $this->assertEquals('', $this->slipData->getBankingCustomerId());
129
        $this->assertTrue($this->slipData->getWithBankingCustomerId());
130
131
        // Set data when enabled, also check for returned instance
132
        $returned = $this->slipData->setBankingCustomerId('012345');
133
        $this->assertInstanceOf('SwissPaymentSlip\SwissPaymentSlip\OrangePaymentSlipData', $returned);
134
        $this->assertEquals('012345', $this->slipData->getBankingCustomerId());
135
136
        // Disable feature, also check for returned instance
137
        $returned = $this->slipData->setWithBankingCustomerId(false);
138
        $this->assertInstanceOf('SwissPaymentSlip\SwissPaymentSlip\OrangePaymentSlipData', $returned);
139
        $this->assertFalse($this->slipData->getWithBankingCustomerId());
140
141
        // Re-enable feature, using no parameter
142
        $this->slipData->setWithBankingCustomerId();
143
        $this->assertTrue($this->slipData->getWithBankingCustomerId());
144
        $this->assertEquals('', $this->slipData->getBankingCustomerId());
145
    }
146
147
    /**
148
     * Tests the setBankingCustomerId method when disabled
149
     *
150
     * @return void
151
     * @expectedException \SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException
152
     * @expectedExceptionMessage You are accessing the disabled banking customer ID. You need to re-enable it first.
153
     * @covers ::setBankingCustomerId
154
     */
155
    public function testSetBankingCustomerIdNumberWhenDisabled()
156
    {
157
        $this->slipData->setWithBankingCustomerId(false);
158
        $this->slipData->setBankingCustomerId('');
159
    }
160
161
    /**
162
     * Tests the getBankingCustomerId method when disabled
163
     *
164
     * @return void
165
     * @expectedException \SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException
166
     * @expectedExceptionMessage You are accessing the disabled banking customer ID. You need to re-enable it first.
167
     * @covers ::getBankingCustomerId
168
     */
169
    public function testGetBankingCustomerIdNumberWhenDisabled()
170
    {
171
        $this->slipData->setWithBankingCustomerId(false);
172
        $this->slipData->getBankingCustomerId();
173
    }
174
175
    /**
176
     * Tests the setWithBankingCustomerId method with an invalid parameter
177
     *
178
     * @return void
179
     * @expectedException \InvalidArgumentException
180
     * @expectedExceptionMessage $withBankingCustomerId is not a boolean.
181
     * @covers ::setWithBankingCustomerId
182
     * @covers ::isBool
183
     */
184
    public function testSetWithBankingCustomerIdInvalidParameter()
185
    {
186
        $this->slipData->setWithBankingCustomerId(1);
187
    }
188
189
    /**
190
     * Tests the getCompleteReferenceNumber method
191
     *
192
     * @return void
193
     * @covers ::getCompleteReferenceNumber
194
     * @covers ::appendCheckDigit
195
     * @covers ::breakStringIntoBlocks
196
     * @covers ::modulo10
197
     */
198
    public function testGetCompleteReferenceNumber()
199
    {
200
        // Test with reference number & banking customer ID
201
        $this->slipData->setReferenceNumber('7520033455900012');
202
        $this->slipData->setBankingCustomerId('215703');
203
204
        // Formatted and filled with zeros
205
        $this->assertEquals(
206
            '21 57030 00075 20033 45590 00126',
207
            $this->slipData->getCompleteReferenceNumber()
208
        );
209
        // Not formatted but filled with zeros
210
        $this->assertEquals(
211
            '215703000075200334559000126',
212
            $this->slipData->getCompleteReferenceNumber(false)
213
        );
214
        // Formatted but not filled with zeros
215
        $this->assertEquals(
216
            '21 57030 00075 20033 45590 00126',
217
            $this->slipData->getCompleteReferenceNumber(true, false)
218
        );
219
        // Neither formatted nor filled with zeros
220
        $this->assertEquals(
221
            '215703000075200334559000126',
222
            $this->slipData->getCompleteReferenceNumber(false, false)
223
        );
224
225
        // Test with reference number but without banking customer ID
226
        $this->slipData->setWithBankingCustomerId(false);
227
228
        // Formatted and filled with zeros
229
        $this->assertEquals(
230
            '00 00000 00075 20033 45590 00129',
231
            $this->slipData->getCompleteReferenceNumber()
232
        );
233
        // Not formatted but filled with zeros
234
        $this->assertEquals(
235
            '000000000075200334559000129',
236
            $this->slipData->getCompleteReferenceNumber(false)
237
        );
238
        // Formatted but not filled with zeros
239
        $this->assertEquals(
240
            '75 20033 45590 00129',
241
            $this->slipData->getCompleteReferenceNumber(true, false)
242
        );
243
        // Neither formatted nor filled with zeros
244
        $this->assertEquals(
245
            '75200334559000129',
246
            $this->slipData->getCompleteReferenceNumber(false, false)
247
        );
248
    }
249
250
    /**
251
     * Tests the getCompleteReferenceNumber method with the reference number disabled
252
     *
253
     * @return void
254
     * @expectedException \SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException
255
     * @expectedExceptionMessage You are accessing the disabled reference number. You need to re-enable it first.
256
     * @covers ::getCompleteReferenceNumber
257
     * @covers ::getReferenceNumber
258
     */
259
    public function testGetCompleteReferenceNumberWithReferenceNrDisabled()
260
    {
261
        $this->slipData->setWithReferenceNumber(false);
262
        $this->slipData->getCompleteReferenceNumber();
263
    }
264
265
    /**
266
     * Tests the getCodeLine method
267
     *
268
     * @return void
269
     * @covers ::getCodeLine
270
     * @covers ::modulo10
271
     * @covers ::getAccountDigits
272
     */
273
    public function testGetCodeLine()
274
    {
275
        $this->slipData->setAccountNumber('01-145-6');
276
        $this->slipData->setAmount(2830.50);
277
        $this->slipData->setReferenceNumber('7520033455900012');
278
        $this->slipData->setBankingCustomerId('215703');
279
280
        // Filled with zeros
281
        $this->assertEquals(
282
            '0100002830509>215703000075200334559000126+ 010001456>',
283
            $this->slipData->getCodeLine()
284
        );
285
        // Not filled with zeros
286
        $this->assertEquals(
287
            '0100002830509>215703000075200334559000126+ 010001456>',
288
            $this->slipData->getCodeLine(false)
289
        );
290
291
        $this->slipData->setReferenceNumber('123456789');
292
        $this->slipData->setBankingCustomerId('1234');
293
294
        // Filled with zeros
295
        $this->assertEquals(
296
            '0100002830509>001234000000000001234567892+ 010001456>',
297
            $this->slipData->getCodeLine()
298
        );
299
        // Not filled with zeros
300
        $this->assertEquals(
301
            '0100002830509>1234000000000001234567892+ 010001456>',
302
            $this->slipData->getCodeLine(false)
303
        );
304
305
        $this->slipData->setWithBankingCustomerId(false);
306
307
        // Filled with zeros
308
        $this->assertEquals(
309
            '0100002830509>000000000000000001234567894+ 010001456>',
310
            $this->slipData->getCodeLine()
311
        );
312
        // Not filled with zeros
313
        $this->assertEquals(
314
            '0100002830509>1234567894+ 010001456>',
315
            $this->slipData->getCodeLine(false)
316
        );
317
318
        $this->slipData->setAmount(0.0);
319
320
        // Filled with zeros
321
        $this->assertEquals(
322
            '0100000000005>000000000000000001234567894+ 010001456>',
323
            $this->slipData->getCodeLine()
324
        );
325
        // Not filled with zeros
326
        $this->assertEquals(
327
            '0100000000005>1234567894+ 010001456>',
328
            $this->slipData->getCodeLine(false)
329
        );
330
331
        $this->slipData->setWithAmount(false);
332
333
        // Filled with zeros
334
        $this->assertEquals(
335
            '042>000000000000000001234567894+ 010001456>',
336
            $this->slipData->getCodeLine()
337
        );
338
        // Not filled with zeros
339
        $this->assertEquals(
340
            '042>1234567894+ 010001456>',
341
            $this->slipData->getCodeLine(false)
342
        );
343
    }
344
345
    /**
346
     * Tests the setNotForPayment method
347
     *
348
     * @return void
349
     * @covers ::setNotForPayment
350
     * @covers ::appendCheckDigit
351
     * @covers ::getCompleteReferenceNumber
352
     * @covers ::getCodeLine
353
     * @covers ::getAccountDigits
354
     */
355
    public function testSetNotForPayment()
356
    {
357
        $returned = $this->slipData->setNotForPayment(true);
358
        $this->assertInstanceOf('SwissPaymentSlip\SwissPaymentSlip\OrangePaymentSlipData', $returned);
359
        $this->assertTrue($this->slipData->getNotForPayment());
360
361
        $this->assertEquals('XXXXXXXXXXXXXXXXXXXX', $this->slipData->getReferenceNumber());
362
        $this->assertEquals('XXXXXXXXXXXXXXXXXXXXXXXXXXX', $this->slipData->getCompleteReferenceNumber(false));
363
        $this->assertEquals('XX XXXXX XXXXX XXXXX XXXXX XXXXX', $this->slipData->getCompleteReferenceNumber());
364
365
        $this->assertEquals('XXXXXXXXXXXXX>XXXXXXXXXXXXXXXXXXXXXXXXXXX+ XXXXXXXXX>', $this->slipData->getCodeLine());
366
    }
367
368
    /**
369
     * Tests the setNotForPayment method when fields are disabled
370
     *
371
     * @return void
372
     * @covers ::setNotForPayment
373
     */
374
    public function testSetNotForPaymentDisabledFields()
375
    {
376
        $this->slipData->setWithReferenceNumber(false);
377
        $this->slipData->setWithBankingCustomerId(false);
378
379
        $this->slipData->setNotForPayment(true);
380
    }
381
382
    /**
383
     * Tests the setAmount method with a problematic float as amount parameter
384
     *
385
     * It is common computer science knowledge that in some cases floats can be imprecise.
386
     * The class should handle that by rounding it properly.
387
     *
388
     * @return void
389
     * @covers ::setAmount
390
     */
391
    public function testSetAmountWithProblematicFloat()
392
    {
393
        $amounts = array (
394
            0 => 1.8,
395
            1 => 11.0,
396
            2 => 18.3,
397
            3 => 2.3,
398
            4 => 7.0,
399
            5 => 10.2,
400
            6 => 7.6,
401
            7 => 2.3,
402
            8 => 7.0,
403
            9 => 6.4,
404
            10 => 1.8,
405
            11 => 2.6,
406
            12 => 15.5,
407
            13 => 1.8,
408
            14 => 7.6,
409
            15 => 8.7,
410
            16 => 5.6,
411
            17 => 7.6,
412
            18 => 5.4,
413
            19 => 3.1,
414
            20 => 10.8,
415
            21 => 2.6,
416
            22 => 2.6,
417
            23 => 6.5,
418
            24 => 10.2,
419
            25 => 47.0,
420
            26 => 3.1,
421
            27 => 2.6,
422
        );
423
424
        $total = 0.0;
425
        foreach ($amounts as $amount) {
426
            $total += $amount;
427
        }
428
429
        $this->assertSame(218, (int)$total);
430
431
        $this->slipData->setAmount($total);
432
433
        $this->assertSame(219, $this->slipData->getAmountFrancs());
434
        $this->assertSame('00', $this->slipData->getAmountCents());
435
    }
436
437
    /**
438
     * Tests the setAmount method with a another problematic float as amount parameter
439
     *
440
     * It is common computer science knowledge that in some cases floats can be imprecise.
441
     * The class should handle that by rounding it properly.
442
     *
443
     * @return void
444
     * @covers ::setAmount
445
     */
446
    public function testSetAmountWithAnotherProblematicFloat()
447
    {
448
        $total = 114.9984;
449
450
        $this->assertSame(114, (int)$total);
451
452
        $this->slipData->setAmount($total);
453
454
        $this->assertSame(115, $this->slipData->getAmountFrancs());
455
        $this->assertSame('00', $this->slipData->getAmountCents());
456
    }
457
}
458