How to create tests w/Doctrine entities without persisting them (how to set id)



PHP Snippet 1:

public function set($entity, $value, $propertyName = 'id')
{
    $class = new ReflectionClass($entity);
    $property = $class->getProperty($propertyName);
    $property->setAccessible(true);

    $property->setValue($entity, $value);
}

PHP Snippet 2:

# app/config/config_test
doctrine:
    dbal:
        driver: pdo_sqlite
        path: :memory:
        memory: true

PHP Snippet 3:

class TestableEntity extends \My\Namespace\Entity
{
    public function setId($id)
    {
       $this->id = $id;

       return $this;
    }
}

PHP Snippet 4:

$entity = new class extends \NameSpace\TheEntity {
    public function getId() {
        return 123;
    }
}