Skip to content

Instantly share code, notes, and snippets.

@robertbasic
Created March 22, 2013 09:42
Show Gist options
  • Select an option

  • Save robertbasic/5220093 to your computer and use it in GitHub Desktop.

Select an option

Save robertbasic/5220093 to your computer and use it in GitHub Desktop.
<?php
namespace Blog\Model\Table;
use PHPUnit_Framework_TestCase as TestCase;
use \Mockery as m;
use Zend\ServiceManager\ServiceManager;
use Zend\Db\Sql\Expression;
use HexCommon\Db\Sql\Select;
class PostTest extends TestCase
{
protected $postTable;
protected $select;
protected $tableName = 'blog_posts';
public function setup()
{
$adapter = $this->getAdapterMock();
$this->postTable = new Post($adapter);
$this->select = m::mock(new Select($this->tableName));
$this->postTable->setSelect($this->select);
}
public function testGetAllPublishedPosts()
{
$this->select->shouldReceive('from')
->once()
->with($this->tableName)
->andReturn($this->select);
$this->select->shouldReceive('columns')
->once()
->with(array('*'))
->andReturn($this->select);
$this->select->shouldReceive('where')
->once()
->with(array('published = ?' => 1))
->andReturn($this->select);
$this->select->shouldReceive('order')
->once()
->with('id DESC')
->andReturn($this->select);
$this->postTable->getAllPublishedPosts();
}
public function testGetRecentPosts()
{
$this->select->shouldReceive('from')
->once()
->with($this->tableName)
->andReturn($this->select);
$this->select->shouldReceive('columns')
->once()
->with(array('id', 'title', 'slug'))
->andReturn($this->select);
$this->select->shouldReceive('where')
->once()
->with(array('published = ?' => 1))
->andReturn($this->select);
$this->select->shouldReceive('order')
->once()
->with('id DESC')
->andReturn($this->select);
$this->select->shouldReceive('limit')
->once()
->with(10)
->andReturn($this->select);
$this->postTable->getRecentPosts();
}
public function testGetYearMonthArchives()
{
$this->select->shouldReceive('from')
->once()
->with($this->tableName)
->andReturn($this->select);
$this->select->shouldReceive('columns')
->once()
->with(array('slug' => new Expression('DISTINCT DATE_FORMAT(datetime_added, "%Y/%m")')))
->andReturn($this->select);
$this->select->shouldReceive('where')
->once()
->with(array('published = ?' => 1))
->andReturn($this->select);
$this->select->shouldReceive('order')
->once()
->with('slug DESC')
->andReturn($this->select);
$this->postTable->getYearMonthArchives();
}
protected function getAdapterMock()
{
$adapter = m::mock('Zend\Db\Adapter\Adapter');
$platform = m::mock('Zend\Db\Adapter\Platform\Mysql[getName]');
$stmt = m::mock('Zend\Db\Adapter\Driver\Pdo\Statement');
$paramContainer = m::mock('Zend\Db\Adapter\ParameterContainer');
$platform->shouldReceive('getName')
->once()
->andReturn('MySQL');
$stmt->shouldReceive('getParameterContainer')
->once()
->andReturn($paramContainer);
$stmt->shouldReceive('setSql')
->once()
->andReturn($stmt);
$stmt->shouldReceive('execute')
->once()
->andReturn(array());
$adapter->shouldReceive('getPlatform')
->twice()
->andReturn($platform);
$adapter->shouldReceive('createStatement')
->once()
->andReturn($stmt);
return $adapter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment