Skip to content

Commit

Permalink
tests: added ContainerBuilder::findByType tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 8, 2015
1 parent 46a3ecc commit 73bcbab
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/DI/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public function hasDefinition($name)


/**
* @param string
* @param string
* @param string
* @param string
*/
public function addAlias($alias, $service)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -489,7 +489,7 @@ private function checkCase($class)


/**
* @param string[]
* @param string[]
* @return self
*/
public function addExcludedClasses(array $classes)
Expand Down
48 changes: 48 additions & 0 deletions tests/DI/Container.getByType.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* Test: Nette\DI\Container::getByType() and findByType()
*/

use Nette\DI,
Tester\Assert;


require __DIR__ . '/../bootstrap.php';


class Service extends Nette\Object
{
}

class Service2 extends Nette\Object
{
}


$builder = new DI\ContainerBuilder;
$one = $builder->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') );
27 changes: 9 additions & 18 deletions tests/DI/ContainerBuilder.getByType.phpt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* Test: Nette\DI\ContainerBuilder and Container: getByType()
* Test: Nette\DI\ContainerBuilder::getByType() and findByType()
*/

use Nette\DI,
Expand Down Expand Up @@ -30,7 +30,8 @@ $three = $builder->addDefinition('three')
->setAutowired(FALSE);


// compile-time
Assert::null( $builder->getByType('\Service') );

$builder->prepareClassList();

Assert::same( 'one', $builder->getByType('\Service') );
Expand All @@ -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') );

0 comments on commit 73bcbab

Please sign in to comment.