Passed
Push — master ( 60a4ae...29fcc8 )
by
unknown
46:42 queued 14:26
created

resolveScenarioDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 70
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 38
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 70
rs 9.312

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
declare(strict_types = 1);
9
10
namespace SprykerTest\Client\CmsPageSearch\SearchQueryResolver;
11
12
use Codeception\Test\Unit;
13
use PHPUnit\Framework\MockObject\MockObject;
14
use Spryker\Client\CmsPageSearch\SearchQueryResolver\SearchQueryResolver;
15
use Spryker\Client\SearchExtension\Dependency\Plugin\QueryInterface;
16
17
/**
18
 * Auto-generated group annotations
19
 *
20
 * @group SprykerTest
21
 * @group Client
22
 * @group CmsPageSearch
23
 * @group SearchQueryResolver
24
 * @group SearchQueryResolverTest
25
 * Add your own group annotations below this line
26
 */
27
class SearchQueryResolverTest extends Unit
28
{
29
    /**
30
     * Test resolve method with different plugin configurations.
31
     *
32
     * @dataProvider resolveScenarioDataProvider
33
     *
34
     * @param array<string, mixed> $scenario
35
     * @param string $expectedResultKey
36
     *
37
     * @return void
38
     */
39
    public function testResolveWithDifferentPluginConfigurations(array $scenario, string $expectedResultKey): void
40
    {
41
        // Arrange
42
        $defaultQueryMock = $this->createQueryMock();
43
        $queryMocks = ['default' => $defaultQueryMock];
44
        $searchQueryPlugins = [];
45
46
        // Create query mocks based on scenario
47
        foreach ($scenario['plugins'] as $pluginKey => $pluginConfig) {
48
            if ($pluginConfig['hasApplicabilityChecker']) {
49
                $queryMocks[$pluginKey] = $this->createQueryWithApplicabilityMock($pluginConfig['isApplicable']);
50
            } else {
51
                $queryMocks[$pluginKey] = $this->createQueryMock();
52
            }
53
            $searchQueryPlugins[] = $queryMocks[$pluginKey];
54
        }
55
56
        $resolver = new SearchQueryResolver($searchQueryPlugins, $defaultQueryMock);
57
58
        // Act
59
        $result = $resolver->resolve();
60
61
        // Assert
62
        $this->assertInstanceOf(QueryInterface::class, $result);
63
        // Verify the resolver returned the expected query by checking if it's the same instance
64
        // For scenarios where we expect the default query, check if it's the default
65
        if ($expectedResultKey === 'default') {
66
            $this->assertSame($defaultQueryMock, $result);
67
        } else {
68
            // For plugin queries, verify it's one of the plugin queries
69
            $this->assertContains($result, $searchQueryPlugins);
70
        }
71
    }
72
73
    /**
74
     * @return array<string, array<int, mixed>>
75
     */
76
    public function resolveScenarioDataProvider(): array
77
    {
78
        return [
79
            'no plugins provided' => [
80
                [
81
                    'plugins' => [],
82
                ],
83
                'default',
84
            ],
85
            'single applicable query' => [
86
                [
87
                    'plugins' => [
88
                        'applicable' => ['hasApplicabilityChecker' => true, 'isApplicable' => true],
89
                    ],
90
                ],
91
                'applicable',
92
            ],
93
            'single non-applicable query' => [
94
                [
95
                    'plugins' => [
96
                        'nonApplicable' => ['hasApplicabilityChecker' => true, 'isApplicable' => false],
97
                    ],
98
                ],
99
                'nonApplicable',
100
            ],
101
            'first applicable query when multiple exist' => [
102
                [
103
                    'plugins' => [
104
                        'firstApplicable' => ['hasApplicabilityChecker' => true, 'isApplicable' => true],
105
                        'secondApplicable' => ['hasApplicabilityChecker' => true, 'isApplicable' => true],
106
                    ],
107
                ],
108
                'firstApplicable',
109
            ],
110
            'applicable query found among non-applicable' => [
111
                [
112
                    'plugins' => [
113
                        'nonApplicable' => ['hasApplicabilityChecker' => true, 'isApplicable' => false],
114
                        'applicable' => ['hasApplicabilityChecker' => true, 'isApplicable' => true],
115
                    ],
116
                ],
117
                'applicable',
118
            ],
119
            'last plugin when no applicable found' => [
120
                [
121
                    'plugins' => [
122
                        'firstNonApplicable' => ['hasApplicabilityChecker' => true, 'isApplicable' => false],
123
                        'lastNonApplicable' => ['hasApplicabilityChecker' => true, 'isApplicable' => false],
124
                    ],
125
                ],
126
                'lastNonApplicable',
127
            ],
128
            'last plugin when no applicability checker' => [
129
                [
130
                    'plugins' => [
131
                        'first' => ['hasApplicabilityChecker' => false, 'isApplicable' => false],
132
                        'last' => ['hasApplicabilityChecker' => false, 'isApplicable' => false],
133
                    ],
134
                ],
135
                'last',
136
            ],
137
            'mixed plugin types with applicable found' => [
138
                [
139
                    'plugins' => [
140
                        'regular' => ['hasApplicabilityChecker' => false, 'isApplicable' => false],
141
                        'nonApplicable' => ['hasApplicabilityChecker' => true, 'isApplicable' => false],
142
                        'applicable' => ['hasApplicabilityChecker' => true, 'isApplicable' => true],
143
                    ],
144
                ],
145
                'applicable',
146
            ],
147
        ];
148
    }
149
150
    /**
151
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Client\SearchExtension\Dependency\Plugin\QueryInterface
152
     */
153
    protected function createQueryMock(): MockObject|QueryInterface
154
    {
155
        $mock = $this->createMock(QueryInterface::class);
156
        $mock->method('getSearchQuery')->willReturn($this->createMock('\Elastica\Query'));
157
158
        return $mock;
159
    }
160
161
    /**
162
     * @param bool $isApplicable
163
     *
164
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Client\SearchExtension\Dependency\Plugin\QueryInterface
165
     */
166
    protected function createQueryWithApplicabilityMock(bool $isApplicable): QueryInterface
167
    {
168
        $mock = $this->getMockBuilder(QueryInterface::class)
169
            ->onlyMethods(['getSearchQuery'])
170
            ->addMethods(['isApplicable'])
171
            ->getMock();
172
        $mock->method('getSearchQuery')->willReturn($this->createMock('\Elastica\Query'));
173
        $mock->method('isApplicable')->willReturn($isApplicable);
174
175
        return $mock;
176
    }
177
}
178