簡體   English   中英

檢查debug:autowiring時找不到WebTestCase類

[英]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類:

<?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文件夾excludesservices.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'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM