PHP code example of r83dev / test-entity

1. Go to this page and download the library: Download r83dev/test-entity library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

r83dev / test-entity example snippets


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 operties(): 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();
    }

}