InteractsWithContainer.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Testing\Concerns;
  12. use Closure;
  13. use Hyperf\Context\ApplicationContext;
  14. use Hyperf\Contract\ApplicationInterface;
  15. use Hyperf\Di\Container;
  16. use Hyperf\Di\Definition\DefinitionSourceFactory;
  17. use Mockery;
  18. use Mockery\MockInterface;
  19. use Psr\Container\ContainerInterface;
  20. trait InteractsWithContainer
  21. {
  22. /**
  23. * @var null|Container|ContainerInterface
  24. */
  25. protected ?ContainerInterface $container = null;
  26. /**
  27. * Register an instance of an object in the container.
  28. *
  29. * @param string $abstract
  30. * @param object $instance
  31. * @return object
  32. */
  33. protected function swap($abstract, $instance)
  34. {
  35. return $this->instance($abstract, $instance);
  36. }
  37. /**
  38. * Register an instance of an object in the container.
  39. *
  40. * @param string $abstract
  41. * @param object $instance
  42. * @return object
  43. */
  44. protected function instance($abstract, $instance)
  45. {
  46. /* @phpstan-ignore-next-line */
  47. $this->container->set($abstract, $instance);
  48. return $instance;
  49. }
  50. /**
  51. * Mock an instance of an object in the container.
  52. *
  53. * @param string $abstract
  54. * @return MockInterface
  55. */
  56. protected function mock($abstract, ?Closure $mock = null)
  57. {
  58. return $this->instance($abstract, Mockery::mock(...array_filter(func_get_args())));
  59. }
  60. /**
  61. * Mock a partial instance of an object in the container.
  62. *
  63. * @param string $abstract
  64. * @return MockInterface
  65. */
  66. protected function partialMock($abstract, ?Closure $mock = null)
  67. {
  68. return $this->instance($abstract, Mockery::mock(...array_filter(func_get_args()))->makePartial());
  69. }
  70. /**
  71. * Spy an instance of an object in the container.
  72. *
  73. * @param string $abstract
  74. * @return MockInterface
  75. */
  76. protected function spy($abstract, ?Closure $mock = null)
  77. {
  78. return $this->instance($abstract, Mockery::spy(...array_filter(func_get_args())));
  79. }
  80. protected function createContainer(): ContainerInterface
  81. {
  82. return new Container((new DefinitionSourceFactory())());
  83. }
  84. protected function getContainer(): ContainerInterface
  85. {
  86. return $this->container;
  87. }
  88. protected function flushContainer(): void
  89. {
  90. $this->container = null;
  91. }
  92. protected function setContainer(ContainerInterface $container): void
  93. {
  94. $this->container = $container;
  95. }
  96. protected function refreshContainer(): void
  97. {
  98. $this->container = ApplicationContext::setContainer($this->createContainer());
  99. $this->container->get(ApplicationInterface::class);
  100. }
  101. }