0

When checking php bin/console debug:autowiring, I get the following errors:

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

The error is due to the following file:

<?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 <[email protected]>
 */
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
        );
    }
}

The file is in the following directory structure:

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

And that WebTestCase class is used in the following file:

<?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());
    }
}

Which is in the following path inside the tests directory:

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

The tests run without any kind of problem, and I actually discovered this error by chance. If I remove that file and any references to it from the tests then I can check debug:autowiring without any hassle. However I can't figure out what I am missing here. I guess it has something to do with file placement or something, but I don't really know. Can you lend me a hand please?

Also, if I didn't check that command, I wouldn't have noticed these errors. In order to avoid committing code to a repository which generates this kind of errors in the future, does Symfony log these errors somewhere? Or do I have to manually check every debug command?

Thank you in advance for your help.


Add contents of 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'
4
  • What is the content of this file: /vagrant/mysymfony/app/config/services.yml? Commented Dec 27, 2017 at 21:02
  • @NikitaLeshchev I just edited the main question. Commented Dec 27, 2017 at 21:07
  • Well... I don't see any errors. Try to execute composer dump-autoload. LMK if it'll help you. Commented Dec 28, 2017 at 7:43
  • @NikitaLeshchev it didn't seem to work. However, moving the Test folder out of the AppBundle folder and modifying the namespaces accordingly seems not to give any errors... But I don't think that's the proper way to do it. Commented Dec 28, 2017 at 14:52

1 Answer 1

2

I received a piece of advice from the Symfony IRC channel. A person named Remco told me to include the Test folder in the excludes of services.yml. Now running debug:autowiring doesn't give me any errors.

I told him to answer my question here but apparently he doesn't have an StackOverflow account so I'll write it up myself, but all the credit goes to him. Thank you. I would also like to thank Nikita for his time and help.

The services.yml would look like this, in case somebody else faces the same problem:

# 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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.