Skip to content

Commit

Permalink
use $this->createStub() to create stub instance of specified reposito…
Browse files Browse the repository at this point in the history
…ry interface
  • Loading branch information
SparkLee committed Feb 27, 2023
1 parent 1b761ec commit 062aea4
Showing 1 changed file with 17 additions and 41 deletions.
58 changes: 17 additions & 41 deletions tests/Unit/Domain/OrgMng/Org/OrgBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
use App\Domain\OrgMng\Org\DTO\OrgDomainDTO;
use App\Domain\OrgMng\Org\OrgBuilder;
use App\Domain\OrgMng\OrgType\OrgTypeRepository;
use App\Domain\OrgMng\OrgType\OrgTypeStatus;
use App\Domain\TenantMng\Tenant;
use App\Domain\TenantMng\TenantRepository;
use App\Domain\TenantMng\TenantStatus;
use PHPUnit\Framework\TestCase;

class OrgBuilderTest extends TestCase
Expand All @@ -25,11 +22,20 @@ protected function setUp(): void

/**
* @return void
*
* 注意:$this->createStub() 的返回值要先赋值给一个局部变量,比如 $stub, 然后再使用 $stub->method()->willReturn(),否则返回的类型将不是预期的类型,会报类似这样的错误:
* "Argument #1 ($tenantRepository) must be of type App\Domain\TenantMng\TenantRepository, PHPUnit\Framework\MockObject\Builder\InvocationMocker given"
* @see https://github.com/sebastianbergmann/phpunit/issues/3706#issuecomment-513094256
*/
public function setUpRepositoryStubs(): void
{
app()->instance(TenantRepository::class, new TenantRepositoryStub());
app()->instance(OrgTypeRepository::class, new OrgTypeRepositoryStub());
$tenantRepositoryStub = $this->createStub(TenantRepository::class);
$tenantRepositoryStub->method('existsByIdAndStatus')->willReturn(true);
app()->instance(TenantRepository::class, $tenantRepositoryStub);

$orgTypeRepositoryStub = $this->createStub(OrgTypeRepository::class);
$orgTypeRepositoryStub->method('existsByCodeAndStatus')->willReturn(true);
app()->instance(OrgTypeRepository::class, $orgTypeRepositoryStub);
}

public function setUpValidOrgDomainDTO(): void
Expand Down Expand Up @@ -64,8 +70,9 @@ public function test_org_type_should_be_valid()
$this->expectException(BusinessException::class);
$this->expectExceptionMessage('[test]不是有效的组织类别代码!');

app()->instance(OrgTypeRepository::class, (new OrgTypeRepositoryStub())
->setExistsByCodeAndStatus(false));
$orgTypeRepositoryStub = $this->createStub(OrgTypeRepository::class);
$orgTypeRepositoryStub->method('existsByCodeAndStatus')->willReturn(false);
app()->instance(OrgTypeRepository::class, $orgTypeRepositoryStub);

$this->build();
}
Expand Down Expand Up @@ -93,8 +100,9 @@ public function test_tenant_should_be_effective()
$this->expectException(BusinessException::class);
$this->expectExceptionMessage('id为[1]的租户不是有效租户!');

app()->instance(TenantRepository::class, (new TenantRepositoryStub())
->setExistsByIdAndStatus(false));
$tenantRepositoryStub = $this->createStub(TenantRepository::class);
$tenantRepositoryStub->method('existsByIdAndStatus')->willReturn(false);
app()->instance(TenantRepository::class, $tenantRepositoryStub);

$this->build();
}
Expand All @@ -111,35 +119,3 @@ public function build(): void
->build();
}
}

class TenantRepositoryStub implements TenantRepository
{
private bool $existsByIdAndStatus = true;

public function existsByIdAndStatus(int $tenantId, TenantStatus $tenantStatus): bool
{
return $this->existsByIdAndStatus;
}

public function setExistsByIdAndStatus(bool $existsByIdAndStatus): self
{
$this->existsByIdAndStatus = $existsByIdAndStatus;
return $this;
}
}

class OrgTypeRepositoryStub implements OrgTypeRepository
{
private bool $existsByCodeAndStatus = true;

public function existsByCodeAndStatus(Tenant|int $tenant, string $code, int|OrgTypeStatus $orgTypeStatus): bool
{
return $this->existsByCodeAndStatus;
}

public function setExistsByCodeAndStatus(bool $existsByCodeAndStatus): self
{
$this->existsByCodeAndStatus = $existsByCodeAndStatus;
return $this;
}
}

0 comments on commit 062aea4

Please sign in to comment.