SupportFacadeTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use ArrayAccess;
  4. use Illuminate\Support\Facades\Facade;
  5. use Mockery as m;
  6. use Mockery\MockInterface;
  7. use PHPUnit\Framework\TestCase;
  8. use stdClass;
  9. class SupportFacadeTest extends TestCase
  10. {
  11. protected function setUp(): void
  12. {
  13. Facade::clearResolvedInstances();
  14. FacadeStub::setFacadeApplication(null);
  15. }
  16. protected function tearDown(): void
  17. {
  18. m::close();
  19. }
  20. public function testFacadeCallsUnderlyingApplication()
  21. {
  22. $app = new ApplicationStub;
  23. $app->setAttributes(['foo' => $mock = m::mock(stdClass::class)]);
  24. $mock->shouldReceive('bar')->once()->andReturn('baz');
  25. FacadeStub::setFacadeApplication($app);
  26. $this->assertSame('baz', FacadeStub::bar());
  27. }
  28. public function testShouldReceiveReturnsAMockeryMock()
  29. {
  30. $app = new ApplicationStub;
  31. $app->setAttributes(['foo' => new stdClass]);
  32. FacadeStub::setFacadeApplication($app);
  33. $this->assertInstanceOf(MockInterface::class, $mock = FacadeStub::shouldReceive('foo')->once()->with('bar')->andReturn('baz')->getMock());
  34. $this->assertSame('baz', $app['foo']->foo('bar'));
  35. }
  36. public function testSpyReturnsAMockerySpy()
  37. {
  38. $app = new ApplicationStub;
  39. $app->setAttributes(['foo' => new stdClass]);
  40. FacadeStub::setFacadeApplication($app);
  41. $this->assertInstanceOf(MockInterface::class, $spy = FacadeStub::spy());
  42. FacadeStub::foo();
  43. $spy->shouldHaveReceived('foo');
  44. }
  45. public function testShouldReceiveCanBeCalledTwice()
  46. {
  47. $app = new ApplicationStub;
  48. $app->setAttributes(['foo' => new stdClass]);
  49. FacadeStub::setFacadeApplication($app);
  50. $this->assertInstanceOf(MockInterface::class, $mock = FacadeStub::shouldReceive('foo')->once()->with('bar')->andReturn('baz')->getMock());
  51. $this->assertInstanceOf(MockInterface::class, $mock = FacadeStub::shouldReceive('foo2')->once()->with('bar2')->andReturn('baz2')->getMock());
  52. $this->assertSame('baz', $app['foo']->foo('bar'));
  53. $this->assertSame('baz2', $app['foo']->foo2('bar2'));
  54. }
  55. public function testCanBeMockedWithoutUnderlyingInstance()
  56. {
  57. FacadeStub::shouldReceive('foo')->once()->andReturn('bar');
  58. $this->assertSame('bar', FacadeStub::foo());
  59. }
  60. }
  61. class FacadeStub extends Facade
  62. {
  63. protected static function getFacadeAccessor()
  64. {
  65. return 'foo';
  66. }
  67. }
  68. class ApplicationStub implements ArrayAccess
  69. {
  70. protected $attributes = [];
  71. public function setAttributes($attributes)
  72. {
  73. $this->attributes = $attributes;
  74. }
  75. public function instance($key, $instance)
  76. {
  77. $this->attributes[$key] = $instance;
  78. }
  79. public function offsetExists($offset): bool
  80. {
  81. return isset($this->attributes[$offset]);
  82. }
  83. #[\ReturnTypeWillChange]
  84. public function offsetGet($key)
  85. {
  86. return $this->attributes[$key];
  87. }
  88. public function offsetSet($key, $value): void
  89. {
  90. $this->attributes[$key] = $value;
  91. }
  92. public function offsetUnset($key): void
  93. {
  94. unset($this->attributes[$key]);
  95. }
  96. }