8
8
9
9
namespace Magento \Quote \Test \Unit \Model \Backpressure ;
10
10
11
+ use Magento \Framework \App \DeploymentConfig ;
12
+ use Magento \Framework \Exception \FileSystemException ;
13
+ use Magento \Framework \Exception \RuntimeException ;
11
14
use Magento \Quote \Model \Backpressure \OrderLimitConfigManager ;
12
15
use Magento \Framework \App \Backpressure \ContextInterface ;
13
16
use Magento \Framework \App \Config \ScopeConfigInterface ;
@@ -19,12 +22,17 @@ class OrderLimitConfigManagerTest extends TestCase
19
22
/**
20
23
* @var ScopeConfigInterface|MockObject
21
24
*/
22
- private $ config ;
25
+ private $ scopeConfigMock ;
26
+
27
+ /**
28
+ * @var DeploymentConfig|MockObject
29
+ */
30
+ private $ deploymentConfigMock ;
23
31
24
32
/**
25
33
* @var OrderLimitConfigManager
26
34
*/
27
- private $ model ;
35
+ private OrderLimitConfigManager $ model ;
28
36
29
37
/**
30
38
* @inheritDoc
@@ -33,9 +41,13 @@ protected function setUp(): void
33
41
{
34
42
parent ::setUp ();
35
43
36
- $ this ->config = $ this ->createMock (ScopeConfigInterface::class);
44
+ $ this ->scopeConfigMock = $ this ->createMock (ScopeConfigInterface::class);
45
+ $ this ->deploymentConfigMock = $ this ->createMock (DeploymentConfig::class);
37
46
38
- $ this ->model = new OrderLimitConfigManager ($ this ->config );
47
+ $ this ->model = new OrderLimitConfigManager (
48
+ $ this ->scopeConfigMock ,
49
+ $ this ->deploymentConfigMock
50
+ );
39
51
}
40
52
41
53
/**
@@ -62,6 +74,7 @@ public function getConfigCases(): array
62
74
* @param int $expectedPeriod
63
75
* @return void
64
76
* @dataProvider getConfigCases
77
+ * @throws RuntimeException
65
78
*/
66
79
public function testReadLimit (
67
80
int $ identityType ,
@@ -71,102 +84,61 @@ public function testReadLimit(
71
84
int $ expectedLimit ,
72
85
int $ expectedPeriod
73
86
): void {
74
- $ this ->initConfig ($ guestLimit , $ authLimit , $ period , true );
75
- $ context = $ this ->createContext ($ identityType );
87
+ $ context = $ this ->createMock (ContextInterface::class);
88
+ $ context ->method ('getIdentityType ' )->willReturn ($ identityType );
89
+
90
+ $ this ->scopeConfigMock ->method ('getValue ' )
91
+ ->willReturnMap (
92
+ [
93
+ ['sales/backpressure/limit ' , 'store ' , null , $ authLimit ],
94
+ ['sales/backpressure/guest_limit ' , 'store ' , null , $ guestLimit ],
95
+ ['sales/backpressure/period ' , 'store ' , null , $ period ],
96
+ ]
97
+ );
76
98
77
99
$ limit = $ this ->model ->readLimit ($ context );
78
100
$ this ->assertEquals ($ expectedLimit , $ limit ->getLimit ());
79
101
$ this ->assertEquals ($ expectedPeriod , $ limit ->getPeriod ());
80
102
}
81
103
82
104
/**
83
- * Config variations for enabled check.
84
- *
85
- * @return array
86
- */
87
- public function getEnabledCases (): array
88
- {
89
- return [
90
- 'disabled ' => [100 , 100 , 60 , false , false ],
91
- 'guest-misconfigured-1 ' => [0 , 100 , 60 , true , false ],
92
- 'auth-misconfigured-1 ' => [10 , -1 , 60 , true , false ],
93
- 'period-misconfigured-1 ' => [10 , 111 , 0 , true , false ],
94
- 'enabled ' => [10 , 111 , 60 , true , true ]
95
- ];
96
- }
97
-
98
- /**
99
- * Verify logic behind enabled check.
105
+ * Verify logic behind enabled check
100
106
*
101
- * @param int $guestLimit
102
- * @param int $authLimit
103
- * @param int $period
104
107
* @param bool $enabled
105
108
* @param bool $expected
109
+ * @param string|null $requestLoggerType
106
110
* @return void
111
+ * @throws RuntimeException
112
+ * @throws FileSystemException
107
113
* @dataProvider getEnabledCases
108
114
*/
109
115
public function testIsEnforcementEnabled (
110
- int $ guestLimit ,
111
- int $ authLimit ,
112
- int $ period ,
113
- bool $ enabled ,
114
- bool $ expected
116
+ bool $ enabled ,
117
+ bool $ expected ,
118
+ ?string $ requestLoggerType
115
119
): void {
116
- $ this ->initConfig ($ guestLimit , $ authLimit , $ period , $ enabled );
120
+ $ this ->deploymentConfigMock ->method ('get ' )
121
+ ->with ('backpressure/logger/type ' )
122
+ ->willReturn ($ requestLoggerType );
123
+ $ this ->scopeConfigMock ->method ('isSetFlag ' )
124
+ ->with ('sales/backpressure/enabled ' )
125
+ ->willReturn ($ enabled );
117
126
118
127
$ this ->assertEquals ($ expected , $ this ->model ->isEnforcementEnabled ());
119
128
}
120
129
121
130
/**
122
- * Initialize config mock.
123
- *
124
- * @param int $guest
125
- * @param int $auth
126
- * @param int $period
127
- * @param bool $enabled
128
- * @return void
129
- */
130
- private function initConfig (int $ guest , int $ auth , int $ period , bool $ enabled ): void
131
- {
132
- $ this ->config ->method ('getValue ' )
133
- ->willReturnCallback (
134
- function (string $ path ) use ($ auth , $ guest , $ period ): ?string {
135
- switch ($ path ) {
136
- case 'sales/backpressure/limit ' :
137
- return (string ) $ auth ;
138
- case 'sales/backpressure/guest_limit ' :
139
- return (string ) $ guest ;
140
- case 'sales/backpressure/period ' :
141
- return (string ) $ period ;
142
- }
143
-
144
- return null ;
145
- }
146
- );
147
- $ this ->config ->method ('isSetFlag ' )
148
- ->willReturnCallback (
149
- function (string $ path ) use ($ enabled ): bool {
150
- if ($ path === 'sales/backpressure/enabled ' ) {
151
- return $ enabled ;
152
- }
153
-
154
- return false ;
155
- }
156
- );
157
- }
158
-
159
- /**
160
- * Create backpressure context.
131
+ * Config variations for enabled check.
161
132
*
162
- * @param int $identityType
163
- * @return ContextInterface
133
+ * @return array
164
134
*/
165
- private function createContext ( int $ identityType ): ContextInterface
135
+ public function getEnabledCases ( ): array
166
136
{
167
- $ context = $ this ->createMock (ContextInterface::class);
168
- $ context ->method ('getIdentityType ' )->willReturn ($ identityType );
169
-
170
- return $ context ;
137
+ return [
138
+ 'disabled ' => [false , false , null ],
139
+ 'disabled-request-logger-type-exists ' => [false , false , 'requestLoggerType ' ],
140
+ 'enabled-request-logger-type-not-exist ' => [true , false , null ],
141
+ 'enabled ' => [true , true , 'requestLoggerType ' ],
142
+ ];
171
143
}
172
144
}
0 commit comments