I am trying to test a custom repository function but when my test call the function I have this error “BadMethodCallException: Undefined method “getByState”. I don’t understand why. The repository seems to be empty of my custom functions.
Maybe it’s because the Document entity is declared as an abstract class…
I also have tests on another repository which are perfectly running.
Entity/Document.php
#[Entity(repositoryClass: DocumentRepository::class)]
#[InheritanceType('JOINED')]
abstract class Document
{
...
}
tests/DocumentRepositoryTest.php
<?php
namespace AppTest;
use AppEntityDocument;
use AppRepositoryDocumentRepository;
use AppWorkflowStateDocumentWorkflowState;
use DoctrineORMEntityManager;
use SymfonyBundleFrameworkBundleTestKernelTestCase;
class DocumentRepositoryTest extends KernelTestCase
{
private ?EntityManager $entityManager;
protected function setUp(): void
{
$kernel = self::bootKernel();
$this->entityManager = $kernel->getContainer()
->get('doctrine')
->getManager();
}
protected function tearDown(): void
{
parent::tearDown();
$this->entityManager->close();
$this->entityManager = null;
}
public function testGetByState(): void
{
/** @var DocumentRepository */
$documentRepository = $this->entityManager
->getRepository(Document::class);
assert($documentRepository instanceof DocumentRepository);
$documents = $documentRepository->getByState(DocumentWorkflowState::BROUILLON);
foreach ($documents as $document) {
$this->assertEquals(DocumentWorkflowState::BROUILLON, $document->getState(), 'Erreur dans la fonction findByStatus');
}
}
}
Repository/DocumentRepository.php
<?php
namespace AppRepository;
use AppEntityDocument;
use DoctrineBundleDoctrineBundleRepositoryServiceEntityRepository;
/**
* @extends ServiceEntityRepository<Document>
*
* @method Document|null find($id, $lockMode = null, $lockVersion = null)
* @method Document|null findOneBy(array $criteria, array $orderBy = null)
* @method Document[] findAll()
* @method Document[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class DocumentRepository extends ServiceEntityRepository
{
/**
* @return Document[] Returns an array of Jury objects
*/
public function getByState(string $state): array
{
return $this->createQueryBuilder('d')
->leftJoin('d.statuses', 'statuses')
->where(
":val = (SELECT st.status FROM AppEntityStatus st
WHERE st.id = (SELECT MAX(s.id) FROM AppEntityStatus s
WHERE s.document = d
GROUP BY s.document)
)"
)
->setParameter('val', $state)
->orderBy('d.libelle_etape', 'ASC')
->getQuery()
->getResult();
}
}
Thanks for ideas !