Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
namespace Generic\Model;
use Zend\Db\Sql\Predicate\Predicate;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
class GenericTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function getGeneric($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(array('id' => $id));
return ($rowset->count() !== 0) ? $rowset->current() : false;
}
public function saveGeneric(Generic $Generic)
{
$data = array(
'artist' => $Generic->artist,
'title' => $Generic->title,
);
$id = (int)$Generic->id;
if ($id == 0) {
return $this->tableGateway->insert($data);
} else {
if ($this->getGeneric($id)) {
return $this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('Form id does not exist');
}
}
}
}
<?php
namespace GenericTest\Model;
use Generic\Model\GenericTable;
use Generic\Model\Generic;
use Zend\Db\ResultSet\ResultSet;
use PHPUnit_Framework_TestCase;
use Zend\Db\Sql\Predicate\Predicate;
use Zend\Db\Sql\Select;
use \Mockery as m;
class GenericTableTest extends PHPUnit_Framework_TestCase
{
protected $_mockTableGateway;
// enable display of errors
protected $traceError = true;
public function setup()
{
$this->_mockTableGateway = \Mockery::mock('Zend\Db\TableGateway\TableGateway');
}
public function tearDown()
{
m::close();
}
public function testSaveNewRecord()
{
$data = new Generic();
$data->artist = "Matthew Setter";
$data->title = "Artist";
$data->id = 0;
$insertCount = 1;
$functionData = array(
'artist' => $data->artist,
'title' => $data->title,
);
$mockGenericTable = \Mockery::mock(new \Generic\Model\GenericTable($this->_mockTableGateway));
$this->_mockTableGateway->shouldReceive('insert')
->times(1)
->with($functionData)
->andReturn($insertCount);
$this->assertEquals($insertCount, $mockGenericTable->saveGeneric($data));
}
public function testUpdateExistingRecord()
{
$data = new Generic();
$updateCount = 1;
$functionData = array(
'artist' => "Matthew Setter",
'title' => "Artist",
'id' => 12
);
$data->exchangeArray($functionData);
$mockResultset = \Mockery::mock('Zend\Db\ResultSet\ResultSet[current,count]');
$mockResultset->shouldReceive('current')
->times(1)
->andReturn($functionData);
$mockResultset->shouldReceive('count')
->times(1)
->andReturn(1);
$this->_mockTableGateway->shouldReceive('update')
->times(1)
->with(array(
'artist' => $functionData['artist'],
'title' => $functionData['title']
),
array('id' => $data->id)
)
->andReturn($updateCount);
$this->_mockTableGateway->shouldReceive('select')
->times(1)
->with(array('id' => $functionData['id']))
->andReturn($mockResultset);
$mockGenericTable = new \Generic\Model\GenericTable($this->_mockTableGateway);
$this->assertEquals($updateCount, $mockGenericTable->saveGeneric($data));
}
/**
* @expectedException Exception
*/
public function testUpdateNonexistentRecordThrowsException()
{
$data = new Generic();
$updateCount = 1;
$functionData = array(
'artist' => "Matthew Setter",
'title' => "Artist",
'id' => 12
);
$data->exchangeArray($functionData);
$mockResultset = \Mockery::mock('Zend\Db\ResultSet\ResultSet[current]');
$this->_mockTableGateway->shouldReceive('select')
->times(1)
->with(array('id' => $functionData['id']))
->andReturn($mockResultset);
$mockGenericTable = new \Generic\Model\GenericTable($this->_mockTableGateway);
$this->assertEquals($updateCount, $mockGenericTable->saveGeneric($data));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.