【问题标题】:Class WebTestCase not found when checking debug:autowiring检查调试时找不到类 WebTestCase:自动装配
【发布时间】:2017-12-27 20:51:45
【问题描述】:

在检查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'

【问题讨论】:

  • 这个文件的内容是什么:/vagrant/mysymfony/app/config/services.yml?
  • @NikitaLeshchev 我刚刚编辑了主要问题。
  • 嗯...我没有看到任何错误。尝试执行composer dump-autoload。 LMK 如果对你有帮助的话。
  • @NikitaLeshchev 它似乎没有用。但是,将Test 文件夹移出AppBundle 文件夹并相应地修改命名空间似乎没有给出任何错误......但我认为这不是正确的方法。

标签: php symfony class testing load


【解决方案1】:

我从 Symfony IRC 频道收到了一条建议。一个叫 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'

【讨论】:

    猜你喜欢
    • 2019-04-17
    • 1970-01-01
    • 2019-06-14
    • 2015-10-13
    • 2015-05-07
    • 1970-01-01
    • 2019-04-20
    • 2020-12-01
    • 1970-01-01
    相关资源
    最近更新 更多