PHPUnit和Symfony2无法访问全局变量

时间:2016-03-31 08:25:56

标签: php symfony phpunit dbunit

这是我的phpunit.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
         backupGlobals="false"
         backupStaticAttributes="false"
         processIsolation="false"
         colors="true"
         stopOnFailure="false"
         syntaxCheck="false"
         bootstrap="bootstrap.php.cache"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
>
    <testsuites>
        <testsuite name="Project Test Suite">
            <directory>../src/*/*Bundle/Tests</directory>
            <directory>../src/*/Bundle/*Bundle/Tests</directory>
            <directory>../src/*Bundle/Tests</directory>
        </testsuite>
    </testsuites>

    <php>
        <server name="KERNEL_DIR" value="./" />
        <var name="DB_DSN" value="mysql:dbname=test_xxx;host=localhost" />
        <var name="DB_USER" value="root" />
        <var name="DB_PASSWD" value="" />
        <var name="DB_DBNAME" value="test_xxx" /> 
    </php>


    <logging>
        <log type="coverage-html" target="build/coverage" title="App Code Coverage" charset="UTF-8" yui="true" highlight="true"
        lowUpperBound="35" highLowerBound="70"/>
        <log type="coverage-clover" target="build/logs/clover.xml"/>
        <log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false"/>
    </logging>

    <filter>
        <whitelist>
            <directory>../src</directory>
            <exclude>
                <directory>../src/*Bundle/Resources</directory>
                <directory>../src/*Bundle/Tests</directory>
                <directory>../src/*/*Bundle/Resources</directory>
                <directory>../src/*/*Bundle/Tests</directory>
                <directory>../src/*/Bundle/*Bundle/Resources</directory>
                <directory>../src/*/Bundle/*Bundle/Tests</directory>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

在我的:

  • SRC
    • 的appbundle
      • 测试
        • AbstractDatabaseTestCase.php

AbstractDatabaseTestCase.php:

abstract class AbstractDatabaseTestCase extends \PHPUnit_Extensions_Database_TestCase{

    /**
     * 
     *@var PDO 
     * 
     */
    static $pdo;

    /**
     * 
     *@var \Doctrine\DBAL\Connection description 
     * 
     */
    static $dbal;

    private $conn;

    final public function getConnection()
    {
        var_dump($GLOBALS);
 // here my GLOBALS['DB_DSN'], GLOBALS['DB_USER'], GLOBALS['DB_PASSWD'] are not seen.

        if ($this->conn === null) {
            if (self::$pdo == null) {
                self::$pdo = new \PDO( $GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD'] );
            }
            $this->conn = $this->createDefaultDBConnection(self::$pdo, $GLOBALS['DB_DBNAME']);
        }

        return $this->conn;
    }

从评论中可以看出,鉴于我遵循了文档,我的类似乎没有看到phpunit.xml文件中定义的全局变量。

你有什么想法吗?我错过了什么吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

在symfony2中,默认的phpunit.xml不是app / phpunit.xml中的那个,而是应用程序的根目录。将所有值放在那里,一切正常。