Created
July 22, 2016 20:30
-
-
Save chukShirley/8b4b125bd25f4d0d5faa3231cc19bb3b to your computer and use it in GitHub Desktop.
Mocking Zend Db Table Gateway
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 Load\Service\Query; | |
use Load\Infrastructure\Persistence\LoadTable; | |
use ServiceBus\Query\QueryHandlerInterface; | |
use Zend\Db\Sql\Predicate\Operator; | |
use Zend\Db\Sql\Select; | |
/** | |
* Class FetchAllUnpaidLoadsQueryHandler | |
* @package Load\Service\Query | |
*/ | |
class FetchAllUnpaidLoadsQueryHandler implements QueryHandlerInterface | |
{ | |
/** @var LoadTable */ | |
private $loadTable; | |
/** | |
* FetchAllUnpaidLoadsQueryHandler constructor. | |
* @param LoadTable $loadTable | |
*/ | |
public function __construct(LoadTable $loadTable) | |
{ | |
$this->loadTable = $loadTable; | |
} | |
/** | |
* @param FetchAllUnpaidLoadsQuery $query | |
* @return array | |
*/ | |
public function handle($query) | |
{ | |
$select = new Select('SCF030J1'); | |
$select->columns( | |
[ | |
'id' => 'TLSRN' | |
] | |
)->where([ | |
'TLPAYSTS' => '0', | |
new Operator('TLLOAD', Operator::OP_GT, '0') | |
]); | |
return $this->loadTable->selectWith($select)->toArray(); | |
} | |
} |
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 LoadTest\Service\Query; | |
use Load\Service\Query\FetchAllUnpaidLoadsQuery; | |
use Load\Service\Query\FetchAllUnpaidLoadsQueryHandler; | |
use PHPUnit_Framework_TestCase; | |
use Load\Infrastructure\Persistence\LoadTable; | |
/** | |
* Class FetchAllUnpaidLoadsQueryHandlerTest | |
* @package LoadTest\Service\Query | |
*/ | |
class FetchAllUnpaidLoadsQueryHandlerTest extends PHPUnit_Framework_TestCase | |
{ | |
private $loadTable; | |
public function setUp() | |
{ | |
$this->loadTable = $this->getMockBuilder(LoadTable::class) | |
->disableOriginalConstructor() | |
->setMethods(['selectWith']) | |
->getMock(); | |
} | |
public function testQueryHandlerReturnsQueryResults() | |
{ | |
$data = [ | |
[ | |
'id' => '12345', | |
], | |
[ | |
'id' => '12346', | |
] | |
]; | |
$this->loadTable->method('toArray')->willReturn( | |
$data | |
); | |
$query = new FetchAllUnpaidLoadsQuery(); | |
$handler = new FetchAllUnpaidLoadsQueryHandler( | |
$this->loadTable | |
); | |
$actual = $handler->handle($query); | |
$expected = $data; | |
$this->assertEquals($expected, $actual); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment