Skip to content

Commit ba52e57

Browse files
Add test for setConstructorArgs()
1 parent d3706a9 commit ba52e57

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 ExtendableClassWithConstructorArguments
13+
{
14+
private string $value;
15+
16+
public function __construct(string $value)
17+
{
18+
$this->value = $value;
19+
}
20+
21+
public function value(): string
22+
{
23+
return $this->value;
24+
}
25+
}

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

+15
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use PHPUnit\TestFixture\MockObject\AbstractClass;
3030
use PHPUnit\TestFixture\MockObject\ExtendableClass;
3131
use PHPUnit\TestFixture\MockObject\ExtendableClassCallingMethodInConstructor;
32+
use PHPUnit\TestFixture\MockObject\ExtendableClassWithConstructorArguments;
3233
use PHPUnit\TestFixture\MockObject\InterfaceWithReturnTypeDeclaration;
3334
use PHPUnit\TestFixture\MockObject\TraitWithConcreteAndAbstractMethod;
3435

@@ -65,6 +66,20 @@ public function testCannotCreateMockObjectWithSpecifiedClassNameWhenClassWithTha
6566
->getMock();
6667
}
6768

69+
#[TestDox('setConstructorArgs() can be used to configure constructor arguments for a partially mocked class')]
70+
public function testConstructorArgumentsCanBeConfiguredForPartiallyMockedClass(): void
71+
{
72+
$value = 'string';
73+
74+
$double = $this->getMockBuilder(ExtendableClassWithConstructorArguments::class)
75+
->enableOriginalConstructor()
76+
->setConstructorArgs([$value])
77+
->onlyMethods([])
78+
->getMock();
79+
80+
$this->assertSame($value, $double->value());
81+
}
82+
6883
#[IgnorePhpunitDeprecations]
6984
#[TestDox('addMethods() can be used to configure an additional method for the mock object class when the original class does not have a method of the same name')]
7085
public function testCanCreateMockObjectForExtendableClassWhileAddingMethodsToIt(): void

0 commit comments

Comments
 (0)