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.

Revisions

  1. gnutix created this gist Dec 2, 2013.
    16 changes: 16 additions & 0 deletions DoctrineDbalStatementInterface.php
    Original 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
    {
    }
    156 changes: 156 additions & 0 deletions DoctrineMockBuilder.php
    Original 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
    );
    }
    }