ServiceTest::testStartFailure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Beelab\PaypalBundle\Tests\Paypal;
4
5
use Beelab\PaypalBundle\Paypal\Service;
6
use Beelab\PaypalBundle\Test\TransactionStub as Transaction;
7
use PHPUnit\Framework\TestCase;
8
9
/**
10
 * @group unit
11
 */
12
class ServiceTest extends TestCase
13
{
14
    private $gateway;
15
    private $router;
16
    private $service;
17
18
    protected function setUp(): void
19
    {
20
        $this->gateway = $this->createMock('Omnipay\PayPal\ExpressGateway');
21
        $this->router = $this->createMock('Symfony\Component\Routing\RouterInterface');
22
        $config = [
23
            'username' => 'a',
24
            'password' => 'b',
25
            'signature' => 'c',
26
            'currency' => 'EUR',
27
            'return_route' => 'pippo',
28
            'cancel_route' => 'pluto',
29
            'test_mode' => true,
30
        ];
31
        $this->gateway
32
            ->expects($this->once())
33
            ->method('setUsername')
34
            ->with('a')
35
            ->willReturnSelf()
36
        ;
37
        $this->gateway
38
            ->expects($this->once())
39
            ->method('setPassword')
40
            ->with('b')
41
            ->willReturnSelf()
42
        ;
43
        $this->gateway
44
            ->expects($this->once())
45
            ->method('setSignature')
46
            ->with('c')
47
            ->willReturnSelf()
48
        ;
49
        $this->gateway
50
            ->expects($this->once())
51
            ->method('setTestMode')
52
            ->with(true)
53
            ->willReturnSelf()
54
        ;
55
        $this->service = new Service($this->gateway, $this->router, $config);
0 ignored issues
show
Documentation introduced by
$this->gateway is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Omnipay\PayPal\ExpressGateway>.

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...
Documentation introduced by
$this->router is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component\Routing\RouterInterface>.

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...
56
    }
57
58
    public function testStart(): void
59
    {
60
        $request = $this->getRequestMock();
61
        $response = $this->getMockBuilder('Omnipay\Common\Message\ResponseInterface')->getMock();
62
        $this->gateway
63
            ->expects($this->once())
64
            ->method('purchase')
65
            ->willReturn($request)
66
        ;
67
        $request
68
            ->expects($this->once())
69
            ->method('setItems')
70
            ->willReturnSelf();
71
        $request
72
            ->expects($this->once())
73
            ->method('send')
74
            ->willReturn($response)
75
        ;
76
        $response
77
            ->expects($this->once())
78
            ->method('isRedirect')
79
            ->willReturn(true)
80
        ;
81
        $response
82
            ->expects($this->once())
83
            ->method('getTransactionReference')
84
            ->willReturn('ref')
85
        ;
86
87
        $transaction = new Transaction(11);
88
        $this->service->setTransaction($transaction);
89
        $this->service->start();
90
    }
91
92
    public function testStartFailure(): void
93
    {
94
        $this->expectException(\Beelab\PaypalBundle\Paypal\Exception::class);
95
96
        $request = $this->getRequestMock();
97
        $response = $this->getMockBuilder('Omnipay\Common\Message\ResponseInterface')->getMock();
98
        $this->gateway
99
            ->expects($this->once())
100
            ->method('purchase')
101
            ->willReturn($request)
102
        ;
103
        $request
104
            ->expects($this->once())
105
            ->method('setItems')
106
            ->willReturnSelf();
107
        $request
108
            ->expects($this->once())
109
            ->method('send')
110
            ->willReturn($response)
111
        ;
112
        $response
113
            ->expects($this->once())
114
            ->method('isRedirect')
115
            ->willReturn(false)
116
        ;
117
118
        $transaction = new Transaction(11);
119
        $this->service->setTransaction($transaction);
120
        $this->service->start();
121
    }
122
123
    public function testStartWithoutTransaction(): void
124
    {
125
        $this->expectException(\RuntimeException::class);
126
127
        $this->service->start();
128
    }
129
130
    public function testCompleteWithSuccess(): void
131
    {
132
        $request = $this->getRequestMock();
133
        $response = $this->getMockBuilder('Omnipay\Common\Message\ResponseInterface')->getMock();
134
        $this->gateway
135
            ->expects($this->once())
136
            ->method('completePurchase')
137
            ->willReturn($request)
138
        ;
139
        $request
140
            ->expects($this->once())
141
            ->method('setItems')
142
            ->willReturnSelf();
143
        $request
144
            ->expects($this->once())
145
            ->method('send')
146
            ->willReturn($response)
147
        ;
148
        $response
149
            ->expects($this->once())
150
            ->method('getData')
151
            ->willReturn(['ACK' => 'Success'])
152
        ;
153
154
        $transaction = new Transaction(11);
155
        $this->service->setTransaction($transaction);
156
        $this->service->complete();
157
    }
158
159
    public function testCompleteWithFailure(): void
160
    {
161
        $this->expectException(\RuntimeException::class);
162
163
        $request = $this->getRequestMock();
164
        $response = $this->getMockBuilder('Omnipay\Common\Message\ResponseInterface')->getMock();
165
        $this->gateway
166
            ->expects($this->once())
167
            ->method('completePurchase')
168
            ->willReturn($request)
169
        ;
170
        $request
171
            ->expects($this->once())
172
            ->method('setItems')
173
            ->willReturnSelf();
174
        $request
175
            ->expects($this->once())
176
            ->method('send')
177
            ->willReturn($response)
178
        ;
179
        $response
180
            ->expects($this->once())
181
            ->method('getData')
182
            ->willReturn(['foo' => 'bar'])
183
        ;
184
185
        $transaction = new Transaction(11);
186
        $this->service->setTransaction($transaction);
187
        $this->service->complete();
188
    }
189
190
    public function testCompleteWithError(): void
191
    {
192
        $request = $this->getRequestMock();
193
        $response = $this->getMockBuilder('Omnipay\Common\Message\ResponseInterface')->getMock();
194
        $this->gateway
195
            ->expects($this->once())
196
            ->method('completePurchase')
197
            ->willReturn($request)
198
        ;
199
        $request
200
            ->expects($this->once())
201
            ->method('setItems')
202
            ->willReturnSelf();
203
        $request
204
            ->expects($this->once())
205
            ->method('send')
206
            ->willReturn($response)
207
        ;
208
        $response
209
            ->expects($this->once())
210
            ->method('getData')
211
            ->willReturn(['ACK' => 'Failure'])
212
        ;
213
214
        $transaction = new Transaction(11);
215
        $this->service->setTransaction($transaction);
216
        $this->service->complete();
217
    }
218
219
    public function testCompleteWithoutTransaction(): void
220
    {
221
        $this->expectException(\RuntimeException::class);
222
223
        $this->service->complete();
224
    }
225
226
    private function getRequestMock()
227
    {
228
        $methods = ['setItems', 'initialize', 'getParameters', 'getResponse', 'send', 'sendData', 'getData'];
229
230
        return $this->getMockBuilder('Omnipay\Common\Message\RequestInterface')->setMethods($methods)->getMock();
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/pull/3687

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
231
    }
232
}
233