Skip to content

Instantly share code, notes, and snippets.

@gnutix
Created December 2, 2013 09:07
Show Gist options
  • Save gnutix/7746893 to your computer and use it in GitHub Desktop.
Save gnutix/7746893 to your computer and use it in GitHub Desktop.
Mock Builder for Doctrine EntityManager / Connection mock objects.
<?php
namespace Mocks;
use Doctrine\DBAL\Driver\Statement;
/**
* Doctrine DBAL Statement implementing \Iterator.
*
* This class has been created because of a bug in PHPUnit Mock Objects.
*
* @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
*/
interface DoctrineDbalStatementInterface extends \Iterator, Statement
{
}
<?php
namespace Mocks;
/**
* Mock Builder for Doctrine objects
*/
class DoctrineMockBuilder extends \PHPUnit_Framework_TestCase
{
/**
* @return \Doctrine\ORM\EntityManager|\PHPUnit_Framework_MockObject_MockObject
*/
public function getEntityManagerMock()
{
$mock = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
->setMethods(
array(
'getConnection',
'getClassMetadata',
'close',
)
)
->getMock();
$mock->expects($this->any())
->method('getConnection')
->will($this->returnValue($this->getConnectionMock()));
$mock->expects($this->any())
->method('getClassMetadata')
->will($this->returnValue($this->getClassMetadataMock()));
return $mock;
}
/**
* @return \Doctrine\Common\Persistence\Mapping\ClassMetadata|\PHPUnit_Framework_MockObject_MockObject
*/
public function getClassMetadataMock()
{
$mock = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataInfo')
->disableOriginalConstructor()
->setMethods(array('getTableName'))
->getMock();
$mock->expects($this->any())
->method('getTableName')
->will($this->returnValue('{tableName}'));
return $mock;
}
/**
* @return \Doctrine\DBAL\Platforms\AbstractPlatform|\PHPUnit_Framework_MockObject_MockObject
*/
public function getDatabasePlatformMock()
{
$mock = $this->getAbstractMock(
'Doctrine\DBAL\Platforms\AbstractPlatform',
array(
'getName',
'getTruncateTableSQL',
)
);
$mock->expects($this->any())
->method('getName')
->will($this->returnValue('mysql'));
$mock->expects($this->any())
->method('getTruncateTableSQL')
->with($this->anything())
->will($this->returnValue('#TRUNCATE {table}'));
return $mock;
}
/**
* @return \Doctrine\DBAL\Connection|\PHPUnit_Framework_MockObject_MockObject
*/
public function getConnectionMock()
{
$mock = $this->getMockBuilder('Doctrine\DBAL\Connection')
->disableOriginalConstructor()
->setMethods(
array(
'beginTransaction',
'commit',
'rollback',
'prepare',
'query',
'executeQuery',
'executeUpdate',
'getDatabasePlatform',
)
)
->getMock();
$mock->expects($this->any())
->method('prepare')
->will($this->returnValue($this->getStatementMock()));
$mock->expects($this->any())
->method('query')
->will($this->returnValue($this->getStatementMock()));
$mock->expects($this->any())
->method('getDatabasePlatform')
->will($this->returnValue($this->getDatabasePlatformMock()));
return $mock;
}
/**
* @return \Doctrine\DBAL\Driver\Statement|\PHPUnit_Framework_MockObject_MockObject
*/
public function getStatementMock()
{
$mock = $this->getAbstractMock(
'Doctrine\DBAL\Driver\Statement', // In case you run PHPUnit <= 3.7, use 'Mocks\DoctrineDbalStatementInterface' instead.
array(
'bindValue',
'execute',
'rowCount',
'fetchColumn',
)
);
$mock->expects($this->any())
->method('fetchColumn')
->will($this->returnValue(1));
return $mock;
}
/**
* @param string $class The class name
* @param array $methods The available methods
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
protected function getAbstractMock($class, array $methods)
{
return $this->getMockForAbstractClass(
$class,
array(),
'',
true,
true,
true,
$methods,
false
);
}
}
Copy link

ghost commented Dec 2, 2013

Could you make this into a repo and add to packagist?

@egulias
Copy link

egulias commented Dec 2, 2013

+1

@cfuerst
Copy link

cfuerst commented Feb 6, 2014

+1

@chalasr
Copy link

chalasr commented Feb 1, 2016

Very very helpful ! +1 for publish on packagist

@jiri-jagos
Copy link

+1 for publishing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment