php - 检查调试 :autowiring 时找不到类 WebTestCase

标签 php symfony class testing load

检查 php bin/console debug:autowiring 时,出现以下错误:

In FileLoader.php line 168:

  Class Symfony\Bundle\FrameworkBundle\Test\WebTestCase not found in /vagrant/mysymfony/app/config/services.yml (whic
  h is being imported from "/vagrant/mysymfony/app/config/config.yml").


In WebTestCase.php line 17:

  Class Symfony\Bundle\FrameworkBundle\Test\WebTestCase not found


In WebTestCase.php line 21:

  Class Symfony\Bundle\FrameworkBundle\Test\KernelTestCase not found


In KernelTestCase.php line 24:

  Class PHPUnit\Framework\TestCase not found

错误是由于以下文件:

<?php
/**
 * This file is part of the RestExtraBundle package [1].
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this[1] source code.
 *
 * @license    MIT License
 * [1] https://github.com/willdurand/BazingaRestExtraBundle
 */
namespace AppBundle\Test;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;

/**
 * @author William Durand <william.durand1@gmail.com>
 */
abstract class WebTestCase extends BaseWebTestCase
{
    protected function assertJsonResponse($response, $statusCode = 200)
    {
        $this->assertEquals(
            $statusCode, $response->getStatusCode(),
            $response->getContent()
        );
        $this->assertTrue(
            $response->headers->contains('Content-Type', 'application/json'),
            $response->headers
        );
    }

    protected function jsonRequest($verb, $endpoint, array $data = array())
    {
        $data = empty($data) ? null : json_encode($data);
        return $this->client->request($verb, $endpoint,
            array(),
            array(),
            array(
                'HTTP_ACCEPT'  => 'application/json',
                'CONTENT_TYPE' => 'application/json'
            ),
            $data
        );
    }
}

该文件位于以下目录结构中:

.
├── AppBundle.php
├── Controller
│   ├── DefaultController.php
│   └── FooController.php
└── Test
    └── WebTestCase.php

WebTestCase 类在以下文件中使用:

<?php

namespace Tests\AppBundle\Controller;

use AppBundle\Test\WebTestCase;

class FooControllerTest extends WebTestCase
{
    public function testFooAction()
    {
        $client = static::createClient();

        $crawler = $client->request('GET', '/foo');

        $this->assertEquals(200, $client->getResponse()->getStatusCode());
        $this->assertJsonResponse($client->getResponse());
    }
}

tests 目录中的以下路径中:

.
└── AppBundle
    └── Controller
        ├── DefaultControllerTest.php
        └── FooControllerTest.php

测试运行没有任何问题,我实际上偶然发现了这个错误。如果我从测试中删除该文件和对它的任何引用,那么我可以毫不费力地检查 debug:autowiring。但是我不知道我在这里错过了什么。我猜它与文件放置或其他东西有关,但我真的不知道。你能帮我一下吗?

另外,如果我没有检查那个命令,我就不会注意到这些错误。为了避免将代码提交到将来会产生此类错误的存储库,Symfony 是否会在某处记录这些错误?还是我必须手动检查每个 debug 命令?

预先感谢您的帮助。


添加vagrant/mysymfony/app/config/services.yml的内容

# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
    #parameter_name: value

services:
    # default configuration for services in *this* file
    _defaults:
        # automatically injects dependencies in your services
        autowire: true
        # automatically registers your services as commands, event subscribers, etc.
        autoconfigure: true
        # this means you cannot fetch services directly from the container via $container->get()
        # if you need to do this, you can override this setting on individual services
        public: false

    # makes classes in src/AppBundle available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    AppBundle\:
        resource: '../../src/AppBundle/*'
        # you can exclude directories or files
        # but if a service is unused, it's removed anyway
        exclude: '../../src/AppBundle/{Entity,Repository,Tests}'

    # controllers are imported separately to make sure they're public
    # and have a tag that allows actions to type-hint services
    AppBundle\Controller\:
        resource: '../../src/AppBundle/Controller'
        public: true
        tags: ['controller.service_arguments']

    # add more services, or override services that need manual wiring
    # AppBundle\Service\ExampleService:
    #     arguments:
    #         $someArgument: 'some_value'

最佳答案

我从 Symfony IRC channel 收到了一条建议。一个叫 Remco 的人告诉我将 Test 文件夹包含在 services.ymlexcludes 中。现在运行 debug:autowiring 不会给我任何错误。

我让他在这里回答我的问题,但显然他没有 StackOverflow 帐户,所以我会自己写,但所有的功劳都归于他。谢谢。我还要感谢 Nikita 抽出时间提供帮助。

services.yml 看起来像这样,以防其他人遇到同样的问题:

# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
    #parameter_name: value

services:
    # default configuration for services in *this* file
    _defaults:
        # automatically injects dependencies in your services
        autowire: true
        # automatically registers your services as commands, event subscribers, etc.
        autoconfigure: true
        # this means you cannot fetch services directly from the container via $container->get()
        # if you need to do this, you can override this setting on individual services
        public: false

    # makes classes in src/AppBundle available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    AppBundle\:
        resource: '../../src/AppBundle/*'
        # you can exclude directories or files
        # but if a service is unused, it's removed anyway
        exclude: '../../src/AppBundle/{Entity,Repository,Tests,Test}'

    # controllers are imported separately to make sure they're public
    # and have a tag that allows actions to type-hint services
    AppBundle\Controller\:
        resource: '../../src/AppBundle/Controller'
        public: true
        tags: ['controller.service_arguments']

    # add more services, or override services that need manual wiring
    # AppBundle\Service\ExampleService:
    #     arguments:
    #         $someArgument: 'some_value'

关于php - 检查调试 :autowiring 时找不到类 WebTestCase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47998511/

相关文章:

如果字段重复,Php sql 只回显一行

php - Cake 无法连接到数据库

php - 我想让div一直出现在输入框下面

php - CodeIgniter-YouTube-API-Library 注册账号认证

php - 如何在 Twig 文件中设置变量、递增和回显?

php - MySql 学说 : find if given variable is IN array property

c++ - 没有重载函数的实例 "CVector4::operator"匹配指定的类型/术语不计算为采用 2 个参数的函数

java-- 为什么要复制类免疫性的参数?

android - 推送通知 Symfony/Sly notification Pusher 上的 400 错误请求

c++ - 循环依赖与可以相互初始化的类