Skip to content

Commit e5b3c30

Browse files
sbuerksebastianbergmann
authored andcommitted
[TASK] Provide a test to demonstrate a regresision
This change adds a fixture class and a MockBuilder test to showcase a regression introduced with version 11.2.0. Given is a class, where one method is mocked using the `onlyMethods()` option to check the method gets called. The method is called in the class constructor and also in other public method. Providing a test mocking that method this breaks since 11.2.0 - albeit having the count in previous version only working for calls to the mock method AFTER the constructor call. Following exception thrown is not expectd: Error: Typed property MockObject_ClassCallingMethodInConstructor_b5ada611::$__phpunit_state must not be accessed before initialization Related: #5857
1 parent deccf9e commit e5b3c30

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture\MockObject;
11+
12+
class ClassCallingMethodInConstructor
13+
{
14+
public function __construct()
15+
{
16+
$this->reset();
17+
}
18+
19+
public function reset(): void
20+
{
21+
}
22+
23+
public function second(): void
24+
{
25+
$this->reset();
26+
}
27+
}

tests/unit/Framework/MockObject/Creation/MockBuilderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace PHPUnit\Framework\MockObject;
1111

12+
use PHPUnit\TestFixture\MockObject\ClassCallingMethodInConstructor;
1213
use function assert;
1314
use function class_exists;
1415
use function interface_exists;
@@ -165,4 +166,15 @@ public function testCreatesMockObjectForUnknownType(): void
165166
$this->assertInstanceOf(MockObject::class, $double);
166167

167168
}
169+
170+
#[TestDox('onlyMethods() mocked methods can be called within the original constructor')]
171+
public function testOnlyMethodCalledInConstructorWorks(): void
172+
{
173+
$testClassMock = $this->getMockBuilder(ClassCallingMethodInConstructor::class)
174+
->onlyMethods(['reset'])
175+
->getMock();
176+
177+
$testClassMock->expects($this::once())->method('reset');
178+
$testClassMock->second();
179+
}
168180
}

0 commit comments

Comments
 (0)