title: Mocking The PDO Object using PHPUnit
body: <p>I'm having difficulty mocking the PDO object with PHPUnit.</p>
  
  <p>There doesn't seem to be much information on the web about my problem but from what I can gather:</p>
  
  <ol>
  <li>PDO has 'final' __wakeup and
  __sleep  methods that prevent it from being serialised. </li>
  <li>PHPunit's mock object implementation serialises the object at some point.</li>
  <li>The unit tests then fail with a PHP error generated by PDO when this occurs.</li>
  </ol>
  
  <p>There is a feature meant to prevent this behavior, by adding the following line to your unit test:</p>
  
  <pre><code>class MyTest extends PHPUnit_Framework_TestCase
  
  {    
      protected $backupGlobals = FALSE;
       // ...
  
  }
  </code></pre>
  
  <p>Source: <a href="http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html" rel="noreferrer">http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html</a> </p>
  
  <p>This isnt working for me, my test still produces an error. </p>
  
  <p>Full test code:</p>
  
  <pre><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-&gt;backupGlobals = false;
          parent::__construct();
  
      }
  
  
      /**
       * Tests MyTest-&gt;__construct()
       */
      public function test__construct()
      {
  
          $pdoMock = $this-&gt;getMock('PDO', array('prepare'), array(), '', false);
  
          $classToTest = new MyTest($pdoMock);
  
          // Assert stuff here!
  
  
      }
  
      // More test code.......
  </code></pre>
  
  <p>Any PHPUnit pro's give me a hand? </p>
  
  <p>Thanks,</p>
  
  <p>Ben</p>