Is there a way to create a Doctrine 2 entity manager layout and provide it to the Symfony 2 PHPUnit test layout?

I was assigned to write unit tests for legacy code. My current test is on an object that accepts the Doctrine 2 Entity Manager parameter as a constructor. I need to be able to submit test data so that I can reliably test its methods.

So, ideally, I would like to be able to create an entity manager and then create a fake object and attach it to the mock entity manager and then use it in my tests. Is it possible? If so, how?

This project uses Symfony 2.4+, so I'm sure it adds wrinkles or two to the setting.

+4
source share
1 answer

Not sure if this will apply or not suit your specific Symfony setup needs (full stack?), But in previous projects where we used Symfony Components (and Doctrine), but not a full stack, it had more or less settings:

<?php

namespace Tests\SomeNameSpace;

use Doctrine\ORM\EntityRepository;
use SomeNameSpace\Repositories\SomeRepository;
use SomeNameSpace\SubjectUnderTesting;

class SubjectUnderTestingTest extends \PHPUnit_Framework_TestCase
{

    public function testSomething()
    {
        $queryExpectedValue = 'define expected return from query';

        /**
         * Infering that the the Subject Under Test is dealing with a single
         * repository.
         *
         * @var Doctrine\ORM\EntityRepository
         */
        $repository = $this
            ->getMockBuilder('Doctrine\ORM\EntityRepository')
            ->disableOriginalConstructor()
            ->setMethods(array('findOneByEmail'))
            ->getMock();

        $repository
            ->expects($this->once())
            ->method('findOneByEmail')
            ->will($this->returnValue($queryExpectedValue));

        /**
         * Now mock the EntityManager that will return the aforementioned
         * Repository. Extend to more repositories with a returnValueMap or
         * with clauses in case you need to handle more than one repository.
         *
         * @var Doctrine\ORM\EntityManager
         */
        $entityManager = $this
            ->getMockBuilder('Doctrine\ORM\EntityManager')
            ->setMethods(array('getRepository'))
            ->disableOriginalConstructor()
            ->getMock();

        $entityManager
            ->expects($this->once())
            ->method('getRepository')
            ->with('SomeNameSpace\Repositories\SomeRepository')
            ->will($this->returnValue($repository));

        /**
         * Let instantiate our Subject Under Test passing the mock as
         * required.
         *
         * This code above is tailored to a scenario where the SUT method
         * being tested will call something like this in the method to test:
         *
         * $queryResult = $entityManager
         *     ->getRepository('SomeNameSpace\Repositories\SomeRepository')
         *     ->findOneByEmail($someEmail);
         *
         * @var SubjectUnderTesting
         */
        $sut = new SubjectUnderTesting($entityManager);

        /**
         * Assertions time if they apply.
         */
        $expected = 'define what to expect';

        $this->assertEquals(
            $expected,
            $sut->callSomeMethodToTestThatDependsOnSomeQueryByEmail()
        );
    }
}
+17
source

Source: https://habr.com/ru/post/1546036/


All Articles