Skip to content
Advertisement

Symfony mocking repository for testing

I’m trying to mock a repository containing all modules, and it has a method called findAll() that returns all elements from that repository. I cant seem to be able to pull any data from the mocked repository. Any ideas how I could fix it?

Mocking repository:

$moduleRepository = $this->createMock(ModuleRepository::class);

Trying to use method from ModuleRepository class called findAll() that returns all elements from the repo in an array.

$moduleRepository->expects($this->any())
        ->method('findAll')
        ->willReturn([]);

Thats what I get when I print $moduleRepository:

Mock_ModuleRepository_f6bead1c Object
(
    [_entityName:protected] => 
    [_em:protected] => 
    [_class:protected] => 
    [__phpunit_originalObject:Mock_ModuleRepository_f6bead1c:private] => 
    [__phpunit_returnValueGeneration:Mock_ModuleRepository_f6bead1c:private] => 1
    [__phpunit_invocationMocker:Mock_ModuleRepository_f6bead1c:private] => PHPUnitFrameworkMockObjectInvocationHandler Object
        (
            [matchers:PHPUnitFrameworkMockObjectInvocationHandler:private] => Array
                (
                    [0] => PHPUnitFrameworkMockObjectMatcher Object
                        (
                            [invocationRule:PHPUnitFrameworkMockObjectMatcher:private] => PHPUnitFrameworkMockObjectRuleAnyInvokedCount Object
                                (
                                    [invocations:PHPUnitFrameworkMockObjectRuleInvocationOrder:private] => Array
                                        (
                                        )

                                )

                            [afterMatchBuilderId:PHPUnitFrameworkMockObjectMatcher:private] => 
                            [afterMatchBuilderIsInvoked:PHPUnitFrameworkMockObjectMatcher:private] => 
                            [methodNameRule:PHPUnitFrameworkMockObjectMatcher:private] => PHPUnitFrameworkMockObjectRuleMethodName Object
                                (
                                    [constraint:PHPUnitFrameworkMockObjectRuleMethodName:private] => PHPUnitFrameworkMockObjectMethodNameConstraint Object
                                        (
                                            [methodName:PHPUnitFrameworkMockObjectMethodNameConstraint:private] => findAll
                                            [exporter:PHPUnitFrameworkConstraintConstraint:private] => 
                                        )

                                )

                            [parametersRule:PHPUnitFrameworkMockObjectMatcher:private] => 
                            [stub:PHPUnitFrameworkMockObjectMatcher:private] => PHPUnitFrameworkMockObjectStubReturnStub Object
                                (
                                    [value:PHPUnitFrameworkMockObjectStubReturnStub:private] => Array
                                        (
                                        )

                                )

                        )

                )

            [matcherMap:PHPUnitFrameworkMockObjectInvocationHandler:private] => Array
                (
                )

            [configurableMethods:PHPUnitFrameworkMockObjectInvocationHandler:private] => Array
                (
                    [0] => PHPUnitFrameworkMockObjectConfigurableMethod Object
                        (
                            [name:PHPUnitFrameworkMockObjectConfigurableMethod:private] => findAllModuleRoutes
                            [returnType:PHPUnitFrameworkMockObjectConfigurableMethod:private] => SebastianBergmannTypeSimpleType Object
                                (
                                    [name:SebastianBergmannTypeSimpleType:private] => array
                                    [allowsNull:SebastianBergmannTypeSimpleType:private] => 
                                    [value:SebastianBergmannTypeSimpleType:private] => 
                                )

                        )

                    [1] => PHPUnitFrameworkMockObjectConfigurableMethod Object
                        (
                            [name:PHPUnitFrameworkMockObjectConfigurableMethod:private] => findAllQueryBuilderWithSearch
                            [returnType:PHPUnitFrameworkMockObjectConfigurableMethod:private] => SebastianBergmannTypeObjectType Object
                                (
                                    [className:SebastianBergmannTypeObjectType:private] => SebastianBergmannTypeTypeName Object
                                        (
                                            [namespaceName:SebastianBergmannTypeTypeName:private] => DoctrineORM
                                            [simpleName:SebastianBergmannTypeTypeName:private] => QueryBuilder
                                        )

                                    [allowsNull:SebastianBergmannTypeObjectType:private] => 
                                )

                        )

                    [2] => PHPUnitFrameworkMockObjectConfigurableMethod Object
                        (
                            [name:PHPUnitFrameworkMockObjectConfigurableMethod:private] => findListResults
                            [returnType:PHPUnitFrameworkMockObjectConfigurableMethod:private] => SebastianBergmannTypeSimpleType Object
                                (
                                    [name:SebastianBergmannTypeSimpleType:private] => array
                                    [allowsNull:SebastianBergmannTypeSimpleType:private] => 
                                    [value:SebastianBergmannTypeSimpleType:private] => 
                                )

                        )

                    [3] => PHPUnitFrameworkMockObjectConfigurableMethod Object
                        (
                            [name:PHPUnitFrameworkMockObjectConfigurableMethod:private] => createQueryBuilder
                            [returnType:PHPUnitFrameworkMockObjectConfigurableMethod:private] => SebastianBergmannTypeUnknownType Object
                                (
                                )

                        )

                    [4] => PHPUnitFrameworkMockObjectConfigurableMethod Object
                        (
                            [name:PHPUnitFrameworkMockObjectConfigurableMethod:private] => createResultSetMappingBuilder
                            [returnType:PHPUnitFrameworkMockObjectConfigurableMethod:private] => SebastianBergmannTypeUnknownType Object
                                (
                                )

                        )

                    [5] => PHPUnitFrameworkMockObjectConfigurableMethod Object
                        (
                            [name:PHPUnitFrameworkMockObjectConfigurableMethod:private] => createNamedQuery
                            [returnType:PHPUnitFrameworkMockObjectConfigurableMethod:private] => SebastianBergmannTypeUnknownType Object
                                (
                                )

                        )

                    [6] => PHPUnitFrameworkMockObjectConfigurableMethod Object
                        (
                            [name:PHPUnitFrameworkMockObjectConfigurableMethod:private] => createNativeNamedQuery
                            [returnType:PHPUnitFrameworkMockObjectConfigurableMethod:private] => SebastianBergmannTypeUnknownType Object
                                (
                                )

                        )

                    [7] => PHPUnitFrameworkMockObjectConfigurableMethod Object
                        (
                            [name:PHPUnitFrameworkMockObjectConfigurableMethod:private] => clear
                            [returnType:PHPUnitFrameworkMockObjectConfigurableMethod:private] => SebastianBergmannTypeUnknownType Object
                                (
                                )

                        )

                    [8] => PHPUnitFrameworkMockObjectConfigurableMethod Object
                        (
                            [name:PHPUnitFrameworkMockObjectConfigurableMethod:private] => find
                            [returnType:PHPUnitFrameworkMockObjectConfigurableMethod:private] => SebastianBergmannTypeUnknownType Object
                                (
                                )

                        )

                    [9] => PHPUnitFrameworkMockObjectConfigurableMethod Object
                        (
                            [name:PHPUnitFrameworkMockObjectConfigurableMethod:private] => findAll
                            [returnType:PHPUnitFrameworkMockObjectConfigurableMethod:private] => SebastianBergmannTypeUnknownType Object
                                (
                                )

                        )

                    [10] => PHPUnitFrameworkMockObjectConfigurableMethod Object
                        (
                            [name:PHPUnitFrameworkMockObjectConfigurableMethod:private] => findBy
                            [returnType:PHPUnitFrameworkMockObjectConfigurableMethod:private] => SebastianBergmannTypeUnknownType Object
                                (
                                )

                        )

                    [11] => PHPUnitFrameworkMockObjectConfigurableMethod Object
                        (
                            [name:PHPUnitFrameworkMockObjectConfigurableMethod:private] => findOneBy
                            [returnType:PHPUnitFrameworkMockObjectConfigurableMethod:private] => SebastianBergmannTypeUnknownType Object
                                (
                                )

                        )

                    [12] => PHPUnitFrameworkMockObjectConfigurableMethod Object
                        (
                            [name:PHPUnitFrameworkMockObjectConfigurableMethod:private] => count
                            [returnType:PHPUnitFrameworkMockObjectConfigurableMethod:private] => SebastianBergmannTypeUnknownType Object
                                (
                                )

                        )

                    [13] => PHPUnitFrameworkMockObjectConfigurableMethod Object
                        (
                            [name:PHPUnitFrameworkMockObjectConfigurableMethod:private] => __call
                            [returnType:PHPUnitFrameworkMockObjectConfigurableMethod:private] => SebastianBergmannTypeUnknownType Object
                                (
                                )

                        )

                    [14] => PHPUnitFrameworkMockObjectConfigurableMethod Object
                        (
                            [name:PHPUnitFrameworkMockObjectConfigurableMethod:private] => getClassName
                            [returnType:PHPUnitFrameworkMockObjectConfigurableMethod:private] => SebastianBergmannTypeUnknownType Object
                                (
                                )

                        )

                    [15] => PHPUnitFrameworkMockObjectConfigurableMethod Object
                        (
                            [name:PHPUnitFrameworkMockObjectConfigurableMethod:private] => matching
                            [returnType:PHPUnitFrameworkMockObjectConfigurableMethod:private] => SebastianBergmannTypeUnknownType Object
                                (
                                )

                        )

                )

            [returnValueGeneration:PHPUnitFrameworkMockObjectInvocationHandler:private] => 1
            [deferredError:PHPUnitFrameworkMockObjectInvocationHandler:private] => 
        )

)

I tried it like this and the method actually gets called, but the repo has no data for it to find.

$moduleRepository->expects($this->any())
        ->method('findAll')
        ->willReturn([]);
    $myDebugVar = $moduleRepository->findAll();
    fwrite(STDERR, print_r($myDebugVar, TRUE));

Output:

Array()

Advertisement

Answer

Your moduleRepository will always return empty array, because you said to do it.

here you said to return an empty array []:

->willReturn([]);

from method findAll:

->method('findAll')

There are some ways how to handle it, but for now, I recommend you to create a new database only for testing purposes, then create a new .env.test file which will extend the original .env file and use the exact repository instead of mock.

# .env.test.local
DATABASE_URL="mysql://USERNAME:PASSWORD@127.0.0.1:3306/DB_NAME?serverVersion=5.7"
namespace AppTestsRepository;
use DoctrineORMEntityManager;
use SymfonyBundleFrameworkBundleTestKernelTestCase;

final class SomeTest extends KernelTestCase
{
    private ?EntityManager $entityManager;

    protected function setUp(): void
    {
        $kernel = self::bootKernel();

        $this->entityManager = $kernel->getContainer()
            ->get('doctrine')
            ->getManager();
    }


    public function testSomething()
    {
        //given
        $moduleRepository = $this->entityManager->getRepository(ModuleRepository::class);

        //when
        $result = $moduleRepository->findAll(...);

        //then
        $this->assertEquals(...);
    }

    protected function tearDown(): void
    {
        parent::tearDown();

        // doing this is recommended to avoid memory leaks
        $this->entityManager->close();
        $this->entityManager = null;
    }
}

Be sure you will fill up a new database with the data. It’s a good idea to create fixtures for doing it automatically and clean up the database on tearDown method.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement