Created
December 2, 2013 09:07
-
-
Save gnutix/7746893 to your computer and use it in GitHub Desktop.
Revisions
-
gnutix created this gist
Dec 2, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ <?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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,156 @@ <?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 ); } }