InvocationMockerTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /*
  3. * This file is part of the phpunit-mock-objects package.
  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. use PHPUnit\Framework\TestCase;
  11. class InvocationMockerTest extends TestCase
  12. {
  13. public function testWillReturnWithOneValue()
  14. {
  15. $mock = $this->getMockBuilder(stdClass::class)
  16. ->setMethods(['foo'])
  17. ->getMock();
  18. $mock->expects($this->any())
  19. ->method('foo')
  20. ->willReturn(1);
  21. $this->assertEquals(1, $mock->foo());
  22. }
  23. public function testWillReturnWithMultipleValues()
  24. {
  25. $mock = $this->getMockBuilder(stdClass::class)
  26. ->setMethods(['foo'])
  27. ->getMock();
  28. $mock->expects($this->any())
  29. ->method('foo')
  30. ->willReturn(1, 2, 3);
  31. $this->assertEquals(1, $mock->foo());
  32. $this->assertEquals(2, $mock->foo());
  33. $this->assertEquals(3, $mock->foo());
  34. }
  35. public function testWillReturnOnConsecutiveCalls()
  36. {
  37. $mock = $this->getMockBuilder(stdClass::class)
  38. ->setMethods(['foo'])
  39. ->getMock();
  40. $mock->expects($this->any())
  41. ->method('foo')
  42. ->willReturnOnConsecutiveCalls(1, 2, 3);
  43. $this->assertEquals(1, $mock->foo());
  44. $this->assertEquals(2, $mock->foo());
  45. $this->assertEquals(3, $mock->foo());
  46. }
  47. public function testWillReturnByReference()
  48. {
  49. $mock = $this->getMockBuilder(stdClass::class)
  50. ->setMethods(['foo'])
  51. ->getMock();
  52. $mock->expects($this->any())
  53. ->method('foo')
  54. ->willReturnReference($value);
  55. $this->assertSame(null, $mock->foo());
  56. $value = 'foo';
  57. $this->assertSame('foo', $mock->foo());
  58. $value = 'bar';
  59. $this->assertSame('bar', $mock->foo());
  60. }
  61. }