TL;博士
在共同欺骗测试中,我正在尝试$I->grabService()。服务在控制器中工作,没有自定义配置,但我得到:
Fail Service App\Service\Car is not available in container
完整故事
我有一个带有一些Services的项目,这些类基本上是做一些处理的类。所有Services都可以通过服务容器访问。我正在测试功能套件中的每个类(有些在单元中),直到今天,一切都很正常。
所以今天我增加了一个新的服务,当然也是一个测试。我做了:
root@9c80b567f681:/var/www/html# vendor/bin/codecept g:cest functional Service/Car
Test was created in /var/www/html/tests/functional/Service/CarCest.php测试如下:
<?php
namespace App\Tests\Service;
use App\Service\Car;
use App\Tests\FunctionalTester;
class CarCest
{
public function _before(FunctionalTester $I)
{
$I->grabService(Car::class);
}
public function tryToTest(FunctionalTester $I)
{
}
}现在,我在PhpStorm中手动创建了一个新类。类如下所示:
<?php
namespace App\Service;
class Car
{
}这是我测试的结果:
root@9c80b567f681:/var/www/html# vendor/bin/codecept run tests/functional/Service/CarCest.php
Codeception PHP Testing Framework v4.1.6
Powered by PHPUnit 9.2.6 by Sebastian Bergmann and contributors.
Running with seed:
App\Tests.functional Tests (1) ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
✖ CarCest: Try to test (0.00s)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Time: 00:01.616, Memory: 34.00 MB
There was 1 failure:
---------
1) CarCest: Try to test
Test tests/functional/Service/CarCest.php:tryToTest
Step Grab service "App\Service\Car"
Fail Service App\Service\Car is not available in container
Scenario Steps:
1. $I->grabService("App\Service\Car") at tests/functional/Service/CarCest.php:12
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.失败服务应用程序\ Service \Car在容器中不可用
现在,大多数其他测试都使用了相同的概念:我在_before()中获得服务,然后测试它。除了今天我加的任何一门课外,一切都通过了:) WTF?!?
BTW:如果我用以前创建的任何其他服务替换$I->grabService(Car::class);,它可以正常工作。
我的services.yaml是标准的,开箱即用的Symfony版本.我总是简单地依赖于这样一个事实:src/*中的所有东西都已经是服务了。
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']我花了一整个早上安装/重新安装/重新启动电脑.我完全迷失了,也很愚蠢。有人知道吗?
编辑:
我注意到了一些非常有趣的事情。如果我手动将服务添加到services.yml并设置public: true,那么我可以使用它。但是请注意,我以前创建的任何其他服务都不需要这样做。
发布于 2020-07-28 09:54:12
TL;DR
问题似乎是Symfony在容器编译时删除了所有未使用的服务。您可以看到代码在symfony项目git页面上。
当我注意到我的服务在被显式设置为public时工作正常之后,我开始研究这个问题,我偶然发现了git发行,那里有人也有同样的问题。更多的挖掘(和比我聪明的人交谈)让我找到了这个答案上面的链接。
砰!只花了4天时间.
https://stackoverflow.com/questions/63076272
复制相似问题