@@ -117,62 +117,30 @@ public function testValidate(): void
117
117
$ serviceOS ,
118
118
$ serviceMariaDB
119
119
);
120
- $ this ->loggerMock ->expects ($ this ->exactly (6 ))
121
- ->method ('info ' )
122
- /*->withConsecutive(
123
- ['Version of service \'rabbitmq\' is not detected', []],
124
- ['Version of service \'redis\' is 3.2', []],
125
- ['Version of service \'redis-session\' is 3.2', []],
126
- ['Version of service \'elasticsearch\' is 7.7', []],
127
- ['Version of service \'opensearch\' is 1.2', []],
128
- ['Version of service \'mariadb\' is 10.2', []]
129
- );*/
130
- // withConsecutive() alternative.
131
- ->willReturnCallback (function (...$ args ) {
132
- static $ series = [
133
- ['Version of service \'rabbitmq \' is not detected ' , []],
134
- ['Version of service \'redis \' is 3.2 ' , []],
135
- ['Version of service \'redis-session \' is 3.2 ' , []],
136
- ['Version of service \'elasticsearch \' is 7.7 ' , []],
137
- ['Version of service \'opensearch \' is 1.2 ' , []],
138
- ['Version of service \'mariadb \' is 10.2 ' , []]
139
- ];
140
- $ expectedArgs = array_shift ($ series );
141
- $ this ->assertSame ($ expectedArgs , $ args );
142
- });
143
120
$ series = [
144
- [ServiceInterface::NAME_REDIS , '3.2 ' ],
145
- [ServiceInterface::NAME_REDIS_SESSION , '3.2 ' ],
146
- [ServiceInterface::NAME_ELASTICSEARCH , '7.7 ' ],
147
- [ServiceInterface::NAME_OPENSEARCH , '1.2 ' ],
148
- [ServiceInterface::NAME_DB_MARIA , '10.2 ' ]
121
+ ['Version of service \'rabbitmq \' is not detected ' , []],
122
+ ['Version of service \'redis \' is 3.2 ' , []],
123
+ ['Version of service \'redis-session \' is 3.2 ' , []],
124
+ ['Version of service \'elasticsearch \' is 7.7 ' , []],
125
+ ['Version of service \'opensearch \' is 1.2 ' , []],
126
+ ['Version of service \'mariadb \' is 10.2 ' , []]
149
127
];
150
- $ this ->serviceVersionValidatorMock ->expects ($ this ->exactly (5 ))
151
- ->method ('validateService ' )
152
- /*->withConsecutive(
153
- [ServiceInterface::NAME_REDIS, '3.2'],
154
- [ServiceInterface::NAME_REDIS_SESSION, '3.2'],
155
- [ServiceInterface::NAME_ELASTICSEARCH, '7.7'],
156
- [ServiceInterface::NAME_OPENSEARCH, '1.2'],
157
- [ServiceInterface::NAME_DB_MARIA, '10.2']
158
- )
159
- ->willReturn('');*/
160
- /*->willReturnCallback(function ($arg1) use ($series) {
161
- if (in_array($arg1, $series)) {
162
- return 'test';
163
- }
164
- });*/
165
- ->willReturnCallback (function (...$ args ) {
166
- static $ series = [
167
- [ServiceInterface::NAME_REDIS , '3.2 ' ],
168
- [ServiceInterface::NAME_REDIS_SESSION , '3.2 ' ],
169
- [ServiceInterface::NAME_ELASTICSEARCH , '7.7 ' ],
170
- [ServiceInterface::NAME_OPENSEARCH , '1.2 ' ],
171
- [ServiceInterface::NAME_DB_MARIA , '10.2 ' ]
172
- ];
173
- $ expectedArgs = array_shift ($ series );
174
- $ this ->assertSame ($ expectedArgs , $ args );
175
- });
128
+ $ matcher = $ this ->exactly (6 );
129
+ $ this ->loggerMock ->expects ($ matcher )
130
+ ->method ('info ' )
131
+ // withConsecutive() alternative.
132
+ ->with (
133
+ $ this ->callback (function ($ param ) use ($ series , $ matcher ) {
134
+ $ arguments = $ series [$ this ->resolveInvocations ($ matcher ) - 1 ]; // retrieves arguments
135
+ $ this ->assertStringContainsString ($ arguments [0 ], $ param ); // performs assertion on the argument
136
+ return true ;
137
+ }),
138
+ $ this ->callback (function ($ param ) use ($ series , $ matcher ) {
139
+ $ arguments = $ series [$ this ->resolveInvocations ($ matcher ) - 1 ]; // retrieves arguments
140
+ $ this ->assertSame ($ arguments [1 ], $ param ); // performs assertion on the argument
141
+ return true ;
142
+ }),
143
+ );
176
144
$ this ->resultFactoryMock ->expects ($ this ->once ())
177
145
->method ('success ' );
178
146
@@ -221,23 +189,23 @@ public function testValidateWithErrors(): void
221
189
->willReturnOnConsecutiveCalls ($ service1 , $ service2 , $ service3 , $ service4 , $ service5 , $ service6 );
222
190
$ this ->serviceVersionValidatorMock ->expects ($ this ->exactly (6 ))
223
191
->method ('validateService ' )
224
- /*-> withConsecutive(
225
- [ServiceInterface::NAME_RABBITMQ, '1.5'],
226
- [ServiceInterface::NAME_REDIS, '2.2'],
227
- [ ServiceInterface::NAME_REDIS_SESSION, '2.2'],
228
- [ServiceInterface::NAME_ELASTICSEARCH, '7.7'],
229
- [ ServiceInterface::NAME_OPENSEARCH, '1 .2'],
230
- [ServiceInterface::NAME_DB_MYSQL, '5.7']
231
- )
232
- ->willReturnOnConsecutiveCalls(... $errorMessages);*/
233
- -> willReturnCallback ( fn ( $ param ) => match ( $ param ) {
234
- ServiceInterface:: NAME_RABBITMQ , ' 1.5 ' => $ errorMessages,
235
- ServiceInterface::NAME_REDIS , ' 2 .2 ' => $ errorMessages ,
236
- ServiceInterface:: NAME_REDIS_SESSION , ' 2.2 ' => $ errorMessages,
237
- ServiceInterface::NAME_ELASTICSEARCH , ' 7 .7 ' => $ errorMessages ,
238
- ServiceInterface:: NAME_OPENSEARCH , ' 1.2 ' => $ errorMessages,
239
- ServiceInterface:: NAME_DB_MYSQL , ' 5.7 ' => $ errorMessages
240
- });
192
+ // withConsecutive() alternative.
193
+ -> willReturnCallback (
194
+ function ( $ arg1 , $ arg2 ) use ( $ errorMessages ){
195
+ if ( $ arg1 == ServiceInterface::NAME_RABBITMQ && $ arg2 == ' 1.5 ' ) {
196
+ return $ errorMessages [ 0 ];
197
+ } elseif ( $ arg1 == ServiceInterface::NAME_REDIS && $ arg2 == ' 2 .2 ') {
198
+ return $ errorMessages [ 1 ];
199
+ } elseif ( $ arg1 == ServiceInterface:: NAME_REDIS_SESSION && $ arg2 == ' 2.2 ' ) {
200
+ return $ errorMessages[ 2 ];
201
+ } elseif ( $ arg1 == ServiceInterface:: NAME_ELASTICSEARCH && $ arg2 == ' 7.7 ' ) {
202
+ return $ errorMessages[ 3 ];
203
+ } elseif ( $ arg1 == ServiceInterface::NAME_OPENSEARCH && $ arg2 == ' 1 .2 ') {
204
+ return $ errorMessages[ 4 ];
205
+ } elseif ( $ arg1 == ServiceInterface::NAME_DB_MYSQL && $ arg2 == ' 5 .7 ') {
206
+ return $ errorMessages[ 5 ];
207
+ }
208
+ });
241
209
$ this ->resultFactoryMock ->expects ($ this ->once ())
242
210
->method ('error ' )
243
211
->with ($ this ->anything (), implode (PHP_EOL , $ errorMessages ));
@@ -256,4 +224,17 @@ public function testValidateWithException(): void
256
224
257
225
$ this ->validator ->validate ();
258
226
}
227
+
228
+ private function resolveInvocations (\PHPUnit \Framework \MockObject \Rule \InvocationOrder $ matcher ): int
229
+ {
230
+ if (method_exists ($ matcher , 'numberOfInvocations ' )) { // PHPUnit 10+ (including PHPUnit 12)
231
+ return $ matcher ->numberOfInvocations ();
232
+ }
233
+
234
+ if (method_exists ($ matcher , 'getInvocationCount ' )) { // before PHPUnit 10
235
+ return $ matcher ->getInvocationCount ();
236
+ }
237
+
238
+ $ this ->fail ('Cannot count the number of invocations. ' );
239
+ }
259
240
}
0 commit comments