Skip to content

Instantly share code, notes, and snippets.

@williamespindola
Created July 31, 2017 00:58
Show Gist options
  • Save williamespindola/097acae42d5f17bc1b9f4bb07bbed408 to your computer and use it in GitHub Desktop.
Save williamespindola/097acae42d5f17bc1b9f4bb07bbed408 to your computer and use it in GitHub Desktop.
example mock query builder
<?php
use ArizonaTecnologia\CarrefourWebView\Services\Offer\OfferExtraField;
use Mockery as m;
class OfferDataTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->mockDoctrineQueryBuilder = $this
->getMockBuilder('Doctrine\DBAL\Query\QueryBuilder')
->setMethods([
'from',
'select',
'where',
'setParameter',
'execute',
'fetchAll',
'innerJoin',
'leftJoin',
'andWhere'
])
->disableOriginalConstructor()
->getMock();
$this->mockDoctrineQueryBuilder
->expects($this->any())
->method('from')
->will($this->returnSelf());
$this->mockDoctrineQueryBuilder
->expects($this->any())
->method('leftJoin')
->will($this->returnSelf());
$this->mockDoctrineQueryBuilder
->expects($this->any())
->method('andWhere')
->will($this->returnSelf());
$this->mockDoctrineQueryBuilder
->expects($this->any())
->method('innerJoin')
->will($this->returnSelf());
$this->mockDoctrineQueryBuilder
->expects($this->any())
->method('select')
->will($this->returnSelf());
$this->mockDoctrineQueryBuilder
->expects($this->any())
->method('where')
->will($this->returnSelf());
$this->mockDoctrineQueryBuilder
->expects($this->any())
->method('setParameter')
->will($this->returnSelf());
$this->mockDoctrineQueryBuilder
->expects($this->any())
->method('execute')
->will($this->returnSelf());
$this->mockSelectExtraFields = m::mock(
'overload:ArizonaTecnologia\RetailPIP\Database\Sql\Offer\SelectExtraFields'
);
}
/**
* @expectedException ArizonaTecnologia\RetailPIP\Database\Exception\DatabaseSqlException
*/
public function testRequestWithInvalidOffersShouldThrowAndException()
{
$this->mockSelectExtraFields
->shouldReceive('execute')
->once()
->andThrow(new ArizonaTecnologia\RetailPIP\Database\Exception\DatabaseSqlException);
$instance = new OfferData($this->mockDoctrineQueryBuilder);
$instance->getOffersOfJobId(10101010101);
}
public function testSelectExtraFieldsWithValidParamShouldWork()
{
$this->mockSelectExtraFields
->shouldReceive('execute')
->once()
->andReturn('Test');
$instance = new OfferData($this->mockDoctrineQueryBuilder);
$actual = $instance->getOffersOfJobId(10101010101);
$this->assertEquals('Test', $actual);
}
protected function tearDown()
{
m::close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment