ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

仅在使用PHPUnit时找不到类

2019-05-28 07:18:30  阅读:185  来源: 互联网

标签:php phpunit symfony


我正在使用Symfony2(2.7.3)应用程序进行测试,并且页面控制器仅在从PHPUnit(4.8.6)发送请求时才能加载类.

测试看起来像这样:

//AppBundle/Tests/Controller/PagesAvailableTest.php

<?php

namespace AppBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class PagesAvailableTest extends WebTestCase
{
    public function testpages()
    {
        $client = static::createClient();
        $client->request('GET', '/contact'); // Throws the error
    }
}

使用$phpunit -c app /运行时抛出此错误:

PHP Fatal error: Class ‘AppBundle\Entity\ContactMessage’ not found in SymfonyRoot/src/AppBundle/Controller/ContactController.php on line 28

通过浏览器调用时,页面按预期加载.当使用$curl -I http:// localhost / contact调用时,页面的状态为HTTP / 1.1 200 OK

我看过其他similar questions讨论自动加载的问题 – 但Symfony docs建议类应该自动加载没有问题:

Just like in your real application – autoloading is automatically enabled via the bootstrap.php.cache file (as configured by default in the app/phpunit.xml.dist file).

<!-- app/phpunit.xml.dist -->
<?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"
         colors="true"
         bootstrap="bootstrap.php.cache"
>
    <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>-->
        <!--In my experimentation, I uncommented this and tinkered with 
            a few values. After having no luck, I commented it out again. -->
        <!--<server name="KERNEL_DIR" value="." />-->
    <!--</php>-->

    <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>

如何在使用PHPUnit时加载这些类?

解决方法:

我尴尬地忽略了一个关键细节:我的应用程序运行在与我的PATH环境变量指向的不同的PHP解释器上.

当我运行$phpunit -c app /时,它使用我的操作系统的默认PHP(5.5.20) – 我的网站运行在PHP 5.6.10上,由MAMP提供.

通过在调用PHPUnit时提供自己的正确PHP解释器路径,我能够解决这个问题:

$/ path / to / correct / php phpunit -c app /

标签:php,phpunit,symfony
来源: https://codeday.me/bug/20190528/1169244.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有