Created
December 2, 2013 09:07
-
-
Save gnutix/7746893 to your computer and use it in GitHub Desktop.
Mock Builder for Doctrine EntityManager / Connection mock objects.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
); | |
} | |
} |
+1
+1
Very very helpful ! +1 for publish on packagist
+1 for publishing
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you make this into a repo and add to packagist?