From 73bcbabbec2b884746b246e2cfc7e69fbf3be8b8 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 8 Apr 2015 15:43:22 +0200 Subject: [PATCH] tests: added ContainerBuilder::findByType tests --- src/DI/ContainerBuilder.php | 8 ++-- tests/DI/Container.getByType.phpt | 48 ++++++++++++++++++++++++ tests/DI/ContainerBuilder.getByType.phpt | 27 +++++-------- 3 files changed, 61 insertions(+), 22 deletions(-) create mode 100644 tests/DI/Container.getByType.phpt diff --git a/src/DI/ContainerBuilder.php b/src/DI/ContainerBuilder.php index 28fd67e23..f4dab8c62 100644 --- a/src/DI/ContainerBuilder.php +++ b/src/DI/ContainerBuilder.php @@ -120,8 +120,8 @@ public function hasDefinition($name) /** - * @param string - * @param string + * @param string + * @param string */ public function addAlias($alias, $service) { @@ -206,7 +206,7 @@ public function getByType($class) /** * Gets the service names and definitions of the specified type. - * @param string + * @param string * @return ServiceDefinition[] */ public function findByType($class) @@ -489,7 +489,7 @@ private function checkCase($class) /** - * @param string[] + * @param string[] * @return self */ public function addExcludedClasses(array $classes) diff --git a/tests/DI/Container.getByType.phpt b/tests/DI/Container.getByType.phpt new file mode 100644 index 000000000..97323bf19 --- /dev/null +++ b/tests/DI/Container.getByType.phpt @@ -0,0 +1,48 @@ +addDefinition('one') + ->setClass('Service'); +$two = $builder->addDefinition('two') + ->setClass('Service2'); +$three = $builder->addDefinition('three') + ->setClass('Service2') + ->setAutowired(FALSE); + +$container = createContainer($builder); + +Assert::type( 'Service', $container->getByType('Service') ); +Assert::null( $container->getByType('unknown', FALSE) ); + +Assert::exception(function() use ($container) { + $container->getByType('unknown'); +}, 'Nette\DI\MissingServiceException', 'Service of type unknown not found.'); + +Assert::exception(function() use ($container) { + $container->getByType('Nette\Object'); +}, 'Nette\DI\MissingServiceException', 'Multiple services of type Nette\Object found: one, two, container.'); + + +Assert::same( array('one'), $container->findByType('Service') ); +Assert::same( array('two', 'three'), $container->findByType('Service2') ); +Assert::same( array(), $container->findByType('unknown') ); diff --git a/tests/DI/ContainerBuilder.getByType.phpt b/tests/DI/ContainerBuilder.getByType.phpt index 1a3c3e919..4b6f75464 100644 --- a/tests/DI/ContainerBuilder.getByType.phpt +++ b/tests/DI/ContainerBuilder.getByType.phpt @@ -1,7 +1,7 @@ addDefinition('three') ->setAutowired(FALSE); -// compile-time +Assert::null( $builder->getByType('\Service') ); + $builder->prepareClassList(); Assert::same( 'one', $builder->getByType('\Service') ); @@ -40,19 +41,9 @@ Assert::exception(function() use ($builder) { }, 'Nette\DI\ServiceCreationException', 'Multiple services of type Nette\Object found: one, two, container'); -$container = createContainer($builder); - -Assert::type( 'Service', $container->getByType('Service') ); -Assert::null( $container->getByType('unknown', FALSE) ); - -Assert::same( array('one'), $container->findByType('Service') ); -Assert::same( array('two', 'three'), $container->findByType('Service2') ); -Assert::same( array(), $container->findByType('unknown') ); - -Assert::exception(function() use ($container) { - $container->getByType('unknown'); -}, 'Nette\DI\MissingServiceException', 'Service of type unknown not found.'); - -Assert::exception(function() use ($container) { - $container->getByType('Nette\Object'); -}, 'Nette\DI\MissingServiceException', 'Multiple services of type Nette\Object found: one, two, container.'); +Assert::same( array('one' => $builder->getDefinition('one')), $builder->findByType('Service') ); +Assert::same( + array('two' => $builder->getDefinition('two'), 'three' => $builder->getDefinition('three')), + $builder->findByType('Service2') +); +Assert::same( array(), $builder->findByType('unknown') );