Skip to content

Commit 61d35f0

Browse files
committed
withConsecutive method deprecation fix 6
1 parent 91ea211 commit 61d35f0

File tree

9 files changed

+413
-327
lines changed

9 files changed

+413
-327
lines changed

src/Test/Unit/Command/ConfigShow/RendererTest.php

Lines changed: 116 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,43 @@ public function testPrintRelationships()
7474
$this->outputFormatterMock->expects(self::any())
7575
->method('format')
7676
->willReturnArgument(0);
77-
$this->outputMock->expects($this->atLeast(8))
77+
$invokedCount = $this->atLeast(8);
78+
$this->outputMock->expects($invokedCount)
7879
->method('writeln')
79-
->withConsecutive(
80-
[PHP_EOL . '<info>Magento Cloud Services:</info>'],
81-
[$this->anything()],
82-
[$this->matchesRegularExpression('|Service configuration.*?Value|')],
83-
[$this->anything()],
84-
[$this->stringContains('service1')],
85-
[$this->anything()],
86-
[$this->matchesRegularExpression('|option1.*?value1|')],
87-
[$this->matchesRegularExpression('|option2.*?value2|')]
88-
);
80+
// withConsecutive() alternative.
81+
->willReturnCallback(function ($parameters) use ($invokedCount) {
82+
if ($invokedCount->numberOfInvocations() === 1) {
83+
$this->assertSame(PHP_EOL . '<info>Magento Cloud Services:</info>', $parameters);
84+
}
85+
86+
if ($invokedCount->numberOfInvocations() === 2) {
87+
$this->assertThat($parameters, $this->anything());
88+
}
89+
90+
if ($invokedCount->numberOfInvocations() === 3) {
91+
$this->assertMatchesRegularExpression('|Service configuration.*?Value|', $parameters);
92+
}
93+
94+
if ($invokedCount->numberOfInvocations() === 4) {
95+
$this->assertThat($parameters, $this->anything());
96+
}
97+
98+
if ($invokedCount->numberOfInvocations() === 5) {
99+
$this->assertStringContainsString('service1', $parameters);
100+
}
101+
102+
if ($invokedCount->numberOfInvocations() === 6) {
103+
$this->assertThat($parameters, $this->anything());
104+
}
105+
106+
if ($invokedCount->numberOfInvocations() === 7) {
107+
$this->assertMatchesRegularExpression('|option1.*?value1|', $parameters);
108+
}
109+
110+
if ($invokedCount->numberOfInvocations() === 8) {
111+
$this->assertMatchesRegularExpression('|option2.*?value2|', $parameters);
112+
}
113+
});
89114

90115
$this->renderer->printRelationships($this->outputMock);
91116
}
@@ -102,18 +127,43 @@ public function testPrintRoutes()
102127
$this->outputFormatterMock->expects(self::any())
103128
->method('format')
104129
->willReturnArgument(0);
105-
$this->outputMock->expects($this->atLeast(8))
130+
$invokedCount = $this->atLeast(8);
131+
$this->outputMock->expects($invokedCount)
106132
->method('writeln')
107-
->withConsecutive(
108-
[PHP_EOL . '<info>Magento Cloud Routes:</info>'],
109-
[$this->anything()],
110-
[$this->matchesRegularExpression('|Route configuration.*?Value|')],
111-
[$this->anything()],
112-
[$this->stringContains('route1')],
113-
[$this->anything()],
114-
[$this->anything()],
115-
[$this->matchesRegularExpression('|option1.*?value1|')]
116-
);
133+
// withConsecutive() alternative.
134+
->willReturnCallback(function ($parameters) use ($invokedCount) {
135+
if ($invokedCount->numberOfInvocations() === 1) {
136+
$this->assertSame(PHP_EOL . '<info>Magento Cloud Routes:</info>', $parameters);
137+
}
138+
139+
if ($invokedCount->numberOfInvocations() === 2) {
140+
$this->assertThat($parameters, $this->anything());
141+
}
142+
143+
if ($invokedCount->numberOfInvocations() === 3) {
144+
$this->assertMatchesRegularExpression('|Route configuration.*?Value|', $parameters);
145+
}
146+
147+
if ($invokedCount->numberOfInvocations() === 4) {
148+
$this->assertThat($parameters, $this->anything());
149+
}
150+
151+
if ($invokedCount->numberOfInvocations() === 5) {
152+
$this->assertStringContainsString('route1', $parameters);
153+
}
154+
155+
if ($invokedCount->numberOfInvocations() === 6) {
156+
$this->assertThat($parameters, $this->anything());
157+
}
158+
159+
if ($invokedCount->numberOfInvocations() === 7) {
160+
$this->assertThat($parameters, $this->anything());
161+
}
162+
163+
if ($invokedCount->numberOfInvocations() === 8) {
164+
$this->assertMatchesRegularExpression('|option1.*?value1|', $parameters);
165+
}
166+
});
117167

118168
$this->renderer->printRoutes($this->outputMock);
119169
}
@@ -134,20 +184,51 @@ public function testPrintVariables()
134184
$this->outputFormatterMock->expects(self::any())
135185
->method('format')
136186
->willReturnArgument(0);
137-
$this->outputMock->expects($this->atLeast(10))
187+
$invokedCount = $this->atLeast(8);
188+
$this->outputMock->expects($invokedCount)
138189
->method('writeln')
139-
->withConsecutive(
140-
[PHP_EOL . '<info>Magento Cloud Environment Variables:</info>'],
141-
[$this->anything()],
142-
[$this->matchesRegularExpression('|Variable name.*?Value|')],
143-
[$this->anything()],
144-
[$this->matchesRegularExpression('|variable1.*?value1|')],
145-
[$this->matchesRegularExpression('|variable2.*?null|')],
146-
[$this->matchesRegularExpression('|variable3.*?true|')],
147-
[$this->stringContains('variable4')],
148-
[$this->matchesRegularExpression('|option1.*?false|')],
149-
[$this->matchesRegularExpression('|option2.*?optionValue2|')]
150-
);
190+
// withConsecutive() alternative.
191+
->willReturnCallback(function ($parameters) use ($invokedCount) {
192+
if ($invokedCount->numberOfInvocations() === 1) {
193+
$this->assertSame(PHP_EOL . '<info>Magento Cloud Environment Variables:</info>', $parameters);
194+
}
195+
196+
if ($invokedCount->numberOfInvocations() === 2) {
197+
$this->assertThat($parameters, $this->anything());
198+
}
199+
200+
if ($invokedCount->numberOfInvocations() === 3) {
201+
$this->assertMatchesRegularExpression('|Variable name.*?Value|', $parameters);
202+
}
203+
204+
if ($invokedCount->numberOfInvocations() === 4) {
205+
$this->assertThat($parameters, $this->anything());
206+
}
207+
208+
if ($invokedCount->numberOfInvocations() === 5) {
209+
$this->assertMatchesRegularExpression('|variable1.*?value1|', $parameters);
210+
}
211+
212+
if ($invokedCount->numberOfInvocations() === 6) {
213+
$this->assertMatchesRegularExpression('|variable2.*?null|', $parameters);
214+
}
215+
216+
if ($invokedCount->numberOfInvocations() === 7) {
217+
$this->assertMatchesRegularExpression('|variable3.*?true|', $parameters);
218+
}
219+
220+
if ($invokedCount->numberOfInvocations() === 8) {
221+
$this->assertStringContainsString('variable4', $parameters);
222+
}
223+
224+
if ($invokedCount->numberOfInvocations() === 9) {
225+
$this->assertMatchesRegularExpression('|option1.*?false|', $parameters);
226+
}
227+
228+
if ($invokedCount->numberOfInvocations() === 10) {
229+
$this->assertMatchesRegularExpression('|option2.*?optionValue2|', $parameters);
230+
}
231+
});
151232

152233
$this->renderer->printVariables($this->outputMock);
153234
}

src/Test/Unit/Command/Wizard/IdealStateTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ public function testExecuteWithErrors()
8080
[$outputMock, $error2]
8181
];
8282

83-
var_dump($series);
84-
8583
$this->validatorMock->expects($this->once())
8684
->method('validate')
8785
->willReturn(new Error('State is not ideal'));

src/Test/Unit/Config/ModuleTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,20 @@ public function testRefreshWithNewModules(): void
139139
->with('module:enable --all', ['']);
140140
$this->writerMock->expects($this->exactly(2))
141141
->method('update')
142-
->withConsecutive(
143-
[['modules' => []]],
144-
[
145-
[
142+
// withConsecutive() alternative.
143+
->with(self::callback(function (array $message) {
144+
static $i = 0;
145+
return match (++$i) {
146+
1 => $message === ['modules' => []],
147+
2 => $message === [
146148
'modules' => [
147149
'Magento_Module1' => 1,
148150
'Magento_Module2' => 0,
149151
'Magento_Module3' => 1,
150152
]
151-
]
152-
]
153-
);
153+
],
154+
};
155+
}));
154156

155157
$this->assertEquals(
156158
[

src/Test/Unit/Config/Validator/Deploy/ServiceVersionTest.php

Lines changed: 52 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -117,62 +117,30 @@ public function testValidate(): void
117117
$serviceOS,
118118
$serviceMariaDB
119119
);
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-
});
143120
$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', []]
149127
];
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+
);
176144
$this->resultFactoryMock->expects($this->once())
177145
->method('success');
178146

@@ -221,23 +189,23 @@ public function testValidateWithErrors(): void
221189
->willReturnOnConsecutiveCalls($service1, $service2, $service3, $service4, $service5, $service6);
222190
$this->serviceVersionValidatorMock->expects($this->exactly(6))
223191
->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+
});
241209
$this->resultFactoryMock->expects($this->once())
242210
->method('error')
243211
->with($this->anything(), implode(PHP_EOL, $errorMessages));
@@ -256,4 +224,17 @@ public function testValidateWithException(): void
256224

257225
$this->validator->validate();
258226
}
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+
}
259240
}

0 commit comments

Comments
 (0)