Skip to content

Commit

Permalink
Improve Testing (#5885)
Browse files Browse the repository at this point in the history
* Improve Test

* feat: Add container to InteractsWithContainer trait

This commit adds a property `container` of type `ContainerInterface` to the `InteractsWithContainer` trait. It also removes a redundant annotation and an unnecessary `PHPStan` ignore statement. The `instance()` method now sets instances using the container object.

Co-authored-by: 李铭昕 <715557344@qq.com>
  • Loading branch information
huangdijia and limingxinleo committed Jun 28, 2023
1 parent 8ddecf6 commit b2fb337
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
28 changes: 23 additions & 5 deletions src/Concerns/InteractsWithContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

trait InteractsWithContainer
{
/**
* @var null|ContainerInterface|\Hyperf\Di\Container
*/
protected ?ContainerInterface $container = null;

/**
Expand Down Expand Up @@ -82,14 +85,29 @@ protected function spy($abstract, Closure $mock = null)
return $this->instance($abstract, Mockery::spy(...array_filter(func_get_args())));
}

protected function refreshContainer(): void
protected function createContainer(): ContainerInterface
{
$this->container = ApplicationContext::setContainer($this->createContainer());
$this->container->get(\Hyperf\Contract\ApplicationInterface::class);
return new Container((new DefinitionSourceFactory())());
}

protected function createContainer(): ContainerInterface
protected function getContainer(): ContainerInterface
{
return new Container((new DefinitionSourceFactory())());
return $this->container;
}

protected function flushContainer(): void
{
$this->container = null;
}

protected function setContainer(ContainerInterface $container): void
{
$this->container = $container;
}

protected function refreshContainer(): void
{
$this->container = ApplicationContext::setContainer($this->createContainer());
$this->container->get(\Hyperf\Contract\ApplicationInterface::class);
}
}
7 changes: 2 additions & 5 deletions src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase

protected function setUp(): void
{
/* @phpstan-ignore-next-line */
if (! $this->container) {
$this->refreshContainer();
}
$this->refreshContainer();
}

protected function tearDown(): void
{
$this->container = null;
$this->flushContainer();

try {
m::close();
Expand Down

0 comments on commit b2fb337

Please sign in to comment.