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';
$repository = $this
->getMockBuilder('Doctrine\ORM\EntityRepository')
->disableOriginalConstructor()
->setMethods(array('findOneByEmail'))
->getMock();
$repository
->expects($this->once())
->method('findOneByEmail')
->will($this->returnValue($queryExpectedValue));
$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));
$sut = new SubjectUnderTesting($entityManager);
$expected = 'define what to expect';
$this->assertEquals(
$expected,
$sut->callSomeMethodToTestThatDependsOnSomeQueryByEmail()
);
}
}
source
share