forked from sebastianbergmann/phpunit
/
InvocationMockerTest.php
89 lines (74 loc) · 2.61 KB
/
InvocationMockerTest.php
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
<?php
/*
* 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\MockObject\Stub\MatcherCollection;
use PHPUnit\Framework\TestCase;
class InvocationMockerTest extends TestCase
{
public function testWillReturnWithOneValue(): void
{
$mock = $this->getMockBuilder(stdClass::class)
->setMethods(['foo'])
->getMock();
$mock->expects($this->any())
->method('foo')
->willReturn(1);
$this->assertEquals(1, $mock->foo());
}
public function testWillReturnWithMultipleValues(): void
{
$mock = $this->getMockBuilder(stdClass::class)
->setMethods(['foo'])
->getMock();
$mock->expects($this->any())
->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)
->setMethods(['foo'])
->getMock();
$mock->expects($this->any())
->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)
->setMethods(['foo'])
->getMock();
$mock->expects($this->any())
->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()
{
/** @var MatcherCollection|\PHPUnit\Framework\MockObject\MockObject $matcherCollection */
$matcherCollection = $this->createMock(MatcherCollection::class);
$invocationMocker = new \PHPUnit\Framework\MockObject\Builder\InvocationMocker(
$matcherCollection,
$this->any(),
[]
);
$this->expectException(RuntimeException::class);
$invocationMocker->method('someMethod');
}
}