简体   繁体   中英

Unit testing services annotations without Symfony

I got a few bundles that are not installed in Symfony yet.

These bundles have a services.yml file in them:

mybundle/src/Bundle/Resources/config/services.yml

The services.yml contains classes and arguments from the bundle that are later used by Symfony , but not by the bundle itself:

mybundle.data.download.get:
  class: mybundle\data\download\getinfo\get
  arguments:
  - "@bundle.myDepdendency.generate"
  - "@bundle.myDepdendency.dosomething"
  - "@bundle.helloThere"

I have working unit tests in Symfony for services.yml that checks that all classes are loaded correctly, however since I am developing the bundles independently outside of Symfony, I'd like to have a test to know if services.yml contains all the classes and their arguments.

So the question is:

Is there a way to test if services.yml contains given classes and their arguments without using Symfony?

I would start with this snippet:

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

$containerBuilder = new ContainerBuilder();
$loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__.'/../BundlePath/Resources/config'));
$loader->load('services.yml');
$containerBuilder->compile();

Of course you need the symfony/dependency-injection and symfony/config components of symfony. But here you would test if any exception will thrown. If not, then every service was found and could be wired.

With

$containerBuilder->get('service_id') instanceof Bundle\Service\SomeService

you can even test if the service class was realy loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM