Skip to content

Commit 5fd4172

Browse files
authored
Merge pull request #51 from W0rma/abstract-classes
Improve error message if an abstract class is mocked in PHPUnit 12
2 parents 6ce4530 + d544612 commit 5fd4172

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/Stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,10 @@ private static function doGenerateMock($args, $isAbstract = false)
462462
$methodName = $isAbstract ? 'getMockForAbstractClass' : 'getMock';
463463
}
464464

465+
if ($isAbstract && version_compare(PHPUnitVersion::series(), '12', '>=')) {
466+
throw new RuntimeException('PHPUnit 12 or greater does not allow to mock abstract classes anymore');
467+
}
468+
465469
// PHPUnit 10.3 changed the namespace
466470
if (version_compare(PHPUnitVersion::series(), '10.3', '>=')) {
467471
$generatorClass = new Generator();

tests/StubTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ final class StubTest extends TestCase
1919

2020
public function setUp(): void
2121
{
22+
require_once $file = __DIR__. '/_data/DummyAbstractClass.php';
2223
require_once $file = __DIR__. '/_data/DummyOverloadableClass.php';
2324
require_once $file = __DIR__. '/_data/DummyClass.php';
2425
$this->dummy = new DummyClass(true);
@@ -404,6 +405,17 @@ public function testStubMakeEmptyInterface()
404405
$stub = Stub::makeEmpty(Countable::class, ['count' => 5]);
405406
$this->assertEquals(5, $stub->count());
406407
}
408+
409+
public function testStubMakeEmptyAbstractClass()
410+
{
411+
if (version_compare(PHPUnitVersion::id(), '12', '>=')) {
412+
$this->expectException(RuntimeException::class);
413+
$this->expectExceptionMessage('PHPUnit 12 or greater does not allow to mock abstract classes anymore');
414+
}
415+
416+
$stub = Stub::make('DummyAbstractClass');
417+
$this->assertInstanceOf('DummyAbstractClass', $stub);
418+
}
407419
}
408420

409421
class MyClassWithPrivateProperties

tests/_data/DummyAbstractClass.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
abstract class DummyAbstractClass
6+
{
7+
}

0 commit comments

Comments
 (0)