30/03/2018 - DOCTRINE, PHPUNIT, SYMFONY
PHPUnit ile Doctrine ORM Query class test etmenin sonucu aşağıdaki hata mesajıdır. Eğer yinede test etmek isterseniz, aşağıdaki örneği kullanabilirsiniz.
Class "Doctrine\ORM\Query" is declared "final" and cannot be mocked.
use Doctrine\ORM\EntityRepository;
public function findByEmailAddress(string $emailAddress): iterable
{
return $this->entityRepository
->createQueryBuilder('u')
->where('r.emailAddress = :emailAddress')
->setParameter('emailAddress', $emailAddress)
->getQuery()
->getArrayResult();
}
use AppBundle\Repository\RegistrationRepository;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;
/**
* @test
*/
public function find_by_email_address_returns_an_array_of_results()
{
$emailAddress = 'email_address';
/** @var QueryBuilder|PHPUnit_Framework_MockObject_MockObject $queryBuilderMock */
$queryBuilderMock = $this->getMockBuilder(QueryBuilder::class)
->disableOriginalConstructor()
->getMock();
/** @var EntityRepository|PHPUnit_Framework_MockObject_MockObject $entityRepositoryMock */
$entityRepositoryMock = $this->getMockBuilder(EntityRepository::class)
->disableOriginalConstructor()
->getMock();
$entityRepositoryMock
->expects($this->once())
->method('createQueryBuilder')
->with('r')
->willReturn($queryBuilderMock);
$queryBuilderMock
->expects($this->at(0))
->method('where')
->with('r.userEmailAddress = :userEmailAddress')
->willReturn($queryBuilderMock);
$queryBuilderMock
->expects($this->at(1))
->method('setParameter')
->with('userEmailAddress', $emailAddress)
->willReturn($queryBuilderMock);
/** @var AbstractQuery|PHPUnit_Framework_MockObject_MockObject $queryMock */
$queryMock = $this->getMockBuilder(AbstractQuery::class)
->disableOriginalConstructor()
->setMethods(['getArrayResult'])
->getMockForAbstractClass();
$queryBuilderMock
->expects($this->at(2))
->method('getQuery')
->with()
->willReturn($queryMock);
$queryMock
->expects($this->once())
->method('getArrayResult')
->with()
->willReturn([]);
$this->assertSame([], (new RegistrationRepository())->findByEmailAddress($emailAddress));
}