Mocking The PDO Object using PHPUnit

Ben Waine picture Ben Waine · Jun 29, 2010 · Viewed 10.2k times · Source

I'm having difficulty mocking the PDO object with PHPUnit.

There doesn't seem to be much information on the web about my problem but from what I can gather:

  1. PDO has 'final' __wakeup and __sleep methods that prevent it from being serialised.
  2. PHPunit's mock object implementation serialises the object at some point.
  3. The unit tests then fail with a PHP error generated by PDO when this occurs.

There is a feature meant to prevent this behavior, by adding the following line to your unit test:

class MyTest extends PHPUnit_Framework_TestCase

{    
    protected $backupGlobals = FALSE;
     // ...

}

Source: http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html

This isnt working for me, my test still produces an error.

Full test code:

class MyTest extends PHPUnit_Framework_TestCase
{

    /**
     * @var MyTest
     */
    private $MyTestr;

    protected $backupGlobals = FALSE;

    /**
     * Prepares the environment before running a test.
     */
    protected function setUp()
    {
        parent::setUp();

    }

    /**
     * Cleans up the environment after running a test.
     */
    protected function tearDown()
    {

        parent::tearDown();
    }

    public function __construct()
    {

        $this->backupGlobals = false;
        parent::__construct();

    }


    /**
     * Tests MyTest->__construct()
     */
    public function test__construct()
    {

        $pdoMock = $this->getMock('PDO', array('prepare'), array(), '', false);

        $classToTest = new MyTest($pdoMock);

        // Assert stuff here!


    }

    // More test code.......

Any PHPUnit pro's give me a hand?

Thanks,

Ben

Answer

Anti Veeranna picture Anti Veeranna · Nov 2, 2010

$backupGlobals does not help you, because this error comes from elsewhere. PHPUnit 3.5.2 (possibly earlier versions as well) has the following code in PHPUnit/Framework/MockObject/Generator.php

    if ($callOriginalConstructor &&
        !interface_exists($originalClassName, $callAutoload)) {
        if (count($arguments) == 0) {
            $mockObject = new $mock['mockClassName'];
        } else {
            $mockClass  = new ReflectionClass($mock['mockClassName']);
            $mockObject = $mockClass->newInstanceArgs($arguments);
        }
    } else {
        // Use a trick to create a new object of a class
        // without invoking its constructor.
        $mockObject = unserialize(
          sprintf(
            'O:%d:"%s":0:{}',
            strlen($mock['mockClassName']), $mock['mockClassName']
          )
        );
    }

This "trick" with unserialize is used when you ask getMock to not execute the original constructor and it will promptly fail with PDO.

So, how do work around it?

One option is to create a test helper like this

class mockPDO extends PDO
{
    public function __construct ()
    {}

}

The goal here is to get rid of the original PDO constructor, which you do not need. Then, change your test code to this:

$pdoMock = $this->getMock('mockPDO', array('prepare'));

Creating mock like this will execute original constructor, but since it is now harmless thanks to mockPDO test helper, you can continue testing.