2

我最近通过 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()
    {

    }
}
4

1 回答 1

4

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

通过创建 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 相关的信息。

于 2015-12-11T20:18:03.050 回答