Skip to content

Commit 118f599

Browse files
Merge branch '13.0'
* 13.0: Add tests
2 parents 65161ba + 6a58d0e commit 118f599

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
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+
interface InterfaceWithMethodNamedMethod
13+
{
14+
public function method(): bool;
15+
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
use PHPUnit\Framework\Attributes\TestDox;
1515
use PHPUnit\Framework\MockObject\Generator\ClassIsEnumerationException;
1616
use PHPUnit\Framework\MockObject\Generator\ClassIsFinalException;
17+
use PHPUnit\Framework\MockObject\Generator\Generator;
18+
use PHPUnit\Framework\MockObject\Generator\InvalidMethodNameException;
19+
use PHPUnit\Framework\MockObject\Generator\MethodNamedMethodException;
1720
use PHPUnit\Framework\MockObject\Generator\UnknownTypeException;
1821
use PHPUnit\Framework\TestCase;
1922
use PHPUnit\TestFixture\MockObject\AnInterface;
2023
use PHPUnit\TestFixture\MockObject\Enumeration;
2124
use PHPUnit\TestFixture\MockObject\ExtendableClass;
2225
use PHPUnit\TestFixture\MockObject\ExtendableReadonlyClass;
2326
use PHPUnit\TestFixture\MockObject\FinalClass;
27+
use PHPUnit\TestFixture\MockObject\InterfaceWithMethodNamedMethod;
2428

2529
#[Group('test-doubles')]
2630
#[Group('test-doubles/creation')]
@@ -83,4 +87,24 @@ public function testCannotCreateMockObjectForUnknownType(): void
8387

8488
$this->createMock('this\does\not\exist');
8589
}
90+
91+
public function testCannotCreateMockObjectWithInvalidMethodName(): void
92+
{
93+
$this->expectException(InvalidMethodNameException::class);
94+
$this->expectExceptionMessage('Cannot double method with invalid name "0"');
95+
96+
(new Generator)->testDouble(
97+
AnInterface::class,
98+
true,
99+
['0'],
100+
);
101+
}
102+
103+
public function testCannotCreateMockObjectForInterfaceWithMethodNamedMethod(): void
104+
{
105+
$this->expectException(MethodNamedMethodException::class);
106+
$this->expectExceptionMessage('Doubling interfaces (or classes) that have a method named "method" is not supported.');
107+
108+
$this->createMock(InterfaceWithMethodNamedMethod::class);
109+
}
86110
}

tests/unit/Framework/MockObject/MockObjectTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,40 @@ public function testSealedPartialMockObjectOnlyAffectsMockedMethods(): void
778778
$this->assertFalse($double->doSomething());
779779
}
780780

781+
public function testMethodNameMustBeConfiguredBeforeMethodParametersCanBeConfigured(): void
782+
{
783+
$double = $this->createMock(InterfaceWithReturnTypeDeclaration::class);
784+
785+
try {
786+
$double->expects($this->once())->with(1);
787+
} catch (MethodNameNotConfiguredException $e) {
788+
$this->assertSame('Method name is not configured', $e->getMessage());
789+
790+
return;
791+
} finally {
792+
$this->resetMockObjects();
793+
}
794+
795+
$this->fail();
796+
}
797+
798+
public function testMethodParametersCanOnlyBeConfiguredOnce(): void
799+
{
800+
$double = $this->createMock(InterfaceWithReturnTypeDeclaration::class);
801+
802+
try {
803+
$double->expects($this->once())->method('doSomethingElse')->with(1)->with(2);
804+
} catch (MethodParametersAlreadyConfiguredException $e) {
805+
$this->assertSame('Method parameters already configured', $e->getMessage());
806+
807+
return;
808+
} finally {
809+
$this->resetMockObjects();
810+
}
811+
812+
$this->fail();
813+
}
814+
781815
/**
782816
* @param class-string $type
783817
*/

tests/unit/Framework/MockObject/TestDoubleTestCase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
use Exception;
1313
use PHPUnit\Framework\Attributes\Group;
1414
use PHPUnit\Framework\Attributes\TestDox;
15+
use PHPUnit\Framework\MockObject\Generator\MethodNamedMethodException;
1516
use PHPUnit\Framework\MockObject\Runtime\PropertyHook;
1617
use PHPUnit\Framework\TestCase;
1718
use PHPUnit\TestFixture\MockObject\ExtendableClassCallingMethodInDestructor;
1819
use PHPUnit\TestFixture\MockObject\ExtendableClassWithCloneMethod;
1920
use PHPUnit\TestFixture\MockObject\ExtendableClassWithPropertyWithGetHook;
2021
use PHPUnit\TestFixture\MockObject\ExtendableReadonlyClassWithCloneMethod;
22+
use PHPUnit\TestFixture\MockObject\InterfaceWithMethodNamedMethod;
2123
use PHPUnit\TestFixture\MockObject\InterfaceWithMethodThatHasDefaultParameterValues;
2224
use PHPUnit\TestFixture\MockObject\InterfaceWithNeverReturningMethod;
2325
use PHPUnit\TestFixture\MockObject\InterfaceWithPropertyWithGetHook;
@@ -344,6 +346,15 @@ public function testClonedSealedTestDoubleRemainsSealed(): void
344346
$clone->method('doSomethingElse');
345347
}
346348

349+
#[TestDox('Cannot create test double for an interface that has a method named "method"')]
350+
public function testCannotCreateTestDoubleForInterfaceWithMethodNamedMethod(): void
351+
{
352+
$this->expectException(MethodNamedMethodException::class);
353+
$this->expectExceptionMessage('Doubling interfaces (or classes) that have a method named "method" is not supported.');
354+
355+
$this->createTestDouble(InterfaceWithMethodNamedMethod::class);
356+
}
357+
347358
/**
348359
* @template RealInstanceType of object
349360
*

0 commit comments

Comments
 (0)