Passed
Push — master ( 6039c6...64cbfb )
by Francis
01:13 queued 17s
created

testExpiresAnOpenedCheckoutSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 47
rs 9.472
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\FluxSE\PayumStripe\Action\StripeCheckoutSession;
4
5
use FluxSE\PayumStripe\Action\StripeCheckoutSession\LegacyCancelAction;
6
use FluxSE\PayumStripe\Request\Api\Resource\AllSession;
7
use FluxSE\PayumStripe\Request\Api\Resource\ExpireSession;
8
use Payum\Core\Action\ActionInterface;
9
use Payum\Core\Request\Authorize;
10
use Payum\Core\Request\Cancel;
11
use PHPUnit\Framework\TestCase;
12
use Stripe\Checkout\Session;
13
use Stripe\Collection;
14
use Stripe\PaymentIntent;
15
use Tests\FluxSE\PayumStripe\Action\GatewayAwareTestTrait;
16
17
final class LegacyCancelActionTest extends TestCase
18
{
19
    use GatewayAwareTestTrait;
20
21
    public function testShouldImplements(): void
22
    {
23
        $action = new LegacyCancelAction();
24
25
        $this->assertInstanceOf(ActionInterface::class, $action);
26
    }
27
28
    public function testShouldSupportOnlyCaptureAndArrayAccessModelWithCaptureMethodAutomatic(): void
29
    {
30
        $action = new LegacyCancelAction();
31
32
        $this->assertTrue($action->supports(new Cancel(['object' => PaymentIntent::OBJECT_NAME, 'capture_method' => 'automatic'])));
33
        $this->assertFalse($action->supports(new Cancel(['object' => PaymentIntent::OBJECT_NAME, 'capture_method' => 'manual'])));
34
        $this->assertFalse($action->supports(new Cancel([])));
35
        $this->assertFalse($action->supports(new Cancel(['object' => ''])));
36
        $this->assertFalse($action->supports(new Cancel(['object' => 'foo'])));
37
        $this->assertFalse($action->supports(new Cancel(null)));
38
        $this->assertFalse($action->supports(new Authorize(['capture_method' => 'manual'])));
39
    }
40
41
    public function testDoesNothingForNotACheckoutSession(): void
42
    {
43
        $request = new Cancel([
44
            'object' => PaymentIntent::OBJECT_NAME,
45
            'capture_method' => 'automatic',
46
            'id' => 'pi_abc123'
47
        ]);
48
        $action = new LegacyCancelAction();
49
50
        $allSessionRequest = new AllSession(['payment_intent' => 'pi_abc123']);
51
        $gatewayMock = $this->createGatewayMock();
52
        $gatewayMock
53
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Payum\Core\GatewayInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
            ->/** @scrutinizer ignore-call */ 
54
              expects($this->once())

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...
54
            ->method('execute')
55
            ->with($allSessionRequest)
56
            ->willReturn(
57
                $this->returnCallback(
58
                    function (AllSession $request) {
59
                        $sessions = Collection::emptyCollection();
60
                        $request->setApiResources($sessions);
61
                        return true;
62
                    }
63
                )
64
            );
65
66
        $action->setGateway($gatewayMock);
67
        $action->execute($request);
68
    }
69
70
    public function testDoesNothingForNotAnOpenedCheckoutSession(): void
71
    {
72
        $request = new Cancel([
73
            'object' => PaymentIntent::OBJECT_NAME,
74
            'capture_method' => 'automatic',
75
            'id' => 'pi_abc123'
76
        ]);
77
        $action = new LegacyCancelAction();
78
79
        $allSessionRequest = new AllSession(['payment_intent' => 'pi_abc123']);
80
        $gatewayMock = $this->createGatewayMock();
81
        $gatewayMock
82
            ->expects($this->once())
83
            ->method('execute')
84
            ->with($allSessionRequest)
85
            ->willReturn(
86
                $this->returnCallback(
87
                    function (AllSession $request) {
88
                        $sessions = Collection::constructFrom(
89
                            [
90
                                'data' => [
91
                                    [
92
                                        'id' => 'cs_1',
93
                                        'object' => Session::OBJECT_NAME,
94
                                        'status' => Session::STATUS_COMPLETE,
95
                                    ],
96
                                ],
97
                            ]
98
                        );
99
                        $request->setApiResources($sessions);
100
                        return true;
101
                    }
102
                )
103
            );
104
105
        $action->setGateway($gatewayMock);
106
        $action->execute($request);
107
    }
108
109
    public function testExpiresAnOpenedCheckoutSession(): void
110
    {
111
        $request = new Cancel([
112
            'object' => PaymentIntent::OBJECT_NAME,
113
            'capture_method' => 'automatic',
114
            'id' => 'pi_abc123'
115
        ]);
116
        $action = new LegacyCancelAction();
117
118
        $gatewayMock = $this->createGatewayMock();
119
        $gatewayMock
120
            ->expects($this->exactly(2))
121
            ->method('execute')
122
            ->withConsecutive(
123
                [
124
                    $this->callback(
125
                        function (AllSession $request): bool {
126
                            $this->assertSame(['payment_intent' => 'pi_abc123'], $request->getParameters());
127
                            return true;
128
                        }
129
                    ),
130
                ],
131
                [new ExpireSession('cs_1')]
132
            )
133
            ->willReturnOnConsecutiveCalls(
134
                $this->returnCallback(
135
                    function (AllSession $request) {
136
                        $sessions = Collection::constructFrom(
137
                            [
138
                                'data' => [
139
                                    [
140
                                        'id' => 'cs_1',
141
                                        'object' => Session::OBJECT_NAME,
142
                                        'status' => Session::STATUS_OPEN,
143
                                    ],
144
                                ],
145
                            ]
146
                        );
147
                        $request->setApiResources($sessions);
148
                        return true;
149
                    }
150
                ),
151
                $this->anything()
152
            );
153
154
        $action->setGateway($gatewayMock);
155
        $action->execute($request);
156
    }
157
}
158