PlaceOrderStepTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 66
dl 0
loc 182
c 0
b 0
f 0
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A createRequest() 0 3 1
A testRequireInputShouldBeFalse() 0 7 1
A testPostConditionsShouldReturnTrueWhenOrderPlaceIsReady() 0 12 1
A testPlaceOrderExecuteWhenOrderHaveErrorsShouldLogToFlashMessenger() 0 21 1
A createPlaceOrderStep() 0 15 2
A createGlossaryStorageClientMock() 0 3 1
A createQuoteTransfer() 0 6 1
A testPlaceOrderExecuteWhenOrderSuccessfullyPlacedShouldHaveStoreOrderData() 0 18 1
A createCheckoutClientMock() 0 3 1
A createFlashMessengerMock() 0 3 1
A createShipmentMock() 0 3 1
A testPlaceOrderExecuteWhenExternalRedirectProvidedShouldSetIt() 0 14 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\CheckoutErrorTransfer;
14
use Generated\Shared\Transfer\CheckoutResponseTransfer;
15
use Generated\Shared\Transfer\QuoteTransfer;
16
use Generated\Shared\Transfer\SaveOrderTransfer;
17
use Spryker\Yves\Messenger\FlashMessenger\FlashMessengerInterface;
18
use Spryker\Yves\StepEngine\Dependency\Plugin\Handler\StepHandlerPluginInterface;
19
use SprykerShop\Yves\CheckoutPage\Dependency\Client\CheckoutPageToCheckoutClientInterface;
20
use SprykerShop\Yves\CheckoutPage\Dependency\Client\CheckoutPageToGlossaryStorageClientInterface;
21
use SprykerShop\Yves\CheckoutPage\Process\Steps\PlaceOrderStep;
22
use Symfony\Component\HttpFoundation\Request;
23
24
/**
25
 * Auto-generated group annotations
26
 *
27
 * @group PyzTest
28
 * @group Yves
29
 * @group Checkout
30
 * @group Process
31
 * @group Steps
32
 * @group PlaceOrderStepTest
33
 * Add your own group annotations below this line
34
 */
35
class PlaceOrderStepTest extends Unit
36
{
37
    /**
38
     * @var string
39
     */
40
    protected const LOCALE_NAME_PLACE_ORDER_STEP = 'en_US';
41
42
    /**
43
     * @var string
44
     */
45
    protected const MESSAGE_CHECKOUT_ERROR_TRANSFER = 'MESSAGE_CHECKOUT_ERROR_TRANSFER';
46
47
    /**
48
     * @return void
49
     */
50
    public function testPlaceOrderExecuteWhenExternalRedirectProvidedShouldSetIt(): void
51
    {
52
        $checkoutClientMock = $this->createCheckoutClientMock();
53
        $redirectUrl = 'http://www.ten-kur-toli.lt';
54
55
        $checkoutResponseTransfer = new CheckoutResponseTransfer();
56
        $checkoutResponseTransfer->setIsExternalRedirect(true);
57
        $checkoutResponseTransfer->setRedirectUrl($redirectUrl);
58
        $checkoutClientMock->expects($this->once())->method('placeOrder')->willReturn($checkoutResponseTransfer);
0 ignored issues
show
Bug introduced by
The method expects() does not exist on SprykerShop\Yves\Checkou...CheckoutClientInterface. ( Ignorable by Annotation )

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

58
        $checkoutClientMock->/** @scrutinizer ignore-call */ 
59
                             expects($this->once())->method('placeOrder')->willReturn($checkoutResponseTransfer);

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...
59
60
        $quoteTransfer = $this->createQuoteTransfer();
61
        $placeOrderStep = $this->createPlaceOrderStep($checkoutClientMock);
62
        $placeOrderStep->execute($this->createRequest(), $quoteTransfer);
63
        $this->assertEquals($redirectUrl, $placeOrderStep->getExternalRedirectUrl());
64
    }
65
66
    /**
67
     * @return void
68
     */
69
    public function testPlaceOrderExecuteWhenOrderSuccessfullyPlacedShouldHaveStoreOrderData(): void
70
    {
71
        $checkoutClientMock = $this->createCheckoutClientMock();
72
73
        $checkoutResponseTransfer = new CheckoutResponseTransfer();
74
        $checkoutResponseTransfer->setIsSuccess(true);
75
        $saverOrderTransfer = new SaveOrderTransfer();
76
        $saverOrderTransfer->setOrderReference('#123');
77
        $checkoutResponseTransfer->setSaveOrder($saverOrderTransfer);
78
79
        $checkoutClientMock->expects($this->once())->method('placeOrder')->willReturn($checkoutResponseTransfer);
80
81
        $placeOrderStep = $this->createPlaceOrderStep($checkoutClientMock);
82
        $quoteTransfer = $this->createQuoteTransfer();
83
84
        $placeOrderStep->execute($this->createRequest(), $quoteTransfer);
85
86
        $this->assertTrue($placeOrderStep->postCondition($quoteTransfer));
87
    }
88
89
    /**
90
     * @return void
91
     */
92
    public function testPlaceOrderExecuteWhenOrderHaveErrorsShouldLogToFlashMessenger(): void
93
    {
94
        $checkoutClientMock = $this->createCheckoutClientMock();
95
96
        $checkoutResponseTransfer = new CheckoutResponseTransfer();
97
        $checkoutResponseTransfer->addError(
98
            (new CheckoutErrorTransfer())->setMessage(static::MESSAGE_CHECKOUT_ERROR_TRANSFER),
99
        );
100
        $checkoutResponseTransfer->addError(
101
            (new CheckoutErrorTransfer())->setMessage(static::MESSAGE_CHECKOUT_ERROR_TRANSFER),
102
        );
103
104
        $checkoutClientMock->expects($this->once())->method('placeOrder')->willReturn($checkoutResponseTransfer);
105
106
        $flashMessengerMock = $this->createFlashMessengerMock();
107
        $flashMessengerMock->expects($this->exactly(2))->method('addErrorMessage');
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Spryker\Yves\Messenger\F...FlashMessengerInterface. ( Ignorable by Annotation )

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

107
        $flashMessengerMock->/** @scrutinizer ignore-call */ 
108
                             expects($this->exactly(2))->method('addErrorMessage');

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...
108
109
        $placeOrderStep = $this->createPlaceOrderStep($checkoutClientMock, $flashMessengerMock);
110
111
        $quoteTransfer = $this->createQuoteTransfer();
112
        $placeOrderStep->execute($this->createRequest(), $quoteTransfer);
113
    }
114
115
    /**
116
     * @return void
117
     */
118
    public function testPostConditionsShouldReturnTrueWhenOrderPlaceIsReady(): void
119
    {
120
        $checkoutResponseTransfer = new CheckoutResponseTransfer();
121
        $checkoutResponseTransfer->setIsSuccess(true);
122
        $checkoutClientMock = $this->createCheckoutClientMock();
123
        $checkoutClientMock->expects($this->once())->method('placeOrder')->willReturn($checkoutResponseTransfer);
124
        $placeOrderStep = $this->createPlaceOrderStep($checkoutClientMock);
125
        $quoteTransfer = $this->createQuoteTransfer();
126
        $quoteTransfer->setOrderReference('#123');
127
128
        $placeOrderStep->execute($this->createRequest(), $quoteTransfer);
129
        $this->assertTrue($placeOrderStep->postCondition($quoteTransfer));
130
    }
131
132
    /**
133
     * @return void
134
     */
135
    public function testRequireInputShouldBeFalse(): void
136
    {
137
        $checkoutClientMock = $this->createCheckoutClientMock();
138
        $placeOrderStep = $this->createPlaceOrderStep($checkoutClientMock);
139
140
        $quoteTransfer = $this->createQuoteTransfer();
141
        $this->assertFalse($placeOrderStep->requireInput($quoteTransfer));
142
    }
143
144
    /**
145
     * @param \SprykerShop\Yves\CheckoutPage\Dependency\Client\CheckoutPageToCheckoutClientInterface|\PHPUnit\Framework\MockObject\MockObject $checkoutClientMock
146
     * @param \Spryker\Yves\Messenger\FlashMessenger\FlashMessengerInterface|\PHPUnit\Framework\MockObject\MockObject|null $flashMessengerMock
147
     *
148
     * @return \SprykerShop\Yves\CheckoutPage\Process\Steps\PlaceOrderStep
149
     */
150
    protected function createPlaceOrderStep(
151
        CheckoutPageToCheckoutClientInterface $checkoutClientMock,
152
        $flashMessengerMock = null,
153
    ): PlaceOrderStep {
154
        if ($flashMessengerMock === null) {
155
            $flashMessengerMock = $this->createFlashMessengerMock();
156
        }
157
158
        return new PlaceOrderStep(
159
            $checkoutClientMock,
160
            $flashMessengerMock,
161
            static::LOCALE_NAME_PLACE_ORDER_STEP,
162
            $this->createGlossaryStorageClientMock(),
163
            'place_order',
164
            'escape_route',
165
        );
166
    }
167
168
    /**
169
     * @return \PHPUnit\Framework\MockObject\MockObject|\SprykerShop\Yves\CheckoutPage\Dependency\Client\CheckoutPageToGlossaryStorageClientInterface
170
     */
171
    protected function createGlossaryStorageClientMock(): CheckoutPageToGlossaryStorageClientInterface
172
    {
173
        return $this->getMockBuilder(CheckoutPageToGlossaryStorageClientInterface::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...yStorageClientInterface.
Loading history...
174
    }
175
176
    /**
177
     * @return \Symfony\Component\HttpFoundation\Request
178
     */
179
    protected function createRequest(): Request
180
    {
181
        return Request::createFromGlobals();
182
    }
183
184
    /**
185
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Yves\Messenger\FlashMessenger\FlashMessengerInterface
186
     */
187
    protected function createFlashMessengerMock(): FlashMessengerInterface
188
    {
189
        return $this->getMockBuilder(FlashMessengerInterface::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\Messenger\F...FlashMessengerInterface.
Loading history...
190
    }
191
192
    /**
193
     * @return \PHPUnit\Framework\MockObject\MockObject|\SprykerShop\Yves\CheckoutPage\Dependency\Client\CheckoutPageToCheckoutClientInterface
194
     */
195
    protected function createCheckoutClientMock(): CheckoutPageToCheckoutClientInterface
196
    {
197
        return $this->getMockBuilder(CheckoutPageToCheckoutClientInterface::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...CheckoutClientInterface.
Loading history...
198
    }
199
200
    /**
201
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Yves\StepEngine\Dependency\Plugin\Handler\StepHandlerPluginInterface
202
     */
203
    protected function createShipmentMock(): StepHandlerPluginInterface
204
    {
205
        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...
206
    }
207
208
    /**
209
     * @return \Generated\Shared\Transfer\QuoteTransfer
210
     */
211
    protected function createQuoteTransfer(): QuoteTransfer
212
    {
213
        $quoteTransfer = new QuoteTransfer();
214
        $quoteTransfer->setCheckoutConfirmed(true);
215
216
        return $quoteTransfer;
217
    }
218
}
219