Skip to content

Instantly share code, notes, and snippets.

@netojoaobatista
Created November 14, 2012 16:25
Show Gist options
  • Save netojoaobatista/4073112 to your computer and use it in GitHub Desktop.
Save netojoaobatista/4073112 to your computer and use it in GitHub Desktop.
To mock or not to mock, that is the question
<?php
class Example {
public function getInsertSQL() {
return 'INSERT INTO `example` (
`email`,
`name`
) VALUES (
:email,
:name
);';
}
public function insert(User $user, PDO $into) {
$stm = $into->prepare($this->getInsertSQL());
$stm->bindValue(':email', $user->getEmail());
$stm->bindValue(':name', $user->getName());
$stm->execute();
$id = $into->lastInsertId();
$user->setId($id);
return !!$id;
}
}
CREATE TABLE IF NOT EXISTS `example` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`email` TEXT NOT NULL UNIQUE,
`name` TEXT NOT NULL
);
<?php
require_once 'User.php';
require_once 'Example.php';
require_once 'PHPUnit/Framework/TestCase.php';
/**
* Example test case.
*/
class ExampleTest extends PHPUnit_Framework_TestCase {
/**
* @var PDO
*/
private $pdo;
/**
* Prepares the environment before running a test.
*/
protected function setUp() {
parent::setUp();
$this->pdo = new PDO('sqlite::memory:');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->exec('
CREATE TABLE IF NOT EXISTS `example` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`email` TEXT NOT NULL UNIQUE,
`name` TEXT NOT NULL
);');
}
/**
* Cleans up the environment after running a test.
*/
protected function tearDown() {
$this->pdo->exec('DROP TABLE `example`;');
parent::tearDown();
}
/**
* Tests Example->insert()
*/
public function testInsert() {
$example = new Example();
$user = new User('neto', '[email protected]');
$this->assertTrue($example->insert($user, $this->pdo));
$this->assertEquals(1, $user->getId());
}
/**
* Tests Example->insert()
* @expectedException PDOException
*/
public function testInsertWithNullShouldThrownAPDOException() {
$example = new Example();
$user = new User(null, '[email protected]');
$example->insert($user, $this->pdo);
}
}
<?php
require_once 'User.php';
require_once 'Example.php';
require_once 'PHPUnit/Framework/TestCase.php';
/**
* Example test case.
*/
class ExampleTestWithMockedPDO extends PHPUnit_Framework_TestCase {
/**
* Tests Example->insert() with mocked PDO
*/
public function testInsert() {
$example = new Example();
$user = new User('Neto', '[email protected]');
$pdoStatement = $this->getMock('PDOStatement',
array('bindValue', 'execute')
);
$pdoStatement->expects($this->at(0))
->method('bindValue')
->with($this->equalTo(':email'),
$user->getEmail());
$pdoStatement->expects($this->at(1))
->method('bindValue')
->with($this->equalTo(':name'),
$user->getName());
$pdoStatement->expects($this->exactly(1))
->method('execute')
->will($this->returnValue(true));
$pdo = $this->getMock('PDO',
array('prepare', 'lastInsertId'),
array('sqlite::memory:')
);
$pdo->expects($this->once())
->method('prepare')
->with($this->equalTo($example->getInsertSQL()))
->will($this->returnValue($pdoStatement));
$pdo->expects($this->once())
->method('lastInsertId')
->will($this->returnValue(1));
$this->assertTrue($example->insert($user, $pdo));
$this->assertEquals(1, $user->getId());
}
}
<?php
class User {
private $id;
private $email;
private $name;
public function __construct($name, $email) {
$this->setName($name);
$this->setEmail($email);
}
public function getId() {
return $this->id;
}
public function getEmail() {
return $this->email;
}
public function getName() {
return $this->name;
}
public function setId($id) {
$this->id = $id;
}
public function setName($name) {
$this->name = $name;
}
public function setEmail($email) {
$this->email = $email;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment