开发者

Mocking The PDO Object using PHPUnit

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->backupG开发者_C百科lobals = 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


$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.


The best I can think of is to use runkit and redefine the two final methods as protected using runkit_function_redefine.

Dont for get to enable the runkit.internal_override setting in php.ini.

And as ever, as with eval, if runkit seems like the answer, the question is probably wrong :)


You are instantiating your test case in your test case?

$classToTest = new MyTest($pdoMock);

Right now, you are essentially testing your test case. It should be more something like:

$classToTest = new My($pdoMock);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜