CustomerStepTest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 49
c 0
b 0
f 0
dl 0
loc 147
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testExecuteShouldTriggerAuthHandler() 0 7 1
A testRequireInputWhenCustomerLoggedInShouldReturnFalse() 0 9 1
A testPostConditionWhenCustomerTransferNotSetShouldReturnFalse() 0 4 1
A createAuthHandlerMock() 0 3 1
A testPostConditionWhenCustomerIsLoggedInAndTriesToLoginAsAGuestShouldReturnFalse() 0 12 1
A testPostConditionWhenGuestCustomerSetShouldReturnTrue() 0 9 1
A testRequireInputWhenNotLoggedInAndNotYetSetInQuoteShouldReturnTrue() 0 4 1
A createRequest() 0 3 1
A testPostConditionWhenInvalidCustomerSetShouldReturnFalse() 0 7 1
A createCustomerStep() 0 15 3
A testRequireInputWhenCustomerIsSetShouldReturnTrue() 0 7 1
A createCustomerClientMock() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types = 1);
9
10
namespace PyzTest\Yves\Checkout\Process\Steps;
11
12
use Codeception\Test\Unit;
13
use Generated\Shared\Transfer\CustomerTransfer;
14
use Generated\Shared\Transfer\QuoteTransfer;
15
use Spryker\Yves\StepEngine\Dependency\Plugin\Handler\StepHandlerPluginInterface;
16
use SprykerShop\Yves\CheckoutPage\Dependency\Client\CheckoutPageToCustomerClientInterface;
17
use SprykerShop\Yves\CheckoutPage\Process\Steps\CustomerStep;
18
use Symfony\Component\HttpFoundation\Request;
19
20
/**
21
 * Auto-generated group annotations
22
 *
23
 * @group PyzTest
24
 * @group Yves
25
 * @group Checkout
26
 * @group Process
27
 * @group Steps
28
 * @group CustomerStepTest
29
 * Add your own group annotations below this line
30
 */
31
class CustomerStepTest extends Unit
32
{
33
    /**
34
     * @return void
35
     */
36
    public function testExecuteShouldTriggerAuthHandler(): void
37
    {
38
        $authHandlerMock = $this->createAuthHandlerMock();
39
        $authHandlerMock->expects($this->once())->method('addToDataClass')->willReturnArgument(1);
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Spryker\Yves\StepEngine\...pHandlerPluginInterface. ( Ignorable by Annotation )

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

39
        $authHandlerMock->/** @scrutinizer ignore-call */ 
40
                          expects($this->once())->method('addToDataClass')->willReturnArgument(1);

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...
40
41
        $customerStep = $this->createCustomerStep(null, $authHandlerMock);
42
        $customerStep->execute($this->createRequest(), new QuoteTransfer());
43
    }
44
45
    /**
46
     * @return void
47
     */
48
    public function testPostConditionWhenCustomerTransferNotSetShouldReturnFalse(): void
49
    {
50
        $customerStep = $this->createCustomerStep();
51
        $this->assertFalse($customerStep->postCondition(new QuoteTransfer()));
52
    }
53
54
    /**
55
     * @return void
56
     */
57
    public function testPostConditionWhenCustomerIsLoggedInAndTriesToLoginAsAGuestShouldReturnFalse(): void
58
    {
59
        $customerClientMock = $this->createCustomerClientMock();
60
        $customerClientMock->expects($this->once())->method('getCustomer')->willReturn(new CustomerTransfer());
0 ignored issues
show
Bug introduced by
The method expects() does not exist on SprykerShop\Yves\Checkou...CustomerClientInterface. ( Ignorable by Annotation )

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

60
        $customerClientMock->/** @scrutinizer ignore-call */ 
61
                             expects($this->once())->method('getCustomer')->willReturn(new CustomerTransfer());

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...
61
62
        $customerStep = $this->createCustomerStep($customerClientMock);
63
        $quoteTransfer = new QuoteTransfer();
64
        $customerTransfer = new CustomerTransfer();
65
        $customerTransfer->setIsGuest(true);
66
        $quoteTransfer->setCustomer($customerTransfer);
67
68
        $this->assertFalse($customerStep->postCondition($quoteTransfer));
69
    }
70
71
    /**
72
     * @return void
73
     */
74
    public function testPostConditionWhenInvalidCustomerSetShouldReturnFalse(): void
75
    {
76
        $customerStep = $this->createCustomerStep();
77
        $quoteTransfer = new QuoteTransfer();
78
        $quoteTransfer->setCustomer(new CustomerTransfer());
79
80
        $this->assertFalse($customerStep->postCondition($quoteTransfer));
81
    }
82
83
    /**
84
     * @return void
85
     */
86
    public function testPostConditionWhenGuestCustomerSetShouldReturnTrue(): void
87
    {
88
        $customerStep = $this->createCustomerStep();
89
        $quoteTransfer = new QuoteTransfer();
90
        $customerTransfer = new CustomerTransfer();
91
        $customerTransfer->setIsGuest(true);
92
        $quoteTransfer->setCustomer($customerTransfer);
93
94
        $this->assertTrue($customerStep->postCondition($quoteTransfer));
95
    }
96
97
    /**
98
     * @return void
99
     */
100
    public function testRequireInputWhenCustomerIsSetShouldReturnTrue(): void
101
    {
102
        $customerStep = $this->createCustomerStep();
103
        $quoteTransfer = new QuoteTransfer();
104
        $quoteTransfer->setCustomer(new CustomerTransfer());
105
106
        $this->assertTrue($customerStep->requireInput($quoteTransfer));
107
    }
108
109
    /**
110
     * @return void
111
     */
112
    public function testRequireInputWhenCustomerLoggedInShouldReturnFalse(): void
113
    {
114
        $customerClientMock = $this->createCustomerClientMock();
115
        $customerClientMock->expects($this->once())->method('getCustomer')->willReturn(new CustomerTransfer());
116
117
        $customerStep = $this->createCustomerStep($customerClientMock);
118
        $quoteTransfer = new QuoteTransfer();
119
120
        $this->assertFalse($customerStep->requireInput($quoteTransfer));
121
    }
122
123
    /**
124
     * @return void
125
     */
126
    public function testRequireInputWhenNotLoggedInAndNotYetSetInQuoteShouldReturnTrue(): void
127
    {
128
        $customerStep = $this->createCustomerStep();
129
        $this->assertTrue($customerStep->requireInput(new QuoteTransfer()));
130
    }
131
132
    /**
133
     * @param \PHPUnit\Framework\MockObject\MockObject|\SprykerShop\Yves\CheckoutPage\Dependency\Client\CheckoutPageToCustomerClientInterface|null $customerClientMock
134
     * @param \PHPUnit\Framework\MockObject\MockObject|\Spryker\Yves\StepEngine\Dependency\Plugin\Handler\StepHandlerPluginInterface|null $authHandlerMock
135
     *
136
     * @return \SprykerShop\Yves\CheckoutPage\Process\Steps\CustomerStep
137
     */
138
    protected function createCustomerStep($customerClientMock = null, $authHandlerMock = null): CustomerStep
139
    {
140
        if ($customerClientMock === null) {
141
            $customerClientMock = $this->createCustomerClientMock();
142
        }
143
        if ($authHandlerMock === null) {
144
            $authHandlerMock = $this->createAuthHandlerMock();
145
        }
146
147
        return new CustomerStep(
148
            $customerClientMock,
149
            $authHandlerMock,
150
            'customer_step',
151
            'escape_route',
152
            '/logout',
153
        );
154
    }
155
156
    /**
157
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Yves\StepEngine\Dependency\Plugin\Handler\StepHandlerPluginInterface
158
     */
159
    protected function createAuthHandlerMock(): StepHandlerPluginInterface
160
    {
161
        return $this->getMockBuilder(StepHandlerPluginInterface::class)->getMock();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMockBui...face::class)->getMock() returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Spryker\Yves\StepEngine\...pHandlerPluginInterface.
Loading history...
162
    }
163
164
    /**
165
     * @return \Symfony\Component\HttpFoundation\Request
166
     */
167
    protected function createRequest(): Request
168
    {
169
        return Request::createFromGlobals();
170
    }
171
172
    /**
173
     * @return \PHPUnit\Framework\MockObject\MockObject|\SprykerShop\Yves\CheckoutPage\Dependency\Client\CheckoutPageToCustomerClientInterface
174
     */
175
    protected function createCustomerClientMock(): CheckoutPageToCustomerClientInterface
176
    {
177
        return $this->getMockBuilder(CheckoutPageToCustomerClientInterface::class)->getMock();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMockBui...face::class)->getMock() returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return SprykerShop\Yves\Checkou...CustomerClientInterface.
Loading history...
178
    }
179
}
180