Class 检查时未找到 WebTestCase debug:autowiring

Class WebTestCase not found when checking debug:autowiring

检查 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 class 在以下文件中使用:

<?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 频道收到了一条建议。一个叫 Remco 的人告诉我将 Test 文件夹包含在 services.ymlexcludes 中。现在 运行 debug:autowiring 没有给我任何错误。

我让他在这里回答我的问题,但显然他没有 Whosebug 帐户,所以我会自己写,但所有的功劳都归于他。谢谢你。我还要感谢 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'