/** * Checks if registered event subscribers use correct method references. * * @param TaggedService $service * @dataProvider getEventSubscribers */ public function testRegisteredEventSubscribers(TaggedService $service = null) { if ($service === null) { // No event subscribers registered, nothing to test. return; } $creator = new ServiceCreator($this->getContainer()); /* @var $subscriber EventSubscriberInterface */ $subscriber = $creator->create($service->getServiceId()); $message = 'Service "%s" is not a valid event subscriber.'; $this->assertThat($subscriber, new IsEventSubscriberConstraint(), sprintf($message, $service->getServiceId())); }
/** * Checks if the validators that are configured in the container implement * the correct interface. * * @param TaggedService $service * @dataProvider getValidators */ public function testRegisteredValidatorsImplementCorrectInterface(TaggedService $service = null) { if ($service === null) { // No validators registered, nothing to test. return; } $creator = new ServiceCreator($this->getContainer()); /* @var $validator \Symfony\Component\Validator\ConstraintValidatorInterface */ $validator = $creator->create($service->getServiceId()); $message = 'Service "%s" is tagged as validator, but it does not implement the required interface.'; $message = sprintf($message, $service->getServiceId()); $this->assertInstanceOf('\\Symfony\\Component\\Validator\\ConstraintValidatorInterface', $validator, $message); }
/** * Checks if registered Twig extensions implement the correct interface. * * @param TaggedService $service * @dataProvider getTwigExtensions */ public function testRegisteredTwigExtensionsImplementCorrectInterface(TaggedService $service = null) { if ($service === null) { // No twig extensions registered, nothing to test. return; } $creator = new ServiceCreator($this->getContainer()); /* @var $extension \Twig_ExtensionInterface */ $extension = $creator->create($service->getServiceId()); $message = 'Service "%s" is tagged as Twig extension, but it does not implement the required interface.'; $message = sprintf($message, $service->getServiceId()); $this->assertInstanceOf('\\Twig_ExtensionInterface', $extension, $message); }
/** * Ensures that an exception is thrown if service creation fails because of * an invalid service definition. */ public function testCreatorThrowsExceptionIfItIsNotPossibleToCreateService() { $definition = new Definition(); // Use the new way to define factories if available or fall back to the // mechanism from previous Symfony versions. if (method_exists($definition, 'setFactory')) { $definition->setFactory(array('Webfactory\\Missing', 'create')); } else { $definition->setFactoryClass('Webfactory\\Missing'); $definition->setFactoryMethod('create'); } $this->container->setDefinition('my.service', $definition); $this->setExpectedException('Webfactory\\Util\\CannotInstantiateServiceException'); $this->creator->create('my.service'); }
/** * Checks if it is possible to create instances of the defined services. */ public function testServicesCanBeInstantiated() { $container = $this->getContainer(); $errors = array(); $creator = new ServiceCreator($container); $services = new ApplicationServiceIterator($this->getKernel(), new \ArrayIterator($container->getServiceIds())); foreach ($services as $id) { /* @var $id string */ try { $creator->create($id); } catch (CannotInstantiateServiceException $e) { $errors[] = $e; } } $this->assertCount(0, $errors, implode(PHP_EOL . PHP_EOL, $errors)); }
/** * Checks if the aliases for form types and their names are equal. * * Form types cannot be used if this is not the case. * * @param TaggedService|null $service * @dataProvider getFormTypes */ public function testFormTypeAliasAndNameAreEqual(TaggedService $service = null) { if ($service === null) { // No form types registered, nothing to test. return; } $creator = new ServiceCreator($this->getContainer()); /* @var $type \Symfony\Component\Form\FormTypeInterface */ $type = $creator->create($service->getServiceId()); $message = 'Service "%s" is tagged as form type, but it does not implement the required interface.'; $message = sprintf($message, $service->getServiceId()); $this->assertInstanceOf('\\Symfony\\Component\\Form\\FormTypeInterface', $type, $message); $tagDefinition = $service->getTagDefinition(); $message = 'An alias must be defined for form type "%s".'; $message = sprintf($message, $service->getServiceId()); $this->assertArrayHasKey('alias', $service->getTagDefinition(), $message); $message = 'Form type name and assigned alias must match, but service "%s" ' . 'uses "%s" as name and "%s" as alias.'; $message = sprintf($message, $service->getServiceId(), $type->getName(), $tagDefinition['alias']); $this->assertEquals($type->getName(), $tagDefinition['alias'], $message); }