Chris@14
|
1 <?php
|
Chris@14
|
2 /*
|
Chris@14
|
3 * This file is part of the phpunit-mock-objects package.
|
Chris@14
|
4 *
|
Chris@14
|
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
|
Chris@14
|
6 *
|
Chris@14
|
7 * For the full copyright and license information, please view the LICENSE
|
Chris@14
|
8 * file that was distributed with this source code.
|
Chris@14
|
9 */
|
Chris@14
|
10
|
Chris@14
|
11 use PHPUnit\Framework\TestCase;
|
Chris@14
|
12
|
Chris@14
|
13 class InvocationMockerTest extends TestCase
|
Chris@14
|
14 {
|
Chris@14
|
15 public function testWillReturnWithOneValue()
|
Chris@14
|
16 {
|
Chris@14
|
17 $mock = $this->getMockBuilder(stdClass::class)
|
Chris@14
|
18 ->setMethods(['foo'])
|
Chris@14
|
19 ->getMock();
|
Chris@14
|
20
|
Chris@14
|
21 $mock->expects($this->any())
|
Chris@14
|
22 ->method('foo')
|
Chris@14
|
23 ->willReturn(1);
|
Chris@14
|
24
|
Chris@14
|
25 $this->assertEquals(1, $mock->foo());
|
Chris@14
|
26 }
|
Chris@14
|
27
|
Chris@14
|
28 public function testWillReturnWithMultipleValues()
|
Chris@14
|
29 {
|
Chris@14
|
30 $mock = $this->getMockBuilder(stdClass::class)
|
Chris@14
|
31 ->setMethods(['foo'])
|
Chris@14
|
32 ->getMock();
|
Chris@14
|
33
|
Chris@14
|
34 $mock->expects($this->any())
|
Chris@14
|
35 ->method('foo')
|
Chris@14
|
36 ->willReturn(1, 2, 3);
|
Chris@14
|
37
|
Chris@14
|
38 $this->assertEquals(1, $mock->foo());
|
Chris@14
|
39 $this->assertEquals(2, $mock->foo());
|
Chris@14
|
40 $this->assertEquals(3, $mock->foo());
|
Chris@14
|
41 }
|
Chris@14
|
42
|
Chris@14
|
43 public function testWillReturnOnConsecutiveCalls()
|
Chris@14
|
44 {
|
Chris@14
|
45 $mock = $this->getMockBuilder(stdClass::class)
|
Chris@14
|
46 ->setMethods(['foo'])
|
Chris@14
|
47 ->getMock();
|
Chris@14
|
48
|
Chris@14
|
49 $mock->expects($this->any())
|
Chris@14
|
50 ->method('foo')
|
Chris@14
|
51 ->willReturnOnConsecutiveCalls(1, 2, 3);
|
Chris@14
|
52
|
Chris@14
|
53 $this->assertEquals(1, $mock->foo());
|
Chris@14
|
54 $this->assertEquals(2, $mock->foo());
|
Chris@14
|
55 $this->assertEquals(3, $mock->foo());
|
Chris@14
|
56 }
|
Chris@14
|
57
|
Chris@14
|
58 public function testWillReturnByReference()
|
Chris@14
|
59 {
|
Chris@14
|
60 $mock = $this->getMockBuilder(stdClass::class)
|
Chris@14
|
61 ->setMethods(['foo'])
|
Chris@14
|
62 ->getMock();
|
Chris@14
|
63
|
Chris@14
|
64 $mock->expects($this->any())
|
Chris@14
|
65 ->method('foo')
|
Chris@14
|
66 ->willReturnReference($value);
|
Chris@14
|
67
|
Chris@14
|
68 $this->assertSame(null, $mock->foo());
|
Chris@14
|
69 $value = 'foo';
|
Chris@14
|
70 $this->assertSame('foo', $mock->foo());
|
Chris@14
|
71 $value = 'bar';
|
Chris@14
|
72 $this->assertSame('bar', $mock->foo());
|
Chris@14
|
73 }
|
Chris@14
|
74 }
|