Skip to content

Instantly share code, notes, and snippets.

@simonjodet
Created October 19, 2012 08:33
Show Gist options
  • Save simonjodet/3916968 to your computer and use it in GitHub Desktop.
Save simonjodet/3916968 to your computer and use it in GitHub Desktop.
Mockery - Partially mocking class's own methods
<?php
class Database
{
/**
* @var \Doctrine\DBAL\Connection
*/
private $conn;
public function __construct(\Silex\Application $app)
{
$this->conn = $app['db'];
}
public function getSchema($version = null)
{
//...
}
public function listTables()
{
//...
}
public function dropTable($table)
{
//...
}
public function reset($version = null)
{
$tables = $this->listTables();
foreach ($tables as $table)
{
$this->dropTable($table);
}
$schema = $this->getSchema($version);
foreach ($schema as $query)
{
$this->conn->query($query);
}
}
}
<?php
namespace Tests\UnitTests;
class DatabaseTest extends \PHPUnit_Framework_TestCase
{
//...
public function test_reset_list_existing_tables_deletes_them_then_create_a_new_schema()
{
$app = new \Silex\Application();
$ConnectionMock = \Mockery::mock('\Doctrine\DBAL\Connection');
$ConnectionMock
->shouldReceive('query')
->with('SQL query 1')
->once()
->ordered('reset');
$ConnectionMock
->shouldReceive('query')
->with('SQL query 2')
->once()
->ordered('reset');
$app['db'] = $ConnectionMock;
$Database = \Mockery::mock('\Database[listTables,dropTable, getSchema]', array($app));
$Database
->shouldReceive('listTables')
->once()
->ordered('reset')
->andReturn(array('system', 'users'));
$Database
->shouldReceive('dropTable')
->with('system')
->once()
->ordered('reset');
$Database
->shouldReceive('dropTable')
->with('users')
->once()
->ordered('reset');
$Database
->shouldReceive('getSchema')
->with(1)
->once()
->ordered('reset')
->andReturn(array('SQL query 1', 'SQL query 2'));
$Database->reset(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment