1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\Constraint\IsEqual;
use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
use PHPUnit\Framework\MockObject\IncompatibleReturnValueException;
use PHPUnit\Framework\MockObject\InvocationHandler;
use PHPUnit\Framework\MockObject\Matcher;
use PHPUnit\Framework\MockObject\MethodCannotBeConfiguredException;
use PHPUnit\Framework\MockObject\Stub\ReturnSelf;
use PHPUnit\Framework\MockObject\Stub\ReturnStub;
use PHPUnit\Framework\TestCase;
use PHPUnit\TestFixture\ClassWithAllPossibleReturnTypes;
use PHPUnit\TestFixture\MockObject\ClassWithImplicitProtocol;
/**
* @covers \PHPUnit\Framework\MockObject\Builder\InvocationMocker
* @small
*/
final class InvocationMockerTest extends TestCase
{
public function testWillReturnWithOneValue(): void
{
$mock = $this->getMockBuilder(stdClass::class)
->addMethods(['foo'])
->getMock();
$mock->method('foo')
->willReturn(1);
$this->assertEquals(1, $mock->foo());
}
public function testWillReturnWithMultipleValues(): void
{
$mock = $this->getMockBuilder(stdClass::class)
->addMethods(['foo'])
->getMock();
$mock->method('foo')
->willReturn(1, 2, 3);
$this->assertEquals(1, $mock->foo());
$this->assertEquals(2, $mock->foo());
$this->assertEquals(3, $mock->foo());
}
public function testWillReturnOnConsecutiveCalls(): void
{
$mock = $this->getMockBuilder(stdClass::class)
->addMethods(['foo'])
->getMock();
$mock->method('foo')
->willReturnOnConsecutiveCalls(1, 2, 3);
$this->assertEquals(1, $mock->foo());
$this->assertEquals(2, $mock->foo());
$this->assertEquals(3, $mock->foo());
}
public function testWillReturnByReference(): void
{
$mock = $this->getMockBuilder(stdClass::class)
->addMethods(['foo'])
->getMock();
$mock->method('foo')
->willReturnReference($value);
$this->assertNull($mock->foo());
$value = 'foo';
$this->assertSame('foo', $mock->foo());
$value = 'bar';
$this->assertSame('bar', $mock->foo());
}
public function testWillFailWhenTryingToPerformExpectationUnconfigurableMethod(): void
{
$matcherCollection = new InvocationHandler([], false);
$invocationMocker = new InvocationMocker(
$matcherCollection,
new Matcher($this->any())
);
$this->expectException(MethodCannotBeConfiguredException::class);
$invocationMocker->method('someMethod');
}
public function testWillReturnFailsWhenTryingToReturnSingleIncompatibleValue(): void
{
$mock = $this->getMockBuilder(ClassWithAllPossibleReturnTypes::class)
->getMock();
$invocationMocker = $mock->method('methodWithBoolReturnTypeDeclaration');
$this->expectException(IncompatibleReturnValueException::class);
$this->expectExceptionMessage('Method methodWithBoolReturnTypeDeclaration may not return value of type integer, its return declaration is "bool"');
$invocationMocker->willReturn(1);
}
public function testWillReturnFailsWhenTryingToReturnIncompatibleValueByConstraint(): void
{
$mock = $this->getMockBuilder(ClassWithAllPossibleReturnTypes::class)
->getMock();
$invocationMocker = $mock->method(new IsEqual('methodWithBoolReturnTypeDeclaration'));
$this->expectException(IncompatibleReturnValueException::class);
$this->expectExceptionMessage('Method methodWithBoolReturnTypeDeclaration may not return value of type integer, its return declaration is "bool"');
$invocationMocker->willReturn(1);
}
public function testWillReturnFailsWhenTryingToReturnAtLeastOneIncompatibleValue(): void
{
$mock = $this->getMockBuilder(ClassWithAllPossibleReturnTypes::class)
->getMock();
$invocationMocker = $mock->method('methodWithBoolReturnTypeDeclaration');
$this->expectException(IncompatibleReturnValueException::class);
$this->expectExceptionMessage('Method methodWithBoolReturnTypeDeclaration may not return value of type integer, its return declaration is "bool"');
$invocationMocker->willReturn(true, 1);
}
public function testWillReturnFailsWhenTryingToReturnSingleIncompatibleClass(): void
{
$mock = $this->getMockBuilder(ClassWithAllPossibleReturnTypes::class)
->getMock();
$invocationMocker = $mock->method('methodWithClassReturnTypeDeclaration');
$this->expectException(IncompatibleReturnValueException::class);
$this->expectExceptionMessage('Method methodWithClassReturnTypeDeclaration may not return value of type Foo, its return declaration is "stdClass"');
$invocationMocker->willReturn(new Foo());
}
public function testWillReturnAllowsMatchersForMultipleMethodsWithDifferentReturnTypes(): void
{
/** @var ClassWithAllPossibleReturnTypes|\PHPUnit\Framework\MockObject\MockObject $mock */
$mock = $this->getMockBuilder(ClassWithAllPossibleReturnTypes::class)
->getMock();
$invocationMocker = $mock->method(new \PHPUnit\Framework\Constraint\IsAnything());
$invocationMocker->willReturn(true, 1);
$this->assertEquals(true, $mock->methodWithBoolReturnTypeDeclaration());
$this->assertEquals(1, $mock->methodWithIntReturnTypeDeclaration());
}
public function testWillReturnValidType(): void
{
$mock = $this->getMockBuilder(ClassWithAllPossibleReturnTypes::class)
->getMock();
$mock->method('methodWithBoolReturnTypeDeclaration')
->willReturn(true);
$this->assertEquals(true, $mock->methodWithBoolReturnTypeDeclaration());
}
public function testWillReturnValidTypeForLowercaseCall(): void
{
$mock = $this->getMockBuilder(ClassWithAllPossibleReturnTypes::class)
->getMock();
$mock->method('methodWithBoolReturnTypeDeclaration')
->willReturn(true);
$this->assertEquals(true, $mock->methodwithboolreturntypedeclaration());
}
public function testWillReturnValidTypeForLowercaseMethod(): void
{
$mock = $this->getMockBuilder(ClassWithAllPossibleReturnTypes::class)
->getMock();
$mock->method('methodwithboolreturntypedeclaration')
->willReturn(true);
$this->assertEquals(true, $mock->methodWithBoolReturnTypeDeclaration());
}
/**
* @see https://github.com/sebastianbergmann/phpunit/issues/3602
*/
public function testWillReturnFailsWhenTryingToReturnValueFromVoidMethod(): void
{
/** @var ClassWithAllPossibleReturnTypes|\PHPUnit\Framework\MockObject\MockObject $out */
$out = $this->createMock(ClassWithAllPossibleReturnTypes::class);
$method = $out->method('methodWithVoidReturnTypeDeclaration');
$this->expectException(IncompatibleReturnValueException::class);
$this->expectExceptionMessage('Method methodWithVoidReturnTypeDeclaration may not return value of type boolean, its return declaration is "void"');
$method->willReturn(true);
}
public function testExpectationsAreEnabledByPreviousMethodCallWhenChainedWithAfter(): void
{
$mock = $this->createMock(ClassWithImplicitProtocol::class);
$mock->expects($this->once())
->method('firstCall')
->id($fristCallId = 'first-call-id');
$mock->expects($this->once())
->method('secondCall')
->after($fristCallId);
$mock->firstCall();
$mock->secondCall();
}
public function testExpectationsAreNotTriggeredUntilPreviousMethodWasCalled(): void
{
$mock = $this->createMock(ClassWithImplicitProtocol::class);
$mock->expects($this->once())
->method('firstCall')
->id($firstCallId = 'first-call-id');
$mock->expects($this->once())
->method('secondCall')
->after($firstCallId);
$mock->secondCall();
$mock->firstCall();
$mock->secondCall();
}
public function testWillReturnAlreadyInstantiatedStubs(): void
{
$mock = $this->getMockBuilder(stdClass::class)
->addMethods(['foo', 'bar'])
->getMock();
$mock->method('foo')
->willReturn(new ReturnStub('foo'));
$mock->method('bar')
->willReturn(new ReturnSelf());
$this->assertSame('foo', $mock->foo());
$this->assertSame($mock, $mock->bar());
}
}
|