Issues (3641)

...ceFormattedControllerBeforeActionPluginTest.php (1 issue)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerTest\Glue\CartsRestApi\Plugin\GlueApplication;
9
10
use Codeception\Test\Unit;
11
use Spryker\Glue\CartsRestApi\CartsRestApiConfig;
12
use Spryker\Glue\CartsRestApi\CartsRestApiDependencyProvider;
13
use Spryker\Glue\CartsRestApi\Dependency\Client\CartsRestApiToPersistentCartClientInterface;
14
use Spryker\Glue\CartsRestApi\Plugin\GlueApplication\ExpandRequestWithCustomerReferenceFormattedControllerBeforeActionPlugin;
15
use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceBuilder;
16
use SprykerTest\Glue\CartsRestApi\CartRestApiGlueTester;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * Auto-generated group annotations
21
 *
22
 * @group SprykerTest
23
 * @group Glue
24
 * @group CartsRestApi
25
 * @group Plugin
26
 * @group GlueApplication
27
 * @group ExpandRequestWithCustomerReferenceFormattedControllerBeforeActionPluginTest
28
 * Add your own group annotations below this line
29
 */
30
class ExpandRequestWithCustomerReferenceFormattedControllerBeforeActionPluginTest extends Unit
31
{
32
    /**
33
     * @uses \Spryker\Glue\CartsRestApi\Processor\Expander\RequestExpander::REQUEST_KEY_CUSTOMER_REFERENCE
34
     *
35
     * @var string
36
     */
37
    protected const REQUEST_KEY_CUSTOMER_REFERENCE = 'customerReference';
38
39
    /**
40
     * @uses \Spryker\Glue\GlueApplication\Plugin\Application\GlueApplicationApplicationPlugin::SERVICE_RESOURCE_BUILDER
41
     *
42
     * @var string
43
     */
44
    protected const SERVICE_RESOURCE_BUILDER = 'resource_builder';
45
46
    /**
47
     * @var string
48
     */
49
    protected const TEST_CUSTOMER_REFERENCE = 'testCustomerReference';
50
51
    /**
52
     * @var string
53
     */
54
    protected const TEST_HEADER_ANONYMOUS_CUSTOMER_UNIQUE_ID = 'testHeader';
55
56
    /**
57
     * /**
58
     *
59
     * @var \SprykerTest\Glue\CartsRestApi\CartRestApiGlueTester
60
     */
61
    protected CartRestApiGlueTester $tester;
62
63
    /**
64
     * @return void
65
     */
66
    public function _before(): void
67
    {
68
        parent::_before();
69
70
        $this->tester->getContainer()->set(
71
            static::SERVICE_RESOURCE_BUILDER,
72
            new RestResourceBuilder(),
0 ignored issues
show
Deprecated Code introduced by
The class Spryker\Glue\GlueApplica...Api\RestResourceBuilder has been deprecated: Will be removed without replacement. ( Ignorable by Annotation )

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

72
            /** @scrutinizer ignore-deprecated */ new RestResourceBuilder(),
Loading history...
73
        );
74
    }
75
76
    /**
77
     * @return void
78
     */
79
    public function testBeforeActionShouldNotExpandRequestWhenCustomerReferenceIsAlreadySet(): void
80
    {
81
        // Arrange
82
        $request = new Request([], [
83
            static::REQUEST_KEY_CUSTOMER_REFERENCE => static::TEST_CUSTOMER_REFERENCE,
84
        ]);
85
        $request->headers->set(CartsRestApiConfig::HEADER_ANONYMOUS_CUSTOMER_UNIQUE_ID, static::TEST_HEADER_ANONYMOUS_CUSTOMER_UNIQUE_ID);
86
87
        $expandRequestWithCustomerReferenceFormattedControllerBeforeActionPlugin = new ExpandRequestWithCustomerReferenceFormattedControllerBeforeActionPlugin();
88
89
        // Act
90
        $expandRequestWithCustomerReferenceFormattedControllerBeforeActionPlugin->beforeAction($request);
91
92
        // Assert
93
        $this->assertSame(static::TEST_CUSTOMER_REFERENCE, $request->request->get(static::REQUEST_KEY_CUSTOMER_REFERENCE));
94
    }
95
96
    /**
97
     * @return void
98
     */
99
    public function testBeforeActionShouldNotExpandRequestWhenAnonymousCustomerUniqueIdHeaderIsNotProvided(): void
100
    {
101
        // Arrange
102
        $request = new Request();
103
        $expandRequestWithCustomerReferenceFormattedControllerBeforeActionPlugin = new ExpandRequestWithCustomerReferenceFormattedControllerBeforeActionPlugin();
104
105
        // Act
106
        $expandRequestWithCustomerReferenceFormattedControllerBeforeActionPlugin->beforeAction($request);
107
108
        // Assert
109
        $this->assertNull($request->request->get(static::REQUEST_KEY_CUSTOMER_REFERENCE));
110
    }
111
112
    /**
113
     * @return void
114
     */
115
    public function testBeforeActionShouldExpandRequestWithCustomerReference(): void
116
    {
117
        // Arrange
118
        $request = new Request();
119
        $request->headers->set(CartsRestApiConfig::HEADER_ANONYMOUS_CUSTOMER_UNIQUE_ID, static::TEST_HEADER_ANONYMOUS_CUSTOMER_UNIQUE_ID);
120
121
        $this->tester->setDependency(
122
            CartsRestApiDependencyProvider::CLIENT_PERSISTENT_CART,
123
            $this->createPersistentCartClientMock(
124
                static::TEST_HEADER_ANONYMOUS_CUSTOMER_UNIQUE_ID,
125
                static::TEST_CUSTOMER_REFERENCE,
126
            ),
127
        );
128
129
        $expandRequestWithCustomerReferenceFormattedControllerBeforeActionPlugin = new ExpandRequestWithCustomerReferenceFormattedControllerBeforeActionPlugin();
130
131
        // Act
132
        $expandRequestWithCustomerReferenceFormattedControllerBeforeActionPlugin->beforeAction($request);
133
134
        // Assert
135
        $this->assertSame(static::TEST_CUSTOMER_REFERENCE, $request->request->get(static::REQUEST_KEY_CUSTOMER_REFERENCE));
136
    }
137
138
    /**
139
     * @param string $anonymousCustomerUniqueId
140
     * @param string $expectedCustomerReference
141
     *
142
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Glue\CartsRestApi\Dependency\Client\CartsRestApiToPersistentCartClientInterface
143
     */
144
    protected function createPersistentCartClientMock(
145
        string $anonymousCustomerUniqueId,
146
        string $expectedCustomerReference
147
    ): CartsRestApiToPersistentCartClientInterface {
148
        $persistentCartClientMock = $this->getMockBuilder(CartsRestApiToPersistentCartClientInterface::class)
149
            ->getMock();
150
151
        $persistentCartClientMock->method('generateGuestCartCustomerReference')
152
            ->with($anonymousCustomerUniqueId)
153
            ->willReturn($expectedCustomerReference);
154
155
        return $persistentCartClientMock;
156
    }
157
}
158