Skip to content

Commit 91ea211

Browse files
committed
withConsecutive method deprecation fix 5
1 parent 66f9f72 commit 91ea211

File tree

14 files changed

+347
-163
lines changed

14 files changed

+347
-163
lines changed

src/Test/Unit/App/Logger/Prepare/ErrorLogFileTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,17 @@ public function testBuildLogContentNotInDeployLog()
122122
$this->fileMock->expects($this->exactly(2))
123123
->method('isExists')
124124
->willReturn(true);
125+
$series = [
126+
[['/init/var/log/cloud.error.log'], 'some build log'],
127+
[['/var/log/cloud.error.log'], 'some deploy log'],
128+
];
125129
$this->fileMock->expects($this->exactly(2))
126130
->method('fileGetContents')
127-
->withConsecutive(['/init/var/log/cloud.error.log'], ['/var/log/cloud.error.log'])
128-
->willReturnOnConsecutiveCalls('some build log', 'some deploy log');
131+
// withConsecutive() alternative.
132+
->willReturnCallback(fn($param) => match ([$param]) {
133+
['/init/var/log/cloud.error.log'] => 'some build log',
134+
['/var/log/cloud.error.log'] => 'some deploy log'
135+
});
129136

130137
$this->fileMock->expects($this->once())
131138
->method('copy')
@@ -142,8 +149,11 @@ public function testBuildLogContentInDeployLog()
142149
->willReturn(true);
143150
$this->fileMock->expects($this->exactly(2))
144151
->method('fileGetContents')
145-
->withConsecutive(['/init/var/log/cloud.error.log'], ['/var/log/cloud.error.log'])
146-
->willReturnOnConsecutiveCalls('some build log', 'some build log, some deploy log');
152+
// withConsecutive() alternative.
153+
->willReturnCallback(fn($param) => match ([$param]) {
154+
['/init/var/log/cloud.error.log'] => 'some build log',
155+
['/var/log/cloud.error.log'] => 'some build log, some deploy log'
156+
});
147157

148158
$this->fileMock->expects($this->never())
149159
->method('copy');

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ public function testExecuteWithErrors()
7575
$error1 = new Error('First error');
7676
$error2 = new Error('Second error');
7777

78+
$series = [
79+
[$outputMock, $error1],
80+
[$outputMock, $error2]
81+
];
82+
83+
var_dump($series);
84+
7885
$this->validatorMock->expects($this->once())
7986
->method('validate')
8087
->willReturn(new Error('State is not ideal'));
@@ -85,10 +92,15 @@ public function testExecuteWithErrors()
8592
]);
8693
$this->outputFormatterMock->expects($this->exactly(2))
8794
->method('writeItem')
88-
->withConsecutive(
89-
[$outputMock, $error1],
90-
[$outputMock, $error2]
91-
);
95+
// withConsecutive() alternative.
96+
->willReturnCallback(
97+
function ($arg1, $arg2) use ($outputMock, $error1, $error2){
98+
if ($arg1 == $outputMock && $arg2 == $error1) {
99+
return $this->outputFormatterMock;
100+
} elseif ($arg1 == $outputMock && $arg2 == $error2) {
101+
return $this->outputFormatterMock;
102+
}
103+
});
92104
$this->outputFormatterMock->expects($this->once())
93105
->method('writeResult')
94106
->with($outputMock, false, 'State is not ideal');

src/Test/Unit/Command/Wizard/Util/OutputFormatterTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,15 @@ public function testWriteResult(): void
4141
{
4242
$this->outputMock->expects($this->exactly(2))
4343
->method('writeln')
44-
->withConsecutive(
45-
['<info>some item</info>'],
46-
['<error>some item</error>']
47-
);
44+
// withConsecutive() alternative.
45+
->willReturnCallback(function ($args) {
46+
static $series = [
47+
'<info>some item</info>',
48+
'<error>some item</error>'
49+
];
50+
$expectedArgs = array_shift($series);
51+
$this->assertSame($expectedArgs, $args);
52+
});
4853

4954
$this->outputFormatter->writeResult(
5055
$this->outputMock,

src/Test/Unit/Config/Validator/GlobalStage/ScdOnDeployTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,18 @@ public function testGetErrors()
9898
$this->scdOnBuildMock->expects($this->once())
9999
->method('validate')
100100
->willReturn($resultMock);
101+
$messages = [
102+
'SCD_ON_DEMAND variable is enabled',
103+
'SKIP_SCD variable is enabled',
104+
'SCD on build is enabled'
105+
];
101106
$this->resultFactoryMock->expects($this->exactly(3))
102107
->method('error')
103-
->withConsecutive(
104-
['SCD_ON_DEMAND variable is enabled'],
105-
['SKIP_SCD variable is enabled'],
106-
['SCD on build is enabled']
108+
// withConsecutive() alternative.
109+
->with(
110+
$this->callback(function (string $message) use (&$messages) {
111+
return array_shift($messages) === $message;
112+
})
107113
);
108114

109115
$this->assertCount(3, $this->validator->getErrors());

src/Test/Unit/DB/ConnectionTest.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,43 @@ public function testAffectingQuery()
171171
':name' => 'John',
172172
':age' => 2
173173
];
174-
174+
$series = [
175+
[':name', 'John', \PDO::PARAM_STR],
176+
[':age', 2, \PDO::PARAM_INT]
177+
];
178+
$matcher = $this->exactly(2);
175179
$this->statementMock->expects($this->exactly(2))
176180
->method('bindValue')
177181
/*->withConsecutive(
178182
[':name', 'John', \PDO::PARAM_STR],
179183
[':age', 2, \PDO::PARAM_INT]
180184
);*/
181-
->willReturnCallback(function (...$args) {
185+
/*->willReturnCallback(function (...$args) {
182186
static $series = [
183187
[':name', 'John', \PDO::PARAM_STR],
184188
[':age', 2, \PDO::PARAM_INT]
185189
];
186190
$expectedArgs = array_shift($series);
187191
$this->assertSame($expectedArgs, $args);
188-
});
192+
});*/
193+
->with(
194+
$this->callback(function ($param) use ($series, $matcher) {
195+
$arguments = $series[$this->resolveInvocations($matcher) - 1]; // retrieves arguments
196+
$this->assertStringContainsString($arguments[0], $param); // performs assertion on the argument
197+
return true;
198+
}),
199+
$this->callback(function ($param) use ($series, $matcher) {
200+
$arguments = $series[$this->resolveInvocations($matcher) - 1]; // retrieves arguments
201+
$this->assertStringContainsString($arguments[1], $param); // performs assertion on the argument
202+
return true;
203+
}),
204+
$this->callback(function ($param) use ($series, $matcher) {
205+
$arguments = $series[$this->resolveInvocations($matcher) - 1]; // retrieves arguments
206+
$this->assertStringContainsString($arguments[2], $param); // performs assertion on the argument
207+
return true;
208+
}),
209+
);
210+
189211
$this->statementMock->expects($this->once())
190212
->method('rowCount')
191213
->willReturn(1);
@@ -202,10 +224,7 @@ public function testQuery()
202224

203225
$this->statementMock->expects($this->exactly(2))
204226
->method('bindValue')
205-
/*->withConsecutive(
206-
[':name', 'John', \PDO::PARAM_STR],
207-
[':age', 2, \PDO::PARAM_INT]
208-
);*/
227+
// withConsecutive() alternative.
209228
->willReturnCallback(function (...$args) {
210229
static $series = [
211230
[':name', 'John', \PDO::PARAM_STR],
@@ -259,4 +278,17 @@ public function getTableNameDataProvider(): array
259278
],
260279
];
261280
}
281+
282+
private function resolveInvocations(\PHPUnit\Framework\MockObject\Rule\InvocationOrder $matcher): int
283+
{
284+
if (method_exists($matcher, 'numberOfInvocations')) { // PHPUnit 10+ (including PHPUnit 12)
285+
return $matcher->numberOfInvocations();
286+
}
287+
288+
if (method_exists($matcher, 'getInvocationCount')) { // before PHPUnit 10
289+
return $matcher->getInvocationCount();
290+
}
291+
292+
$this->fail('Cannot count the number of invocations.');
293+
}
262294
}

src/Test/Unit/DB/DumpGeneratorTest.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,18 @@ protected function tearDown(): void
113113
public function testCreate(bool $removeDefiners)
114114
{
115115
$dumpFilePath = $this->getDumpFilePath('main');
116+
$series = [
117+
'Waiting for lock on db dump.',
118+
'Start creation DB dump for main database...',
119+
'Finished DB dump for main database, it can be found here: ' . $dumpFilePath
120+
];
116121
$this->loggerMock->expects($this->exactly(3))
117122
->method('info')
118-
->withConsecutive(
119-
['Waiting for lock on db dump.'],
120-
['Start creation DB dump for main database...'],
121-
['Finished DB dump for main database, it can be found here: ' . $dumpFilePath]
122-
);
123+
// withConsecutive() alternative.
124+
->willReturnCallback(function ($args) use (&$series) {
125+
$expectedArgs = array_shift($series);
126+
$this->assertSame($expectedArgs, $args);
127+
});
123128
$dumpCommand = $this->getDumpCommand('main');
124129
$this->dumpMock->expects($this->once())
125130
->method('getCommand')
@@ -154,10 +159,15 @@ public function testCreateWithException()
154159
$dumpCommand = $this->getDumpCommand('main');
155160
$this->loggerMock->expects($this->exactly(2))
156161
->method('info')
157-
->withConsecutive(
158-
['Waiting for lock on db dump.'],
159-
['Start creation DB dump for main database...']
160-
);
162+
// withConsecutive() alternative.
163+
->willReturnCallback(function ($args) {
164+
static $series = [
165+
'Waiting for lock on db dump.',
166+
'Start creation DB dump for main database...'
167+
];
168+
$expectedArgs = array_shift($series);
169+
$this->assertSame($expectedArgs, $args);
170+
});
161171
$this->dumpMock->expects($this->once())
162172
->method('getCommand')
163173
->with($this->connectionDataMock)
@@ -201,10 +211,15 @@ public function testLockedFile()
201211
->willReturn(false);
202212
$this->loggerMock->expects($this->exactly(2))
203213
->method('info')
204-
->withConsecutive(
205-
['Waiting for lock on db dump.'],
206-
['Dump process is locked!']
207-
);
214+
// withConsecutive() alternative.
215+
->willReturnCallback(function ($args) {
216+
static $series = [
217+
'Waiting for lock on db dump.',
218+
'Dump process is locked!'
219+
];
220+
$expectedArgs = array_shift($series);
221+
$this->assertSame($expectedArgs, $args);
222+
});
208223
$this->shellMock->expects($this->never())
209224
->method('execute');
210225
$this->dumpGenerator->create('main', $this->connectionDataMock, false, '');

src/Test/Unit/Filesystem/DirectoryCopier/CopySubFolderStrategyTest.php

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,11 @@ public function testCopy()
4747
{
4848
$this->fileMock->expects($this->exactly(2))
4949
->method('isExists')
50-
->withConsecutive(
51-
['fromDir'],
52-
['toDir']
53-
)
54-
->willReturnOnConsecutiveCalls(
55-
true,
56-
false
57-
);
50+
// withConsecutive() alternative.
51+
->willReturnCallback(fn($param) => match ([$param]) {
52+
['fromDir'] => true,
53+
['toDir'] => false
54+
});
5855
$this->fileMock->expects($this->once())
5956
->method('isLink')
6057
->with('toDir')
@@ -75,14 +72,11 @@ public function testCopyToDirectoryIsLink()
7572
{
7673
$this->fileMock->expects($this->exactly(2))
7774
->method('isExists')
78-
->withConsecutive(
79-
['fromDir'],
80-
['toDir']
81-
)
82-
->willReturnOnConsecutiveCalls(
83-
true,
84-
false
85-
);
75+
// withConsecutive() alternative.
76+
->willReturnCallback(fn($param) => match ([$param]) {
77+
['fromDir'] => true,
78+
['toDir'] => false
79+
});
8680
$this->fileMock->expects($this->once())
8781
->method('isLink')
8882
->with('toDir')
@@ -147,14 +141,11 @@ public function testIterativeCopy()
147141

148142
$this->fileMock->expects($this->exactly(2))
149143
->method('isExists')
150-
->withConsecutive(
151-
['fromDir'],
152-
['toDir']
153-
)
154-
->willReturnOnConsecutiveCalls(
155-
true,
156-
true
157-
);
144+
// withConsecutive() alternative.
145+
->willReturnCallback(fn($param) => match ([$param]) {
146+
['fromDir'] => true,
147+
['toDir'] => true
148+
});
158149
$this->fileMock->expects($this->once())
159150
->method('isLink')
160151
->with('toDir')
@@ -165,13 +156,26 @@ public function testIterativeCopy()
165156
->method('getDirectoryIterator')
166157
->with('fromDir')
167158
->willReturn($directoryIteratorMock);
168-
$this->fileMock->expects($this->exactly(2))
159+
$series = [
160+
['fromDir/file1', 'toDir/file1'],
161+
['fromDir/file2', 'toDir/file2']
162+
];
163+
$matcher = $this->exactly(2);
164+
$this->fileMock->expects($matcher)
169165
->method('copy')
170-
->withConsecutive(
171-
['fromDir/file1', 'toDir/file1'],
172-
['fromDir/file2', 'toDir/file2']
166+
// withConsecutive() alternative.
167+
->with(
168+
$this->callback(function ($param) use ($series, $matcher) {
169+
$arguments = $series[$this->resolveInvocations($matcher) - 1]; // retrieves arguments
170+
$this->assertStringContainsString($arguments[0], $param); // performs assertion on the argument
171+
return true;
172+
}),
173+
$this->callback(function ($param) use ($series, $matcher) {
174+
$arguments = $series[$this->resolveInvocations($matcher) - 1]; // retrieves arguments
175+
$this->assertStringContainsString($arguments[1], $param); // performs assertion on the argument
176+
return true;
177+
}),
173178
);
174-
175179
$this->assertTrue($this->copySubFolderStrategy->copy('fromDir', 'toDir'));
176180
}
177181

@@ -263,4 +267,17 @@ function () use ($iteratorData) {
263267

264268
return $iteratorMock;
265269
}
270+
271+
private function resolveInvocations(\PHPUnit\Framework\MockObject\Rule\InvocationOrder $matcher): int
272+
{
273+
if (method_exists($matcher, 'numberOfInvocations')) { // PHPUnit 10+ (including PHPUnit 12)
274+
return $matcher->numberOfInvocations();
275+
}
276+
277+
if (method_exists($matcher, 'getInvocationCount')) { // before PHPUnit 10
278+
return $matcher->getInvocationCount();
279+
}
280+
281+
$this->fail('Cannot count the number of invocations.');
282+
}
266283
}

0 commit comments

Comments
 (0)