PaymentSlipTest::testCreatePaymentSlip()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 9.1344
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Swiss Payment Slip PDF
4
 *
5
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
6
 * @copyright 2012-2015 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/SwissPaymentSlipPdf/
11
 */
12
13
namespace SwissPaymentSlip\SwissPaymentSlip\Tests;
14
15
use SwissPaymentSlip\SwissPaymentSlipPdf\Tests\TestablePaymentSlip;
16
use SwissPaymentSlip\SwissPaymentSlipPdf\Tests\TestablePaymentSlipData;
17
use SwissPaymentSlip\SwissPaymentSlipPdf\Tests\TestablePaymentSlipPdf;
18
19
/**
20
 * Tests for the OrangePaymentSlipPdf class
21
 *
22
 * @coversDefaultClass SwissPaymentSlip\SwissPaymentSlipPdf\PaymentSlipPdf
23
 */
24
class PaymentSlipTest extends \PHPUnit_Framework_TestCase
25
{
26
    /**
27
     * Tests the constructor with an invalid PDF engine object
28
     *
29
     * @return void
30
     * @expectedException \InvalidArgumentException
31
     * @expectedExceptionMessage $pdfEngine is not an object!
32
     * @covers ::__construct
33
     */
34
    public function testConstructorInvalidPdfEngine()
35
    {
36
        new TestablePaymentSlipPdf('FooBar');
0 ignored issues
show
Documentation introduced by
'FooBar' is of type string, but the function expects a object.

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...
37
    }
38
39
    /**
40
     * Tests the constructor with valid parameters
41
     *
42
     * @return void
43
     * @covers ::__construct
44
     */
45
    public function testConstructor()
46
    {
47
        new TestablePaymentSlipPdf((object)'FooBar');
48
    }
49
50
    /**
51
     * Tests the createPaymentSlip method with a valid payment slip
52
     *
53
     * @return void
54
     * @expectedException \PHPUnit_Framework_Error
55
     * @expectedExceptionMessage Argument 1 passed to SwissPaymentSlip\SwissPaymentSlipPdf\PaymentSlipPdf::createPaymentSlip() must be an instance of SwissPaymentSlip\SwissPaymentSlip\PaymentSlip, instance of stdClass given
56
     * @covers ::createPaymentSlip
57
     */
58
    public function testConstructorInvalidPaymentSlip()
59
    {
60
        if (defined('HHVM_VERSION')) {
61
            $this->markTestSkipped('This test fails on HHVM due to error message varieties');
62
        }
63
        $paymentSlipPdf = new TestablePaymentSlipPdf((object)'FooBar');
64
        $paymentSlipPdf->createPaymentSlip((object)'NotAPaymentSlip');
65
    }
66
67
    /**
68
     * Tests the createPaymentSlip method
69
     *
70
     * @return void
71
     * @covers ::createPaymentSlip
72
     */
73
    public function testCreatePaymentSlip()
74
    {
75
        $paymentSlipPdf = $this->getMock(
76
            'SwissPaymentSlip\SwissPaymentSlipPdf\Tests\TestablePaymentSlipPdf',
77
            ['writePaymentSlipLines', 'displayImage'],
78
            [(object)'FooBar']
79
        );
80
81
        // $paymentSLip property should not be set
82
        $this->assertAttributeEquals(null, 'paymentSlip', $paymentSlipPdf);
83
84
        // Setup expectations
85
        $expectedElements = [
86
            'bankLeft',
87
            'bankRight',
88
            'recipientLeft',
89
            'recipientRight',
90
            'accountLeft',
91
            'accountRight',
92
            'amountFrancsLeft',
93
            'amountFrancsRight',
94
            'amountCentsLeft',
95
            'amountCentsRight',
96
            'payerLeft',
97
            'payerRight',
98
        ];
99
        foreach ($expectedElements as $elementNr => $elementName) {
100
            $paymentSlipPdf->expects($this->at($elementNr + 1))
101
                ->method('writePaymentSlipLines')
102
                ->with(
103
                    $elementName,
104
                    $this->anything()
105
                );
106
        }
107
        $paymentSlipPdf->expects($this->exactly(12))
108
            ->method('writePaymentSlipLines')
109
            ->will($this->returnSelf());
110
        $paymentSlipPdf->expects($this->once())
111
            ->method('displayImage')
112
            ->will($this->returnSelf());
113
114
        $slipData = new TestablePaymentSlipData();
115
        $paymentSlip = new TestablePaymentSlip($slipData);
116
        $paymentSlipPdf->createPaymentSlip($paymentSlip);
117
118
        // $paymentSLip property should be null again
119
        $this->assertAttributeEquals(null, 'paymentSlip', $paymentSlipPdf);
120
    }
121
122
    /**
123
     * Tests the createPaymentSlip method when no background image is displayed
124
     *
125
     * @return void
126
     * @covers ::createPaymentSlip
127
     */
128
    public function testCreatePaymentSlipNoBackground()
129
    {
130
        $paymentSlipPdf = $this->getMock(
131
            'SwissPaymentSlip\SwissPaymentSlipPdf\Tests\TestablePaymentSlipPdf',
132
            ['writePaymentSlipLines', 'displayImage'],
133
            [(object)'FooBar']
134
        );
135
136
        // Setup expectations
137
        // Twelve elements
138
        $paymentSlipPdf->expects($this->exactly(12))
139
            ->method('writePaymentSlipLines')
140
            ->will($this->returnSelf());
141
        $paymentSlipPdf->expects($this->never())
142
            ->method('displayImage')
143
            ->will($this->returnSelf());
144
145
        $slipData = new TestablePaymentSlipData();
146
        $paymentSlip = new TestablePaymentSlip($slipData);
147
        $paymentSlip->setDisplayBackground(false);
148
        $paymentSlipPdf->createPaymentSlip($paymentSlip);
149
    }
150
151
    /**
152
     * Tests the writePaymentSlipLines method
153
     *
154
     * @return void
155
     * @covers ::writePaymentSlipLines
156
     */
157
    public function testWritePaymentSlipLines()
158
    {
159
        $paymentSlipPdf = $this->getMock(
160
            'SwissPaymentSlip\SwissPaymentSlipPdf\Tests\TestablePaymentSlipPdf',
161
            ['setFont', 'setBackground', 'setPosition', 'displayImage', 'createCell'],
162
            [(object)'FooBar']
163
        );
164
165
        // Setup expectations
166
        // Twelve elements, some elements with more than one line
167
        $paymentSlipPdf->expects($this->exactly(12))
168
            ->method('setFont');
169
        $paymentSlipPdf->expects($this->exactly(0))
170
            ->method('setBackground');
171
        $paymentSlipPdf->expects($this->exactly(26))
172
            ->method('setPosition');
173
        $paymentSlipPdf->expects($this->exactly(26))
174
            ->method('createCell');
175
176
        $slipData = new TestablePaymentSlipData();
177
        $paymentSlip = new TestablePaymentSlip($slipData);
178
        $paymentSlipPdf->createPaymentSlip($paymentSlip);
179
    }
180
181
    /**
182
     * Tests the writePaymentSlipLines method with an element that has a background set
183
     *
184
     * @return void
185
     * @covers ::writePaymentSlipLines
186
     */
187
    public function testWritePaymentSlipLinesElementWithBackground()
188
    {
189
        $paymentSlipPdf = $this->getMock(
190
            'SwissPaymentSlip\SwissPaymentSlipPdf\Tests\TestablePaymentSlipPdf',
191
            ['setFont', 'setBackground', 'setPosition', 'displayImage', 'createCell'],
192
            [(object)'FooBar']
193
        );
194
195
        // Setup expectations
196
        $paymentSlipPdf->expects($this->exactly(12))
197
            ->method('setFont');
198
        $paymentSlipPdf->expects($this->exactly(1))
199
            ->method('setBackground')
200
            ->with($this->equalTo('#AABBCC'));
201
        $paymentSlipPdf->expects($this->exactly(26))
202
            ->method('setPosition');
203
        $paymentSlipPdf->expects($this->exactly(26))
204
            ->method('createCell');
205
206
        $slipData = new TestablePaymentSlipData();
207
        $paymentSlip = new TestablePaymentSlip($slipData);
208
        $paymentSlip->setBankLeftAttr(null, null, null, null, '#AABBCC');
209
        $paymentSlipPdf->createPaymentSlip($paymentSlip);
210
    }
211
212
    /**
213
     * Tests the writePaymentSlipLines method with an invalid first parameter
214
     *
215
     * @return void
216
     * @expectedException \InvalidArgumentException
217
     * @expectedExceptionMessage $elementName is not a string!
218
     * @covers ::writePaymentSlipLines
219
     */
220
    public function testWritePaymentSlipLinesInvalidFirstParameter()
221
    {
222
        $method = $this->makeMethodAccessible(
223
            'SwissPaymentSlip\SwissPaymentSlipPdf\Tests\TestablePaymentSlipPdf',
224
            'writePaymentSlipLines'
225
        );
226
        $method->invoke(
227
            $paymentSlipPdf = new TestablePaymentSlipPdf((object)'FooBar'),
228
            [],
229
            []
230
        );
231
    }
232
233
    /**
234
     * Tests the writePaymentSlipLines method with an invalid second parameter
235
     *
236
     * @return void
237
     * @expectedException \PHPUnit_Framework_Error
238
     * @expectedExceptionMessage Argument 2 passed to SwissPaymentSlip\SwissPaymentSlipPdf\PaymentSlipPdf::writePaymentSlipLines() must be of the type array, string given
239
     * @covers ::writePaymentSlipLines
240
     */
241
    public function testWritePaymentSlipLinesInvalidSecondParameter()
242
    {
243
        if (version_compare(phpversion(), '5.4.0', '<')) {
244
            $this->markTestSkipped('This test fails on PHP 5.3 due to error message varieties');
245
        }
246
        if (defined('HHVM_VERSION')) {
247
            $this->markTestSkipped('This test fails on HHVM due to error message varieties');
248
        }
249
        $method = $this->makeMethodAccessible(
250
            'SwissPaymentSlip\SwissPaymentSlipPdf\Tests\TestablePaymentSlipPdf',
251
            'writePaymentSlipLines'
252
        );
253
        $method->invoke(
254
            $paymentSlipPdf = new TestablePaymentSlipPdf((object)'FooBar'),
255
            'elementName',
256
            'notAnArray'
257
        );
258
    }
259
260
    /**
261
     * Tests the writePaymentSlipLines method with no 'lines' key
262
     *
263
     * @return void
264
     * @expectedException \InvalidArgumentException
265
     * @expectedExceptionMessage $element contains not "lines" key!
266
     * @covers ::writePaymentSlipLines
267
     */
268 View Code Duplication
    public function testWritePaymentSlipLinesNoLinesKey()
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...
269
    {
270
        $method = $this->makeMethodAccessible(
271
            'SwissPaymentSlip\SwissPaymentSlipPdf\Tests\TestablePaymentSlipPdf',
272
            'writePaymentSlipLines'
273
        );
274
        $method->invoke(
275
            $paymentSlipPdf = new TestablePaymentSlipPdf((object)'FooBar'),
276
            'elementName',
277
            ['attributes' => []]
278
        );
279
    }
280
281
    /**
282
     * Tests the writePaymentSlipLines method with no 'attributes' key
283
     *
284
     * @return void
285
     * @expectedException \InvalidArgumentException
286
     * @expectedExceptionMessage $element contains not "attributes" key!
287
     * @covers ::writePaymentSlipLines
288
     */
289 View Code Duplication
    public function testWritePaymentSlipLinesNoAttributesKey()
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...
290
    {
291
        $method = $this->makeMethodAccessible(
292
            'SwissPaymentSlip\SwissPaymentSlipPdf\Tests\TestablePaymentSlipPdf',
293
            'writePaymentSlipLines'
294
        );
295
        $method->invoke(
296
            $paymentSlipPdf = new TestablePaymentSlipPdf((object)'FooBar'),
297
            'elementName', ['lines' => []]
298
        );
299
    }
300
301
    /**
302
     * Tests the writePaymentSlipLines method with 'lines' key being no array
303
     *
304
     * @return void
305
     * @expectedException \InvalidArgumentException
306
     * @expectedExceptionMessage $lines is not an array!
307
     * @covers ::writePaymentSlipLines
308
     */
309 View Code Duplication
    public function testWritePaymentSlipLinesLinesKeyNoArray()
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...
310
    {
311
        $method = $this->makeMethodAccessible(
312
            'SwissPaymentSlip\SwissPaymentSlipPdf\Tests\TestablePaymentSlipPdf',
313
            'writePaymentSlipLines'
314
        );
315
        $method->invoke(
316
            $paymentSlipPdf = new TestablePaymentSlipPdf((object)'FooBar'),
317
            'elementName', ['lines' => 'notAnArray', 'attributes' => []]
318
        );
319
    }
320
321
    /**
322
     * Tests the writePaymentSlipLines method with 'attributes' key being no array
323
     *
324
     * @return void
325
     * @expectedException \InvalidArgumentException
326
     * @expectedExceptionMessage $attributes is not an array!
327
     * @covers ::writePaymentSlipLines
328
     */
329 View Code Duplication
    public function testWritePaymentSlipLinesAttributesKeyNoArray()
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...
330
    {
331
        $method = $this->makeMethodAccessible(
332
            'SwissPaymentSlip\SwissPaymentSlipPdf\Tests\TestablePaymentSlipPdf',
333
            'writePaymentSlipLines'
334
        );
335
        $method->invoke(
336
            $paymentSlipPdf = new TestablePaymentSlipPdf((object)'FooBar'),
337
            'elementName',
338
            ['lines' => [], 'attributes' => 'notAnArray']
339
        );
340
    }
341
342
    /**
343
     * Make a protected method public using the Reflection API
344
     *
345
     * @param string $className The full name of the class incl. namespace
346
     * @param string $methodName The name of the method to make accessible.
347
     * @return \ReflectionMethod The now public method
348
     */
349
    protected function makeMethodAccessible($className, $methodName) {
350
        $method = new \ReflectionMethod($className, $methodName);
351
        $method->setAccessible(true);
352
        return $method;
353
    }
354
}
355