承接 r83dev/test-entity 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

r83dev/test-entity

最新稳定版本:v1.2.0

Composer 安装命令:

composer require r83dev/test-entity

包简介

Entity properties trait to test properties of an entity.

README 文档

README

A helper class for testing, especially for unit tests to easily test properties of entities or data transfer objects.

Installation

Install via composer:

dcrr composer req --dev r83dev/test-entity

Usage

Use the EntityPropertiesTrait in your test and create getEntityClass and getEntityProperties methods:

Example entity MyEntity
class MyEntity
{
    private int $id;

    private string $name = '';

    private ?Collection $categories;

    private bool $active = false;

    public function __construct(
        private readonly string $key
    ) {
        $this->categories = new ArrayCollection();
    }

    public function getId(): int
    {
        if (!isset($this->id)) {
            throw new \LogicException('Entity not yet initialized or made persistent.');
        }

        return $this->id;
    }

    public function getKey(): string
    {
        return $this->key;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getCategories(): Collection
    {
        return $this->categories;
    }

    public function setCategories(Collection $categories): self
    {
        $this->categories = $categories;

        return $this;
    }

    public function isActive(): bool
    {
        return $this->active;
    }

    public function setActive(bool $active): self
    {
        $this->active = $active;

        return $this;
    }
}
/**
 * @extends R83Dev\TestEntity\EntityPropertiesTrait<MyEntity>
 */
class MyEntityTest {
    use R83Dev\TestEntity\EntityPropertiesTrait;
    
    /**
     * The entities fully qualified class name.
     */
    protected static function getEntityClass(): string
    {
        return MyEntity::class;
    }

    /**
     * Constructor arguments required to create the entity instance.
     * Required for entities with mandatory constructor arguments only
     */
    protected static function getEntityConstructorArguments(): array
    {
        return ['key'];
    }

    /**
     * All properties of the entity.
     * Properties can be private, protected, public, readonly, ...
     */
    protected static function getEntityProperties(): array
    {
        return [
            'id' => 5,
            'name' => 'My Entity',
            'categories' => new ArrayCollection(['category1']),
            'active' => true,
        ];
    }

    /**
     * Add your own custom tests to check special logic.
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function getIdThrowsException(): void
    {
        $this->expectException(\LogicException::class);
        $this->expectExceptionMessage('Entity not yet initialized or made persistent.');
        $this->getEntity()->getId();
    }

}

The Trait will now test the getters, setters and issers of the properties

统计信息

  • 总下载量: 1.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-03