View source
<?php
namespace Drupal\Tests\service_container\DependencyInjection;
use Drupal\service_container\DependencyInjection\Container;
use Drupal\service_container\DependencyInjection\ContainerInterface;
use Mockery;
class ContainerTest extends \PHPUnit_Framework_TestCase {
protected $container;
protected $containerDefinition;
public function setUp() {
$this->containerDefinition = $this
->getMockContainerDefinition();
$this->container = new Container($this->containerDefinition);
}
public function test_hasDefinition() {
$this
->assertEquals($this->container
->hasDefinition('service_container'), TRUE, 'Container has definition of itself.');
$this
->assertEquals($this->container
->hasDefinition('service.does_not_exist'), FALSE, 'Container does not have non-existent service.');
$this
->assertEquals($this->container
->hasDefinition('service.provider'), TRUE, 'Container has service.provider service.');
}
public function test_getDefinition() {
$this
->assertEquals($this->containerDefinition['services']['service_container'], $this->container
->getDefinition('service_container'), 'Container definition matches for container service.');
$this
->assertEquals($this->containerDefinition['services']['service.provider'], $this->container
->getDefinition('service.provider'), 'Container definition matches for service.provider service.');
}
public function test_getDefinition_exception() {
$this->container
->getDefinition('service_not_exist');
}
public function test_getDefinitions() {
$this
->assertEquals($this->containerDefinition['services'], $this->container
->getDefinitions(), 'Container definition matches input.');
}
public function test_getParameter() {
$this
->assertEquals($this->containerDefinition['parameters']['some_config'], $this->container
->getParameter('some_config'), 'Container parameter matches for %some_config%.');
$this
->assertEquals($this->containerDefinition['parameters']['some_other_config'], $this->container
->getParameter('some_other_config'), 'Container parameter matches for %some_other_config%.');
}
public function test_hasParameter() {
$this
->assertTrue($this->container
->hasParameter('some_config'), 'Container parameters include %some_config%.');
$this
->assertFalse($this->container
->hasParameter('some_config_not_exists'), 'Container parameters do not include %some_config_not_exists%.');
}
public function test_setParameter_unfrozenContainer() {
$this->containerDefinition['frozen'] = FALSE;
$this->container = new Container($this->containerDefinition);
$this->container
->setParameter('some_config', 'new_value');
$this
->assertEquals('new_value', $this->container
->getParameter('some_config'), 'Container parameters can be set.');
}
public function test_setParameter_frozenContainer() {
$this->containerDefinition['frozen'] = TRUE;
$this->container = new Container($this->containerDefinition);
$this->container
->setParameter('some_config', 'new_value');
}
public function test_get() {
$container = $this->container
->get('service_container');
$this
->assertSame($this->container, $container, 'Container can be retrieved from itself.');
$other_service_class = $this->containerDefinition['services']['other.service']['class'];
$other_service = $this->container
->get('other.service');
$this
->assertInstanceOf($other_service_class, $other_service, 'other.service has the right class.');
$some_parameter = $this->containerDefinition['parameters']['some_config'];
$some_other_parameter = $this->containerDefinition['parameters']['some_other_config'];
$service = $this->container
->get('service.provider');
$this
->assertEquals($other_service, $service
->getSomeOtherService(), '@other.service was injected via constructor.');
$this
->assertEquals($some_parameter, $service
->getSomeParameter(), '%some_config% was injected via constructor.');
$this
->assertEquals($this->container, $service
->getContainer(), 'Container was injected via setter injection.');
$this
->assertEquals($some_other_parameter, $service
->getSomeOtherParameter(), '%some_other_config% was injected via setter injection.');
$this
->assertEquals($service->_someProperty, 'foo', 'Service has added properties.');
}
public function test_set() {
$this
->assertNull($this->container
->get('new_id', ContainerInterface::NULL_ON_INVALID_REFERENCE));
$mock_service = new MockService();
$this->container
->set('new_id', $mock_service);
$this
->assertSame($mock_service, $this->container
->get('new_id'), 'A manual set service works as expected.');
}
public function test_has() {
$this
->assertTrue($this->container
->has('other.service'));
$this
->assertFalse($this->container
->has('another.service'));
$mock_service = new MockService();
$this->container
->set('another.service', $mock_service);
$this
->assertTrue($this->container
->has('another.service'));
}
public function test_get_circular() {
$this->container
->get('circular_dependency');
}
public function test_get_exception() {
$this->container
->get('service_not_exists');
}
public function test_get_notFound_parameter() {
$service = $this->container
->get('service_parameter_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE);
$this
->assertNull($service, 'Some service is NULL.');
}
public function test_get_notFound_parameterWithExceptionOnSecondCall() {
$service = $this->container
->get('service_parameter_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE);
$this
->assertNull($service, 'Some service is NULL.');
$this->container
->set('service_parameter_not_exists', NULL);
$this->container
->get('service_parameter_not_exists');
}
public function test_get_notFound_parameter_exception() {
$this->container
->get('service_parameter_not_exists');
}
public function test_get_notFound_dependency() {
$service = $this->container
->get('service_dependency_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE);
$this
->assertNull($service, 'Some service is NULL.');
}
public function test_get_notFound_dependency_exception() {
$this->container
->get('service_dependency_not_exists');
}
public function test_get_notFound() {
$this
->assertNull($this->container
->get('service_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE), 'Not found service does not throw exception.');
}
public function test_get_notFoundMultiple() {
$container = \Mockery::mock('Drupal\\service_container\\DependencyInjection\\Container[getDefinition]', array(
$this->containerDefinition,
));
$this
->assertNull($container
->get('service_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE, 'Not found service does not throw exception.'));
$this
->assertNull($container
->get('service_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE, 'Not found service does not throw exception on second call.'));
}
public function test_get_notFoundMultipleWithExceptionOnSecondCall() {
$this
->assertNull($this->container
->get('service_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE, 'Not found service does nto throw exception.'));
$this->container
->get('service_not_exists');
}
public function test_get_alias() {
$service = $this->container
->get('service.provider');
$aliased_service = $this->container
->get('service.provider_alias');
$this
->assertSame($service, $aliased_service);
}
public function test_get_factoryServiceNew() {
$factory_service = $this->container
->get('factory_service_new');
$factory_service_class = $this->container
->getParameter('factory_service_class');
$this
->assertInstanceOf($factory_service_class, $factory_service);
}
public function test_get_factoryClassNew() {
$service = $this->container
->get('service.provider');
$factory_service = $this->container
->get('factory_class_new');
$this
->assertInstanceOf(get_class($service), $factory_service);
$this
->assertEquals('bar', $factory_service
->getSomeParameter(), 'Correct parameter was passed via the factory class instantiation.');
$this
->assertEquals($this->container, $factory_service
->getContainer(), 'Container was injected via setter injection.');
}
public function test_resolveServicesAndParameters_privateService() {
$service = $this->container
->get('service_using_private');
$private_service = $service
->getSomeOtherService();
$this
->assertEquals($private_service
->getSomeParameter(), 'really_private_lama', 'Private was found successfully');
}
public function test_resolveServicesAndParameters_array() {
$service = $this->container
->get('service_using_array');
$other_service = $this->container
->get('other.service');
$this
->assertEquals($other_service, $service
->getSomeOtherService(), '@other.service was injected via constructor.');
}
public function test_resolveServicesAndParameters_optional() {
$service = $this->container
->get('service_with_optional_dependency');
$this
->assertNull($service
->getSomeOtherService(), 'other service was NULL was expected.');
}
public function test_initialized() {
$this
->assertFalse($this->container
->initialized('late.service'), 'Late service is not initialized.');
$this->container
->get('late.service');
$this
->assertTrue($this->container
->initialized('late.service'), 'Late service is initialized after it was gotten.');
}
public function test_camelize($string_underscore, $string_camelize) {
$result = $this->container
->camelize($string_underscore);
$this
->assertEquals($string_camelize, $result);
}
public function test_underscore($string_underscore, $string_camelize) {
$result = $this->container
->underscore($string_camelize);
$this
->assertEquals($string_underscore, $result);
}
public function underscoreCamelizeDataProvider() {
return array(
array(
'service_container',
'ServiceContainer',
),
array(
'service_container_symfony',
'ServiceContainerSymfony',
),
array(
'123service_container',
'123serviceContainer',
),
array(
'123service_container_symfony',
'123serviceContainerSymfony',
),
array(
'123service_container',
'123serviceContainer',
),
array(
'123service_container_symfony',
'123serviceContainerSymfony',
),
);
}
protected function getMockContainerDefinition() {
$fake_service = Mockery::mock('alias:Drupal\\Tests\\service_container\\DependencyInjection\\FakeService');
$parameters = array();
$parameters['some_private_config'] = 'really_private_lama';
$parameters['some_config'] = 'foo';
$parameters['some_other_config'] = 'lama';
$parameters['factory_service_class'] = get_class($fake_service);
$services = array();
$services['service_container'] = array(
'class' => '\\Drupal\\service_container\\DependencyInjection\\Container',
);
$services['other.service'] = array(
'class' => get_class($fake_service),
);
$services['late.service'] = array(
'class' => get_class($fake_service),
);
$services['service.provider'] = array(
'class' => '\\Drupal\\Tests\\service_container\\DependencyInjection\\MockService',
'arguments' => array(
'@other.service',
'%some_config%',
),
'properties' => array(
'_someProperty' => 'foo',
),
'calls' => array(
array(
'setContainer',
array(
'@service_container',
),
),
array(
'setOtherConfigParameter',
array(
'%some_other_config%',
),
),
),
'priority' => 0,
);
$private_service = array(
'class' => '\\Drupal\\Tests\\service_container\\DependencyInjection\\MockService',
'arguments' => array(
'@other.service',
'%some_private_config%',
),
'public' => FALSE,
);
$private_hash = sha1(serialize($private_service));
$services['service_using_private'] = array(
'class' => '\\Drupal\\Tests\\service_container\\DependencyInjection\\MockService',
'arguments' => array(
(object) array(
'type' => 'private_service',
'value' => $private_service,
'id' => 'private__' . $private_hash,
),
'%some_config%',
),
);
$services['service_using_array'] = array(
'class' => '\\Drupal\\Tests\\service_container\\DependencyInjection\\MockService',
'arguments' => array(
array(
'@other.service',
),
'%some_private_config%',
),
);
$services['service_with_optional_dependency'] = array(
'class' => '\\Drupal\\Tests\\service_container\\DependencyInjection\\MockService',
'arguments' => array(
'@?service.does_not_exist',
'%some_private_config%',
),
);
$services['factory_service'] = array(
'class' => '\\Drupal\\service_container\\ServiceContainer\\ControllerInterface',
'factory_method' => 'getFactoryMethod',
'factory_service' => 'service.provider',
'arguments' => array(
'%factory_service_class%',
),
);
$services['factory_class'] = array(
'class' => '\\Drupal\\service_container\\ServiceContainer\\ControllerInterface',
'factory_method' => 'getFactoryMethod',
'factory_class' => '\\Drupal\\Tests\\service_container\\DependencyInjection\\MockService',
'arguments' => array(
'\\Drupal\\Tests\\service_container\\DependencyInjection\\MockService',
array(
NULL,
'bar',
),
),
'calls' => array(
array(
'setContainer',
array(
'@service_container',
),
),
),
);
$services['factory_service_new'] = array(
'class' => '\\Drupal\\service_container\\ServiceContainer\\ControllerInterface',
'factory' => array(
'@service.provider',
'getFactoryMethod',
),
'arguments' => array(
'%factory_service_class%',
),
);
$services['factory_class_new'] = array(
'class' => '\\Drupal\\service_container\\ServiceContainer\\ControllerInterface',
'factory' => '\\Drupal\\Tests\\service_container\\DependencyInjection\\MockService::getFactoryMethod',
'arguments' => array(
'\\Drupal\\Tests\\service_container\\DependencyInjection\\MockService',
array(
NULL,
'bar',
),
),
'calls' => array(
array(
'setContainer',
array(
'@service_container',
),
),
),
);
$services['wrong_factory'] = array(
'class' => '\\Drupal\\service_container\\ServiceContainer\\ControllerInterface',
'factory_method' => 'getFactoryMethod',
);
$services['circular_dependency'] = array(
'class' => '\\Drupal\\Tests\\service_container\\DependencyInjection\\MockService',
'arguments' => array(
'@circular_dependency',
),
);
$services['service_parameter_not_exists'] = array(
'class' => '\\Drupal\\Tests\\service_container\\DependencyInjection\\MockService',
'arguments' => array(
'@service.provider',
'%not_exists%',
-1,
),
);
$services['service_dependency_not_exists'] = array(
'class' => '\\Drupal\\Tests\\service_container\\DependencyInjection\\MockService',
'arguments' => array(
'@service_not_exists',
'%some_config%',
),
);
return array(
'parameters' => $parameters,
'services' => $services,
'aliases' => array(
'service.provider_alias' => 'service.provider',
),
);
}
}