|
16 | 16 | use PHPUnit\Framework\Attributes\Medium;
|
17 | 17 | use PHPUnit\Framework\Attributes\TestDox;
|
18 | 18 | use PHPUnit\Framework\TestCase;
|
| 19 | +use PHPUnit\TestFixture\MockObject\AbstractClass; |
19 | 20 | use PHPUnit\TestFixture\MockObject\ExtendableClass;
|
20 | 21 | use PHPUnit\TestFixture\MockObject\InterfaceWithReturnTypeDeclaration;
|
| 22 | +use PHPUnit\TestFixture\MockObject\TraitWithConcreteAndAbstractMethod; |
21 | 23 |
|
22 | 24 | #[CoversClass(MockBuilder::class)]
|
23 | 25 | #[CoversClass(CannotUseAddMethodsException::class)]
|
@@ -62,4 +64,38 @@ public function testCannotCreateMockObjectForExtendableClassAddingMethodsToItTha
|
62 | 64 | ->addMethods(['doSomething'])
|
63 | 65 | ->getMock();
|
64 | 66 | }
|
| 67 | + |
| 68 | + #[TestDox('getMockForAbstractClass() can be used to create a mock object for an abstract class')] |
| 69 | + public function testCreatesMockObjectForAbstractClassAndAllowsConfigurationOfAbstractMethods(): void |
| 70 | + { |
| 71 | + $mock = $this->getMockBuilder(AbstractClass::class) |
| 72 | + ->getMockForAbstractClass(); |
| 73 | + |
| 74 | + $mock->expects($this->once())->method('doSomethingElse')->willReturn(true); |
| 75 | + |
| 76 | + $this->assertTrue($mock->doSomething()); |
| 77 | + } |
| 78 | + |
| 79 | + #[TestDox('getMockForTrait() can be used to create a mock object for a trait')] |
| 80 | + public function testCreatesMockObjectForTraitAndAllowsConfigurationOfMethods(): void |
| 81 | + { |
| 82 | + $mock = $this->getMockBuilder(TraitWithConcreteAndAbstractMethod::class) |
| 83 | + ->getMockForTrait(); |
| 84 | + |
| 85 | + $mock->method('abstractMethod')->willReturn(true); |
| 86 | + |
| 87 | + $this->assertTrue($mock->concreteMethod()); |
| 88 | + } |
| 89 | + |
| 90 | + #[TestDox('onlyMethods() can be used to configure which methods should be doubled')] |
| 91 | + public function testCreatesPartialMockObjectForExtendableClass(): void |
| 92 | + { |
| 93 | + $mock = $this->getMockBuilder(ExtendableClass::class) |
| 94 | + ->onlyMethods(['doSomethingElse']) |
| 95 | + ->getMock(); |
| 96 | + |
| 97 | + $mock->expects($this->once())->method('doSomethingElse')->willReturn(true); |
| 98 | + |
| 99 | + $this->assertTrue($mock->doSomething()); |
| 100 | + } |
65 | 101 | }
|
0 commit comments