Created
July 31, 2017 00:58
-
-
Save williamespindola/097acae42d5f17bc1b9f4bb07bbed408 to your computer and use it in GitHub Desktop.
example mock query builder
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 | |
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