Skip to content

Commit dc98c25

Browse files
committed
Upgraded test suite to new PHPUnit 13 stricter semantics on mock/stub expectations
1 parent 4209375 commit dc98c25

38 files changed

Lines changed: 283 additions & 122 deletions

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
failOnPhpunitDeprecation="true"
1111
failOnNotice="true"
1212
failOnEmptyTestSuite="true"
13+
failOnPhpunitNotice="true"
1314
cacheDirectory=".phpunit.cache"
1415
>
1516
<testsuites>

test/unit/Command/AssertBackwardsCompatibleTest.php

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPUnit\Framework\Attributes\CoversClass;
99
use PHPUnit\Framework\Attributes\DataProvider;
1010
use PHPUnit\Framework\MockObject\MockObject;
11+
use PHPUnit\Framework\MockObject\Stub;
1112
use PHPUnit\Framework\TestCase;
1213
use Psl\Env;
1314
use Psl\Exception\InvariantViolationException;
@@ -42,7 +43,7 @@
4243
final class AssertBackwardsCompatibleTest extends TestCase
4344
{
4445
private CheckedOutRepository $sourceRepository;
45-
/** @var InputInterface&MockObject */
46+
/** @var InputInterface&Stub */
4647
private InputInterface $input;
4748
/** @var ConsoleOutputInterface&MockObject */
4849
private ConsoleOutputInterface $output;
@@ -71,7 +72,7 @@ public function setUp(): void
7172

7273
Env\set_current_dir($this->sourceRepository->__toString());
7374

74-
$this->input = $this->createMock(InputInterface::class);
75+
$this->input = $this->createStub(InputInterface::class);
7576
$this->output = $this->createMock(ConsoleOutputInterface::class);
7677
$this->stdErr = $this->createMock(OutputInterface::class);
7778
$this->performCheckout = new PerformCheckoutOfRevisionForTests();
@@ -99,6 +100,36 @@ public function setUp(): void
99100

100101
public function testDefinition(): void
101102
{
103+
$this->parseRevision
104+
->expects(self::never())
105+
->method('fromStringForRepository');
106+
107+
$this->locateDependencies
108+
->expects(self::never())
109+
->method('__invoke');
110+
111+
$this->compareApi
112+
->expects(self::never())
113+
->method('__invoke');
114+
115+
$this->stdErr
116+
->expects(self::never())
117+
->method('writeln');
118+
119+
$this->output
120+
->expects(self::never())
121+
->method('writeln');
122+
123+
$this
124+
->getVersions
125+
->expects(self::never())
126+
->method('fromRepository');
127+
128+
$this
129+
->pickVersion
130+
->expects(self::never())
131+
->method('forVersions');
132+
102133
self::assertSame(
103134
'roave-backwards-compatibility-check:assert-backwards-compatible',
104135
$this->compare->getName(),
@@ -144,12 +175,31 @@ public function testExecuteWhenRevisionsAreProvidedAsOptions(): void
144175

145176
$this
146177
->locateDependencies
178+
->expects(self::atLeastOnce())
147179
->method('__invoke')
148180
->with((string) $this->sourceRepository, false)
149181
->willReturn($this->dependencies);
150182

151183
$this->compareApi->expects(self::once())->method('__invoke')->willReturn(Changes::empty());
152184

185+
$this->stdErr
186+
->expects(self::atLeastOnce())
187+
->method('writeln');
188+
189+
$this->output
190+
->expects(self::never())
191+
->method('writeln');
192+
193+
$this
194+
->getVersions
195+
->expects(self::never())
196+
->method('fromRepository');
197+
198+
$this
199+
->pickVersion
200+
->expects(self::never())
201+
->method('forVersions');
202+
153203
self::assertSame(0, $this->compare->execute($this->input, $this->output));
154204
self::assertSame(0, $this->performCheckout->nonRemovedRepositoryCount());
155205
}
@@ -174,12 +224,31 @@ public function testExecuteWhenDevelopmentDependenciesAreRequested(): void
174224

175225
$this
176226
->locateDependencies
227+
->expects(self::atLeastOnce())
177228
->method('__invoke')
178229
->with((string) $this->sourceRepository, true)
179230
->willReturn($this->dependencies);
180231

181232
$this->compareApi->expects(self::once())->method('__invoke')->willReturn(Changes::empty());
182233

234+
$this->stdErr
235+
->expects(self::atLeastOnce())
236+
->method('writeln');
237+
238+
$this->output
239+
->expects(self::never())
240+
->method('writeln');
241+
242+
$this
243+
->getVersions
244+
->expects(self::never())
245+
->method('fromRepository');
246+
247+
$this
248+
->pickVersion
249+
->expects(self::never())
250+
->method('forVersions');
251+
183252
self::assertSame(0, $this->compare->execute($this->input, $this->output));
184253
self::assertSame(0, $this->performCheckout->nonRemovedRepositoryCount());
185254
}
@@ -205,6 +274,7 @@ public function testExecuteReturnsNonZeroExitCodeWhenChangesAreDetected(): void
205274

206275
$this
207276
->locateDependencies
277+
->expects(self::atLeastOnce())
208278
->method('__invoke')
209279
->with((string) $this->sourceRepository, false)
210280
->willReturn($this->dependencies);
@@ -223,6 +293,20 @@ public function testExecuteReturnsNonZeroExitCodeWhenChangesAreDetected(): void
223293
self::matches('<error>1 backwards-incompatible changes detected</error>'),
224294
));
225295

296+
$this->output
297+
->expects(self::never())
298+
->method('writeln');
299+
300+
$this
301+
->getVersions
302+
->expects(self::never())
303+
->method('fromRepository');
304+
305+
$this
306+
->pickVersion
307+
->expects(self::never())
308+
->method('forVersions');
309+
226310
self::assertSame(3, $this->compare->execute($this->input, $this->output));
227311
self::assertSame(0, $this->performCheckout->nonRemovedRepositoryCount());
228312
}
@@ -248,6 +332,7 @@ public function testProvidingMarkdownOptionWritesMarkdownOutput(): void
248332

249333
$this
250334
->locateDependencies
335+
->expects(self::atLeastOnce())
251336
->method('__invoke')
252337
->with((string) $this->sourceRepository, false)
253338
->willReturn($this->dependencies);
@@ -264,6 +349,20 @@ public function testProvidingMarkdownOptionWritesMarkdownOutput(): void
264349
self::assertStringContainsString(' [BC] ' . $changeToExpect, $output);
265350
});
266351

352+
$this->stdErr
353+
->expects(self::atLeastOnce())
354+
->method('writeln');
355+
356+
$this
357+
->getVersions
358+
->expects(self::never())
359+
->method('fromRepository');
360+
361+
$this
362+
->pickVersion
363+
->expects(self::never())
364+
->method('forVersions');
365+
267366
$this->compare->execute($this->input, $this->output);
268367
self::assertSame(0, $this->performCheckout->nonRemovedRepositoryCount());
269368
}
@@ -288,15 +387,29 @@ public function testExecuteWithDefaultRevisionsNotProvidedAndNoDetectedTags(): v
288387
->expects(self::once())
289388
->method('fromRepository')
290389
->willReturn(new VersionCollection());
390+
291391
$this
292392
->pickVersion
293393
->expects(self::never())
294394
->method('forVersions');
395+
295396
$this
296397
->compareApi
297398
->expects(self::never())
298399
->method('__invoke');
299400

401+
$this->output
402+
->expects(self::never())
403+
->method('writeln');
404+
405+
$this->stdErr
406+
->expects(self::never())
407+
->method('writeln');
408+
409+
$this->locateDependencies
410+
->expects(self::never())
411+
->method('__invoke');
412+
300413
$this->expectException(InvariantViolationException::class);
301414

302415
$this->compare->execute($this->input, $this->output);
@@ -327,18 +440,29 @@ public function testExecuteWithDefaultRevisionsNotProvided(VersionCollection $ve
327440
default => throw new LogicException(),
328441
});
329442

330-
$this->getVersions->expects(self::once())
443+
$this->getVersions
444+
->expects(self::once())
331445
->method('fromRepository')
332446
->with(self::callback(function (CheckedOutRepository $checkedOutRepository): bool {
333447
self::assertEquals($this->sourceRepository, $checkedOutRepository);
334448

335449
return true;
336450
}))
337451
->willReturn($versions);
338-
$this->pickVersion->expects(self::once())
452+
453+
$this->pickVersion
454+
->expects(self::once())
339455
->method('forVersions')
340456
->with($versions)
341457
->willReturn($pickedVersion);
458+
459+
$this->output
460+
->expects(self::never())
461+
->method('writeln');
462+
463+
$this->locateDependencies
464+
->expects(self::atLeastOnce())
465+
->method('__invoke');
342466

343467
$this
344468
->stdErr

test/unit/CompareClassesTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ public function testAnonymousClassesAreFilteredOut(): void
167167

168168
public function testSkipsReflectingUndefinedApi(): void
169169
{
170+
$this->traitBasedComparatorWillNotBeCalled();
171+
$this->interfaceBasedComparatorWillNotBeCalled();
170172
$this->classBasedComparatorWillNotBeCalled();
171173

172174
Assertion::assertChangesEqual(

test/unit/DetectChanges/BCBreak/ClassBased/ExcludeAnonymousClassesTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class ANormalClass {}
3939
);
4040

4141
$check = $this->createMock(ClassBased::class);
42-
$check->expects(self::once())
42+
$check
43+
->expects(self::once())
4344
->method('__invoke')
4445
->with($fromReflection, $toReflection)
4546
->willReturn($changes);

test/unit/DetectChanges/BCBreak/ClassBased/ExcludeInternalClassTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ class ANormalClass {}
3333
$toReflection = $reflector->reflectClass('ANormalClass');
3434

3535
$check = $this->createMock(ClassBased::class);
36-
$check->expects(self::once())
37-
->method('__invoke')
38-
->with($fromReflection, $toReflection)
39-
->willReturn(Changes::fromList(Change::removed('foo', true)));
36+
$check
37+
->expects(self::atLeastOnce())
38+
->method('__invoke')
39+
->with($fromReflection, $toReflection)
40+
->willReturn(Changes::fromList(Change::removed('foo', true)));
4041

4142
self::assertEquals(
4243
Changes::fromList(Change::removed('foo', true)),

test/unit/DetectChanges/BCBreak/ClassBased/FinalClassChangedTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\Attributes\CoversClass;
88
use PHPUnit\Framework\MockObject\MockObject;
9+
use PHPUnit\Framework\MockObject\Stub;
910
use PHPUnit\Framework\TestCase;
1011
use Roave\BackwardCompatibility\Change;
1112
use Roave\BackwardCompatibility\Changes;
@@ -23,10 +24,10 @@ final class FinalClassChangedTest extends TestCase
2324

2425
private FinalClassChanged $finalClassChanged;
2526

26-
/** @var ReflectionClass&MockObject */
27+
/** @var ReflectionClass&Stub */
2728
private ReflectionClass $fromClass;
2829

29-
/** @var ReflectionClass&MockObject */
30+
/** @var ReflectionClass&Stub */
3031
private ReflectionClass $toClass;
3132

3233
protected function setUp(): void
@@ -35,8 +36,8 @@ protected function setUp(): void
3536

3637
$this->check = $this->createMock(ClassBased::class);
3738
$this->finalClassChanged = new FinalClassChanged($this->check);
38-
$this->fromClass = $this->createMock(ReflectionClass::class);
39-
$this->toClass = $this->createMock(ReflectionClass::class);
39+
$this->fromClass = $this->createStub(ReflectionClass::class);
40+
$this->toClass = $this->createStub(ReflectionClass::class);
4041
}
4142

4243
public function testWillCheckFinalClass(): void

test/unit/DetectChanges/BCBreak/ClassBased/MultipleChecksOnAClassTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public function testChecksAllGivenCheckers(): void
2424

2525
$multiCheck = new MultipleChecksOnAClass($checker1, $checker2, $checker3);
2626

27-
$from = $this->createMock(ReflectionClass::class);
28-
$to = $this->createMock(ReflectionClass::class);
27+
$from = $this->createStub(ReflectionClass::class);
28+
$to = $this->createStub(ReflectionClass::class);
2929

3030
$to->method('getFileName')
3131
->willReturn('foo.php');

test/unit/DetectChanges/BCBreak/ClassBased/OpenClassChangedTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\Attributes\CoversClass;
88
use PHPUnit\Framework\MockObject\MockObject;
9+
use PHPUnit\Framework\MockObject\Stub;
910
use PHPUnit\Framework\TestCase;
1011
use Roave\BackwardCompatibility\Change;
1112
use Roave\BackwardCompatibility\Changes;
@@ -23,10 +24,10 @@ final class OpenClassChangedTest extends TestCase
2324

2425
private OpenClassChanged $openClassChanged;
2526

26-
/** @var ReflectionClass&MockObject */
27+
/** @var ReflectionClass&Stub */
2728
private ReflectionClass $fromClass;
2829

29-
/** @var ReflectionClass&MockObject */
30+
/** @var ReflectionClass&Stub */
3031
private ReflectionClass $toClass;
3132

3233
protected function setUp(): void
@@ -35,8 +36,8 @@ protected function setUp(): void
3536

3637
$this->check = $this->createMock(ClassBased::class);
3738
$this->openClassChanged = new OpenClassChanged($this->check);
38-
$this->fromClass = $this->createMock(ReflectionClass::class);
39-
$this->toClass = $this->createMock(ReflectionClass::class);
39+
$this->fromClass = $this->createStub(ReflectionClass::class);
40+
$this->toClass = $this->createStub(ReflectionClass::class);
4041
}
4142

4243
public function testWillCheckFinalClass(): void

test/unit/DetectChanges/BCBreak/ClassBased/SkipClassBasedErrorsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ protected function setUp(): void
3232

3333
public function testWillForwardChecks(): void
3434
{
35-
$fromClass = $this->createMock(ReflectionClass::class);
36-
$toClass = $this->createMock(ReflectionClass::class);
35+
$fromClass = $this->createStub(ReflectionClass::class);
36+
$toClass = $this->createStub(ReflectionClass::class);
3737
$expectedChanges = Changes::fromList(Change::added(
3838
uniqid('foo', true),
3939
true,
@@ -51,8 +51,8 @@ public function testWillForwardChecks(): void
5151

5252
public function testWillCollectFailures(): void
5353
{
54-
$fromClass = $this->createMock(ReflectionClass::class);
55-
$toClass = $this->createMock(ReflectionClass::class);
54+
$fromClass = $this->createStub(ReflectionClass::class);
55+
$toClass = $this->createStub(ReflectionClass::class);
5656
$exception = new Exception();
5757

5858
$this

test/unit/DetectChanges/BCBreak/ClassConstantBased/MultipleChecksOnAClassConstantTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public function testChecksAllGivenCheckers(): void
2525

2626
$multiCheck = new MultipleChecksOnAClassConstant($checker1, $checker2, $checker3);
2727

28-
$from = $this->createMock(ReflectionClassConstant::class);
29-
$to = $this->createMock(ReflectionClassConstant::class);
30-
$toClass = $this->createMock(ReflectionClass::class);
28+
$from = $this->createStub(ReflectionClassConstant::class);
29+
$to = $this->createStub(ReflectionClassConstant::class);
30+
$toClass = $this->createStub(ReflectionClass::class);
3131

3232
$to->method('getStartLine')
3333
->willReturn(10);

0 commit comments

Comments
 (0)