pvsaintpe / ltree-bundle

10 stars 5 forks source link

TestRepository is not created. createRepository() must be an instance of Doctrine\Common\Persistence\ObjectRepository #5

Open litvinab opened 2 years ago

litvinab commented 2 years ago

Hi, I'd like to use your bundle v2.0.1 in my project (Symfony 5.3). But now I'm struggling to make it work by provided example.

I made all things as described in example of code. I made entity and test repository. In entity class I changed namespace of repository that I've used. Your repository class is placed under Ltree/Entity/Repository.

Here is a part of the TestEntity class:

/**
 * Class TestEntity
 * @package LTree\Entity
 *
 * @Entity(repositoryClass="App\Infrastructure\Repository\TestRepository")
 * @LTreeEntity()
 */
class TestEntity implements LTreeEntityInterface
{ ...

TestRepository class is exactly the same as provided in the example, except using of my namespace

<?php

namespace App\Infrastructure\Repository;

use Doctrine\ORM\EntityManagerInterface;
use LTree\Repository\LTreeEntityRepository;
use App\Domain\Ltree\TestEntity;

/**
 * Class TestRepository
 *
 * @method TestEntity|null find($id, $lockMode = null, $lockVersion = null)
 * @method TestEntity|null findOneBy(array $criteria, array $orderBy = null)
 * @method TestEntity[]    findAll()
 * @method TestEntity[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 *
 * @package LTree\Entity
 */
class TestRepository extends LTreeEntityRepository
{
    /**
     * TestRepository constructor.
     * @param EntityManagerInterface $registry
     */
    public function __construct(EntityManagerInterface $registry)
    {
        parent::__construct($registry, $registry->getClassMetadata(TestEntity::class));
    }
}

My first test:

<?php
namespace App\Infrastructure\Repository;

use App\Infrastructure\TestCase\ContainerTestCase;
use App\Domain\Ltree\TestEntity;

/**
 * Class TestRepositoryTest
 * @package App\Infrastructure\Repository
 */
class TestRepositoryTest extends ContainerTestCase
{
    /**
     * @var TestRepository
     */
    private $repo;

    /**
     * @inheritdoc
     */
    protected function setUp(): void
    {
        parent::setUp();

        // it's the same as to call $this->getContainer()->get('doctrine')->getManager()->getRepository(...)
        $this->repo = $this->getRepository('Ltree:TestEntity');
    }

    /**
     * @return void
     */
    public function testGetSuccess()
    {
        $node = $this->repo->get(2);

        $this->assertInstanceOf(TestEntity::class, $node);
    }
}

Result of running of this test:

1) App\Infrastructure\Repository\TestRepositoryTest::testGetSuccess
TypeError: Return value of LTree\Repository\RepositoryFactory::createRepository() must be 
an instance of Doctrine\Common\Persistence\ObjectRepository, 
instance of App\Infrastructure\Repository\TestRepository returned

But TestRepository class extends LTreeEntityRepository one. So, it doesn't work even with own types.

BTW I'm using custom doctrine mappings as follows:

doctrine:
    ...
    orm:
    ...
        auto_mapping: false
        mappings:
            ...
            Ltree:
                type: annotation
                prefix: 'App\Domain\Ltree'
                dir: '%kernel.project_dir%/src/Domain/Ltree'
                is_bundle: false

Please help me with this. I'm able to provide any required details.

pvsaintpe commented 2 years ago

@litvinab

Gracias por su interés en mi desarrollo, en este momento estoy un poco ocupado, no hay oportunidad de trabajar activamente para solucionar este error.

La única forma de ayudarlo: puedo ver cómo usamos este paquete en nuestro proyecto Symfony, con capturas de pantalla de configuraciones, etc., y se lo enviamos.

¿Esto es correcto para usted?

En caso afirmativo, búsqueme en un "Telegrama" y recuérdeme a usted mismo a principios de la próxima semana, en algún lugar el lunes o martes.

tel.: +79219572126 Username: @pvsaintpe

vico-gs commented 2 years ago

@litvinab Hey, have the same issue on my project, have you find a way to solve it ?