2

I've recently set up PHPUnit in PHPStorm via Composer.

I'm trying to test some functionality that requires me to bootstrap Processwire (a CMS).

I keep constantly getting the message "You cannot serialize or unserialize PDO instances" despite applying the conditions below which I researched to be the correct way of fixing this issue.

 * @backupGlobals disabled
 * @backupStaticAttributes disabled
 * @runTestsInSeparateProcesses
 * @runInSeparateProcess
 * @preserveGlobalState disabled

Is there anything else I have missed or need to do to get this working?

These are the resources I have referenced so far.

https://phpunit.de/manual/current/en/phpunit-book.html#appendixes.annotations.preserveGlobalState

http://edmondscommerce.github.io/php/phpunit-and-pdoexception-solution.html

I've seen this article that flagged my article as a duplicate but I don't believe it to be the same : PDOException: You cannot serialize or unserialize PDO instances

The test in that article has direct references to the PDO object whereas I'm just trying to get my tests to run with a bootstrap reference to Processwire. This is my test I'm trying to run :

namespace Test;

include_once(__DIR__."/../../../../index.php");     //Bootstrap to Processwire CMS

class ImageTest extends \PHPUnit_Framework_TestCase {

    /**
     * @backupGlobals disabled
     * @backupStaticAttributes disabled
     * @runTestsInSeparateProcesses
     * @runInSeparateProcess
     * @preserveGlobalState disabled
     */


    protected function setUp()
    {
        //$this->testpages = wire(pages)->find("template=fingergames|template=songs|template=poems");
    }

    public function testImageEmptyLinks()
    {
        //$testpages = wire(pages)->find("template=fingergames|template=songs|template=poems");

        $blanks = wire(pages)->find("image=''");
        $this->assertEquals($blanks->count(),0);
    }

    public function testImageMismatchedLinks()
    {
        //$this->assertTrue(true);
        $this->assertEquals(0,0);
    }

    public function testImageMissingSiblings()
    {
        $this->assertEquals(0,0);
    }

    protected function tearDown()
    {

    }
}
Community
  • 1
  • 1
Francis
  • 1,798
  • 1
  • 26
  • 32
  • Possible duplicate of [PDOException: You cannot serialize or unserialize PDO instances](http://stackoverflow.com/questions/8700702/pdoexception-you-cannot-serialize-or-unserialize-pdo-instances) – Flosculus Dec 11 '15 at 14:39
  • I've seen that article before. I don't believe this is a duplicate. – Francis Dec 11 '15 at 14:43

1 Answers1

4

I finally figured it out! For whatever reason, setting the test environment variables in the test was having no effect.

By creating a phpunit.xml configuration, defining the test parameters and creating a reference to it in Phpstorm I was finally able to run the test.

For reference, this was the contents of my phpunit.xml

<phpunit
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd"
        backupGlobals="false"
        backupStaticAttributes="false"
        processIsolation="false">
</phpunit>

I don't think it matters where the file is placed but I placed it in the test folder where my tests resides.

And I had to reference it in PHPStorm by going through the menu (Language & Framework -> PHP -> PHPUnit) and in the Custom Autoloader section, selecting the default configuration file and pointing it to the phpxml file. If you're using a different method, then go to that menu and set the default configuration there.

Hope this helps somebody out there, as there isn't much information relating to PHPUnit & PHPStorm in conjunction.

Francis
  • 1,798
  • 1
  • 26
  • 32
  • 1
    I wrote some articles here that go in more depth. Enjoy. https://processwire.com/talk/topic/13664-testing-with-phpunit-processwire-phpstorm/ – Francis Jul 08 '16 at 22:52