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'
/vagrant/mysymfony/app/config/services.yml?composer dump-autoload. LMK if it'll help you.Testfolder out of theAppBundlefolder and modifying the namespaces accordingly seems not to give any errors... But I don't think that's the proper way to do it.