Test Setup Failed
Pull Request — master (#27)
by
unknown
02:39
created

testSetReferenceNumberLengthExceptionA()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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
use SwissPaymentSlip\SwissPaymentSlip\Exception\PaymentSlipException;
17
18
/**
19
 * Tests for the OrangePaymentSlipData class
20
 *
21
 * @coversDefaultClass SwissPaymentSlip\SwissPaymentSlip\OrangePaymentSlipData
22
 */
23
class OrangePaymentSlipDataTest extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * The object under test
27
     *
28
     * @var OrangePaymentSlipData
29
     */
30
    protected $slipData;
31
32
    /**
33
     * Setup the object under test
34
     *
35
     * @return void
36
     */
37
    protected function setUp()
38
    {
39
        $this->slipData = new OrangePaymentSlipData();
40
    }
41
42
    /**
43
     * Tests the getWithReferenceNumber and setWithReferenceNumber methods
44
     *
45
     * @return void
46
     * @covers ::setWithReferenceNumber
47
     * @covers ::getWithReferenceNumber
48
     * @covers ::isBool
49
     * @covers ::setReferenceNumber
50
     * @covers ::getReferenceNumber
51
     */
52
    public function testSetWithReferenceNumber()
53
    {
54
        // Test default values
55
        $this->assertEquals('', $this->slipData->getReferenceNumber());
56
        $this->assertTrue($this->slipData->getWithReferenceNumber());
57
58
        // Set data when enabled, also check for returned instance
59
        $returned = $this->slipData->setReferenceNumber('0123456789');
60
        $this->assertInstanceOf('SwissPaymentSlip\SwissPaymentSlip\OrangePaymentSlipData', $returned);
61
        $this->assertEquals('0123456789', $this->slipData->getReferenceNumber());
62
63
        // Disable feature, also check for returned instance
64
        $returned = $this->slipData->setWithReferenceNumber(false);
65
        $this->assertInstanceOf('SwissPaymentSlip\SwissPaymentSlip\OrangePaymentSlipData', $returned);
66
        $this->assertFalse($this->slipData->getWithReferenceNumber());
67
68
        // Re-enable feature, using no parameter
69
        $this->slipData->setWithReferenceNumber();
70
        $this->assertTrue($this->slipData->getWithReferenceNumber());
71
        $this->assertEquals('', $this->slipData->getReferenceNumber());
72
    }
73
74
    /**
75
     * Tests the setReferenceNumber method when disabled
76
     *
77
     * @return void
78
     * @expectedException \SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException
79
     * @expectedExceptionMessage You are accessing the disabled reference number. You need to re-enable it first.
80
     * @covers ::setReferenceNumber
81
     */
82
    public function testSetReferenceNumberWhenDisabled()
83
    {
84
        $this->slipData->setWithReferenceNumber(false);
85
        $this->slipData->setReferenceNumber('');
86
    }
87
88
    /**
89
     * Tests the getReferenceNumber method when disabled
90
     *
91
     * @return void
92
     * @expectedException \SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException
93
     * @expectedExceptionMessage You are accessing the disabled reference number. You need to re-enable it first.
94
     * @covers ::getReferenceNumber
95
     */
96
    public function testGetReferenceNumberWhenDisabled()
97
    {
98
        $this->slipData->setWithReferenceNumber(false);
99
        $this->slipData->getReferenceNumber();
100
    }
101
102
    /**
103
     * Tests the setWithReferenceNumber method with an invalid parameter
104
     *
105
     * @return void
106
     * @expectedException \InvalidArgumentException
107
     * @expectedExceptionMessage $withReferenceNumber is not a boolean.
108
     * @covers ::setWithReferenceNumber
109
     * @covers ::isBool
110
     */
111
    public function testSetWithReferenceNumberInvalidParameter()
112
    {
113
        $this->slipData->setWithReferenceNumber(1);
0 ignored issues
show
Documentation introduced by
1 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
    }
115
116
    /**
117
     * Tests the getWithBankingCustomerId and setWithBankingCustomerId methods
118
     *
119
     * @return void
120
     * @covers ::setWithBankingCustomerId
121
     * @covers ::getWithBankingCustomerId
122
     * @covers ::isBool
123
     * @covers ::setBankingCustomerId
124
     * @covers ::getBankingCustomerId
125
     */
126
    public function testSetWithBankingCustomerId()
127
    {
128
        // Test default values
129
        $this->assertEquals('', $this->slipData->getBankingCustomerId());
130
        $this->assertTrue($this->slipData->getWithBankingCustomerId());
131
132
        // Set data when enabled, also check for returned instance
133
        $returned = $this->slipData->setBankingCustomerId('012345');
134
        $this->assertInstanceOf('SwissPaymentSlip\SwissPaymentSlip\OrangePaymentSlipData', $returned);
135
        $this->assertEquals('012345', $this->slipData->getBankingCustomerId());
136
137
        // Disable feature, also check for returned instance
138
        $returned = $this->slipData->setWithBankingCustomerId(false);
139
        $this->assertInstanceOf('SwissPaymentSlip\SwissPaymentSlip\OrangePaymentSlipData', $returned);
140
        $this->assertFalse($this->slipData->getWithBankingCustomerId());
141
142
        // Re-enable feature, using no parameter
143
        $this->slipData->setWithBankingCustomerId();
144
        $this->assertTrue($this->slipData->getWithBankingCustomerId());
145
        $this->assertEquals('', $this->slipData->getBankingCustomerId());
146
    }
147
148
    /**
149
     * Tests the setBankingCustomerId method when disabled
150
     *
151
     * @return void
152
     * @expectedException \SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException
153
     * @expectedExceptionMessage You are accessing the disabled banking customer ID. You need to re-enable it first.
154
     * @covers ::setBankingCustomerId
155
     */
156
    public function testSetBankingCustomerIdNumberWhenDisabled()
157
    {
158
        $this->slipData->setWithBankingCustomerId(false);
159
        $this->slipData->setBankingCustomerId('');
160
    }
161
162
    /**
163
     * Tests the getBankingCustomerId method when disabled
164
     *
165
     * @return void
166
     * @expectedException \SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException
167
     * @expectedExceptionMessage You are accessing the disabled banking customer ID. You need to re-enable it first.
168
     * @covers ::getBankingCustomerId
169
     */
170
    public function testGetBankingCustomerIdNumberWhenDisabled()
171
    {
172
        $this->slipData->setWithBankingCustomerId(false);
173
        $this->slipData->getBankingCustomerId();
174
    }
175
176
    /**
177
     * Tests the setWithBankingCustomerId method with an invalid parameter
178
     *
179
     * @return void
180
     * @expectedException \InvalidArgumentException
181
     * @expectedExceptionMessage $withBankingCustomerId is not a boolean.
182
     * @covers ::setWithBankingCustomerId
183
     * @covers ::isBool
184
     */
185
    public function testSetWithBankingCustomerIdInvalidParameter()
186
    {
187
        $this->slipData->setWithBankingCustomerId(1);
0 ignored issues
show
Documentation introduced by
1 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
188
    }
189
190
    /**
191
     * Tests the getCompleteReferenceNumber method
192
     * Throws exception because of banking customer ID with reference number is longer than max allowed (26)
193
     *
194
     * @return void
195
     * @covers ::setReferenceNumber
196
     */
197
    public function testSetReferenceNumberLengthExceptionA()
198
    {
199
        $this->setExpectedException(PaymentSlipException::class);
200
        $this->slipData->setReferenceNumber('752003345590001277777777777');
201
    }
202
203
    /**
204
     * Tests the getCompleteReferenceNumber method
205
     * Throws exception because of banking customer ID with reference number is longer than max allowed (26)
206
     *
207
     * @return void
208
     * @covers ::setReferenceNumber
209
     */
210
    public function testSetReferenceNumberLengthExceptionB()
211
    {
212
        $this->setExpectedException(PaymentSlipException::class);
213
        $this->slipData->setBankingCustomerId('1234567890', 10);
214
        $this->slipData->setReferenceNumber('75200334559000123');
215
    }
216
217
    /**
218
     * Tests the setBankingCustomerId method
219
     * Throws exception because of banking customer ID is longer than max (10)
220
     *
221
     * @return void
222
     * @covers ::setBankingCustomerId
223
     */
224
    public function testsetBankingCustomerIdLengthExceptionA()
225
    {
226
        $this->setExpectedException(PaymentSlipException::class);
227
        $this->slipData->setBankingCustomerId('12345678901', 11);
228
    }
229
230
    /**
231
     * Tests the setBankingCustomerId method
232
     * Throws exception because of banking customer ID is longer than expected by default (6)
233
     *
234
     * @return void
235
     * @covers ::setBankingCustomerId
236
     */
237
    public function testsetBankingCustomerIdLengthExceptionB()
238
    {
239
        $this->setExpectedException(PaymentSlipException::class);
240
        $this->slipData->setBankingCustomerId('1234567890');
241
    }
242
243
    /**
244
     * Tests the setBankingCustomerId method
245
     * Throws exception because of banking customer ID with reference number is longer than max allowed (26)
246
     *
247
     * @return void
248
     * @covers ::setBankingCustomerId
249
     */
250
    public function testsetBankingCustomerIdLengthExceptionC()
251
    {
252
        $this->setExpectedException(PaymentSlipException::class);
253
        $this->slipData->setReferenceNumber('75200334559000123');
254
        $this->slipData->setBankingCustomerId('1234567890', 10);
255
    }
256
257
    /**
258
     * Tests the getCompleteReferenceNumber method
259
     *
260
     * @return void
261
     * @covers ::getCompleteReferenceNumber
262
     * @covers ::appendCheckDigit
263
     * @covers ::breakStringIntoBlocks
264
     * @covers ::modulo10
265
     */
266
    public function testGetCompleteReferenceNumber()
267
    {
268
        // Test with reference number & banking customer ID
269
        $this->slipData->setReferenceNumber('7520033455900012');
270
        
271
        $this->slipData->setBankingCustomerId('215703');
272
273
        // Formatted and filled with zeros
274
        $this->assertEquals(
275
            '21 57030 00075 20033 45590 00126',
276
            $this->slipData->getCompleteReferenceNumber()
277
        );
278
        // Not formatted but filled with zeros
279
        $this->assertEquals(
280
            '215703000075200334559000126',
281
            $this->slipData->getCompleteReferenceNumber(false)
282
        );
283
        // Formatted but not filled with zeros
284
        $this->assertEquals(
285
            '21 57030 00075 20033 45590 00126',
286
            $this->slipData->getCompleteReferenceNumber(true, false)
287
        );
288
        // Neither formatted nor filled with zeros
289
        $this->assertEquals(
290
            '215703000075200334559000126',
291
            $this->slipData->getCompleteReferenceNumber(false, false)
292
        );
293
294
        // With long banking customer ID with expected 0 filling
295
        $this->slipData->setBankingCustomerId('123456789', 10);
296
297
        // Formatted and filled with zeros
298
        $this->assertEquals(
299
            '01 23456 78975 20033 45590 00128',
300
            $this->slipData->getCompleteReferenceNumber()
301
        );
302
        // Not formatted but filled with zeros
303
        $this->assertEquals(
304
            '012345678975200334559000128',
305
            $this->slipData->getCompleteReferenceNumber(false)
306
        );
307
        
308
        // Formatted but not filled with zeros
309
        $this->assertEquals(
310
            '01 23456 78975 20033 45590 00128',
311
            $this->slipData->getCompleteReferenceNumber(true, false)
312
        );
313
        // Neither formatted nor filled with zeros
314
        $this->assertEquals(
315
            '012345678975200334559000128',
316
            $this->slipData->getCompleteReferenceNumber(false, false)
317
        );
318
319
        // With long banking customer ID without expected 0 filling
320
        $this->slipData->setBankingCustomerId('1234567', 7);
321
322
        // Formatted and filled with zeros
323
        $this->assertEquals(
324
            '12 34567 00075 20033 45590 00128',
325
            $this->slipData->getCompleteReferenceNumber()
326
        );
327
        // Not formatted but filled with zeros
328
        $this->assertEquals(
329
            '123456700075200334559000128',
330
            $this->slipData->getCompleteReferenceNumber(false)
331
        );
332
        
333
        // Formatted but not filled with zeros
334
        $this->assertEquals(
335
            '12 34567 00075 20033 45590 00128',
336
            $this->slipData->getCompleteReferenceNumber(true, false)
337
        );
338
        // Neither formatted nor filled with zeros
339
        $this->assertEquals(
340
            '123456700075200334559000128',
341
            $this->slipData->getCompleteReferenceNumber(false, false)
342
        );
343
344
        // Test with reference number but without banking customer ID
345
        $this->slipData->setWithBankingCustomerId(false);
346
347
        // Formatted and filled with zeros
348
        $this->assertEquals(
349
            '00 00000 00075 20033 45590 00129',
350
            $this->slipData->getCompleteReferenceNumber()
351
        );
352
        // Not formatted but filled with zeros
353
        $this->assertEquals(
354
            '000000000075200334559000129',
355
            $this->slipData->getCompleteReferenceNumber(false)
356
        );
357
        // Formatted but not filled with zeros
358
        $this->assertEquals(
359
            '75 20033 45590 00129',
360
            $this->slipData->getCompleteReferenceNumber(true, false)
361
        );
362
        // Neither formatted nor filled with zeros
363
        $this->assertEquals(
364
            '75200334559000129',
365
            $this->slipData->getCompleteReferenceNumber(false, false)
366
        );
367
    }
368
369
    /**
370
     * Tests the getCompleteReferenceNumber method with the reference number disabled
371
     *
372
     * @return void
373
     * @expectedException \SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException
374
     * @expectedExceptionMessage You are accessing the disabled reference number. You need to re-enable it first.
375
     * @covers ::getCompleteReferenceNumber
376
     * @covers ::getReferenceNumber
377
     */
378
    public function testGetCompleteReferenceNumberWithReferenceNrDisabled()
379
    {
380
        $this->slipData->setWithReferenceNumber(false);
381
        $this->slipData->getCompleteReferenceNumber();
382
    }
383
384
    /**
385
     * Tests the getCodeLine method
386
     *
387
     * @return void
388
     * @covers ::getCodeLine
389
     * @covers ::modulo10
390
     * @covers ::getAccountDigits
391
     */
392
    public function testGetCodeLine()
393
    {
394
        $this->slipData->setAccountNumber('01-145-6');
395
        $this->slipData->setAmount(2830.50);
396
        $this->slipData->setReferenceNumber('7520033455900012');
397
        $this->slipData->setBankingCustomerId('215703');
398
399
        // Filled with zeros
400
        $this->assertEquals(
401
            '0100002830509>215703000075200334559000126+ 010001456>',
402
            $this->slipData->getCodeLine()
403
        );
404
        // Not filled with zeros
405
        $this->assertEquals(
406
            '0100002830509>215703000075200334559000126+ 010001456>',
407
            $this->slipData->getCodeLine(false)
408
        );
409
410
        $this->slipData->setReferenceNumber('123456789');
411
        $this->slipData->setBankingCustomerId('1234');
412
413
        // Filled with zeros
414
        $this->assertEquals(
415
            '0100002830509>001234000000000001234567892+ 010001456>',
416
            $this->slipData->getCodeLine()
417
        );
418
        // Not filled with zeros
419
        $this->assertEquals(
420
            '0100002830509>001234000000000001234567892+ 010001456>',
421
            $this->slipData->getCodeLine(false)
422
        );
423
424
        // With long banking customer ID with expected 0 filling
425
        $this->slipData->setBankingCustomerId('123456789', 10);
426
427
        // Filled with zeros
428
        $this->assertEquals(
429
            '0100002830509>012345678900000001234567891+ 010001456>',
430
            $this->slipData->getCodeLine()
431
        );
432
        // Not filled with zeros
433
        $this->assertEquals(
434
            '0100002830509>012345678900000001234567891+ 010001456>',
435
            $this->slipData->getCodeLine(false)
436
        );
437
        
438
        // With long banking customer ID without expected 0 filling
439
        $this->slipData->setBankingCustomerId('1234567', 7);
440
441
        // Filled with zeros
442
        $this->assertEquals(
443
            '0100002830509>123456700000000001234567891+ 010001456>',
444
            $this->slipData->getCodeLine()
445
        );
446
        // Not filled with zeros
447
        $this->assertEquals(
448
            '0100002830509>123456700000000001234567891+ 010001456>',
449
            $this->slipData->getCodeLine(false)
450
        );
451
        
452
        $this->slipData->setWithBankingCustomerId(false);
453
454
        // Filled with zeros
455
        $this->assertEquals(
456
            '0100002830509>000000000000000001234567894+ 010001456>',
457
            $this->slipData->getCodeLine()
458
        );
459
        // Not filled with zeros
460
        $this->assertEquals(
461
            '0100002830509>1234567894+ 010001456>',
462
            $this->slipData->getCodeLine(false)
463
        );
464
465
        $this->slipData->setAmount(0.0);
466
467
        // Filled with zeros
468
        $this->assertEquals(
469
            '0100000000005>000000000000000001234567894+ 010001456>',
470
            $this->slipData->getCodeLine()
471
        );
472
        // Not filled with zeros
473
        $this->assertEquals(
474
            '0100000000005>1234567894+ 010001456>',
475
            $this->slipData->getCodeLine(false)
476
        );
477
478
        $this->slipData->setWithAmount(false);
479
480
        // Filled with zeros
481
        $this->assertEquals(
482
            '042>000000000000000001234567894+ 010001456>',
483
            $this->slipData->getCodeLine()
484
        );
485
        // Not filled with zeros
486
        $this->assertEquals(
487
            '042>1234567894+ 010001456>',
488
            $this->slipData->getCodeLine(false)
489
        );
490
    }
491
492
    /**
493
     * Tests the setNotForPayment method
494
     *
495
     * @return void
496
     * @covers ::setNotForPayment
497
     * @covers ::appendCheckDigit
498
     * @covers ::getCompleteReferenceNumber
499
     * @covers ::getCodeLine
500
     * @covers ::getAccountDigits
501
     */
502
    public function testSetNotForPayment()
503
    {
504
        $returned = $this->slipData->setNotForPayment(true);
505
        $this->assertInstanceOf('SwissPaymentSlip\SwissPaymentSlip\OrangePaymentSlipData', $returned);
506
        $this->assertTrue($this->slipData->getNotForPayment());
507
508
        $this->assertEquals('XXXXXXXXXXXXXXXXXXXX', $this->slipData->getReferenceNumber());
509
        $this->assertEquals('XXXXXXXXXXXXXXXXXXXXXXXXXXX', $this->slipData->getCompleteReferenceNumber(false));
510
        $this->assertEquals('XX XXXXX XXXXX XXXXX XXXXX XXXXX', $this->slipData->getCompleteReferenceNumber());
511
512
        $this->assertEquals('XXXXXXXXXXXXX>XXXXXXXXXXXXXXXXXXXXXXXXXXX+ XXXXXXXXX>', $this->slipData->getCodeLine());
513
    }
514
515
    /**
516
     * Tests the setNotForPayment method when fields are disabled
517
     *
518
     * @return void
519
     * @covers ::setNotForPayment
520
     */
521
    public function testSetNotForPaymentDisabledFields()
522
    {
523
        $this->slipData->setWithReferenceNumber(false);
524
        $this->slipData->setWithBankingCustomerId(false);
525
526
        $this->slipData->setNotForPayment(true);
527
    }
528
}
529