Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 607
Ben Waine Avatar asked Jun 29 '10 08:06

Ben Waine


People also ask

Which method is used to create a mock with PHPUnit?

PHPUnit provides methods that are used to automatically create objects that will replace the original object in our test. createMock($type) and getMockBuilder($type) methods are used to create mock object. The createMock method immediately returns a mock object of the specified type.

What is PHPUnit used for?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.

Is PHPUnit a framework?

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. The currently supported versions are PHPUnit 9 and PHPUnit 8.


1 Answers

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

like image 186
Anti Veeranna Avatar answered Oct 05 '22 12:10

Anti Veeranna