Skip to content

Commit

Permalink
Closes #5311
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jul 19, 2024
1 parent 6e69c0b commit 6eb5e38
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 74 deletions.
68 changes: 13 additions & 55 deletions src/Framework/MockObject/MockBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use function debug_backtrace;
use PHPUnit\Event\Facade as EventFacade;
use PHPUnit\Framework\InvalidArgumentException;
use PHPUnit\Framework\MockObject\Generator\CannotUseAddMethodsException;
use PHPUnit\Framework\MockObject\Generator\ClassIsEnumerationException;
use PHPUnit\Framework\MockObject\Generator\ClassIsFinalException;
use PHPUnit\Framework\MockObject\Generator\DuplicateMethodException;
Expand All @@ -39,33 +38,28 @@ final class MockBuilder
private readonly TestCase $testCase;

/**
* @var class-string|trait-string
* @psalm-var class-string|trait-string
*/
private readonly string $type;

/**
* @var list<non-empty-string>
* @psalm-var list<non-empty-string>
*/
private array $methods = [];
private bool $emptyMethodsArray = false;

/**
* @var ?class-string
* @psalm-var ?class-string
*/
private ?string $mockClassName = null;

/**
* @var array<mixed>
*/
private array $constructorArgs = [];
private bool $originalConstructor = true;
private bool $originalClone = true;
private bool $autoload = true;
private bool $cloneArguments = false;
private bool $callOriginalMethods = false;
private ?object $proxyTarget = null;
private bool $allowMockingUnknownTypes = true;
private bool $returnValueGeneration = true;
private ?string $mockClassName = null;
private array $constructorArgs = [];
private bool $originalConstructor = true;
private bool $originalClone = true;
private bool $autoload = true;
private bool $cloneArguments = false;
private bool $callOriginalMethods = false;
private ?object $proxyTarget = null;
private bool $returnValueGeneration = true;
private readonly Generator $generator;

/**
Expand Down Expand Up @@ -109,7 +103,7 @@ public function getMock(): MockObject
$this->cloneArguments,
$this->callOriginalMethods,
$this->proxyTarget,
$this->allowMockingUnknownTypes,
false,
$this->returnValueGeneration,
);

Expand Down Expand Up @@ -320,42 +314,6 @@ public function enableArgumentCloning(): self
return $this;
}

/**
* @return $this
*
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/5308
*/
public function allowMockingUnknownTypes(): self
{
EventFacade::emitter()->testTriggeredPhpunitDeprecation(
$this->testCase->valueObjectForEvents(),
'MockBuilder::allowMockingUnknownTypes() is deprecated and will be removed in PHPUnit 12 without replacement.',
);

$this->allowMockingUnknownTypes = true;

return $this;
}

/**
* @return $this
*
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/5308
*/
public function disallowMockingUnknownTypes(): self
{
if (!$this->calledFromTestCase()) {
EventFacade::emitter()->testTriggeredPhpunitDeprecation(
$this->testCase->valueObjectForEvents(),
'MockBuilder::disallowMockingUnknownTypes() is deprecated and will be removed in PHPUnit 12 without replacement.',
);
}

$this->allowMockingUnknownTypes = false;

return $this;
}

/**
* @return $this
*/
Expand Down
1 change: 0 additions & 1 deletion src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,6 @@ final protected function createPartialMock(string $originalClassName, array $met
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->onlyMethods($methods);

if (!self::generateReturnValuesForTestDoubles()) {
Expand Down
18 changes: 0 additions & 18 deletions tests/unit/Framework/MockObject/Creation/MockBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,6 @@ public function testCreatesPartialMockObjectForExtendableClass(): void
$this->assertTrue($double->doSomething());
}

#[IgnorePhpunitDeprecations]
#[TestDox('allowMockingUnknownTypes() can be used to allow mocking of unknown types')]
public function testCreatesMockObjectForUnknownType(): void
{
$type = 'Type_' . substr(md5((string) mt_rand()), 0, 8);

assert(!class_exists($type) && !interface_exists($type) && !trait_exists($type));

$double = $this->getMockBuilder($type)
->allowMockingUnknownTypes()
->getMock();

$this->assertInstanceOf($type, $double);
$this->assertInstanceOf(MockObject::class, $double);
}

#[IgnorePhpunitDeprecations]
public function testDefaultBehaviourCanBeConfiguredExplicitly(): void
{
Expand All @@ -98,8 +82,6 @@ public function testDefaultBehaviourCanBeConfiguredExplicitly(): void
->enableOriginalClone()
->enableAutoload()
->enableArgumentCloning()
->disableProxyingToOriginalMethods()
->allowMockingUnknownTypes()
->enableAutoReturnValueGeneration()
->getMock();

Expand Down

0 comments on commit 6eb5e38

Please sign in to comment.