【问题标题】:Keep getting "You cannot serialize or unserialize PDO instances" in PHPUnit using PHPStorm使用 PHPStorm 在 PHPUnit 中不断收到“您无法序列化或反序列化 PDO 实例”
【发布时间】:2015-12-11 14:23:34
【问题描述】:

我最近通过 Composer 在 PHPStorm 中设置了 PHPUnit。

我正在尝试测试一些需要我引导 Processwire(一个 CMS)的功能。

我不断收到消息“您无法序列化或反序列化 PDO 实例”,尽管应用了我研究过的以下条件以作为解决此问题的正确方法。

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

我还有什么遗漏或需要做的事情吗?

这些是我目前参考的资源。

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

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

我看到这篇文章将我的文章标记为重复,但我不认为它是相同的: PDOException: You cannot serialize or unserialize PDO instances

那篇文章中的测试直接引用了 PDO 对象,而我只是想让我的测试通过对 Processwire 的引导引用来运行。 这是我正在尝试运行的测试:

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()
    {

    }
}

【问题讨论】:

标签: php pdo phpunit processwire


【解决方案1】:

我终于明白了!无论出于何种原因,在测试中设置测试环境变量都没有效果。

通过创建 phpunit.xml 配置,定义测试参数并在 Phpstorm 中创建对它的引用,我终于能够运行测试了。

作为参考,这是我的 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>

我认为文件放在哪里并不重要,但我把它放在了我的测试所在的测试文件夹中。

我必须在 PHPStorm 中通过菜单(语言和框架 -> PHP -> PHPUnit)和自定义自动加载器部分中引用它,选择默认配置文件并将其指向 phpxml 文件。如果您使用不同的方法,请转到该菜单并在那里设置默认配置。

希望这对那里的人有所帮助,因为没有太多关于 PHPUnit 和 PHPStorm 的信息。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-11
  • 2012-01-31
  • 2017-09-10
  • 2019-06-25
  • 2011-05-27
  • 1970-01-01
  • 2015-03-03
相关资源
最近更新 更多