Skip to content

Instantly share code, notes, and snippets.

@amirbehzad
Created January 21, 2016 04:18
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirbehzad/399f8dd031be47ceeab3 to your computer and use it in GitHub Desktop.
Save amirbehzad/399f8dd031be47ceeab3 to your computer and use it in GitHub Desktop.
How to mock PDO for test-cases written in PHPUnit
<?php
// ...
protected function getMockedPDO()
{
$query = $this->getMock('\PDOStatement');
$query->method('execute')->willReturn(true);
$db = $this->getMockBuilder('\PDO')
->disableOriginalConstructor()
->setMethods(['prepare'])
->getMock();
$db->method('prepare')->willReturn($query);
return $db;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment