Skip to content

Commit 73bcbab

Browse files
committed
tests: added ContainerBuilder::findByType tests
1 parent 46a3ecc commit 73bcbab

File tree

3 files changed

+61
-22
lines changed

3 files changed

+61
-22
lines changed

src/DI/ContainerBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ public function hasDefinition($name)
120120

121121

122122
/**
123-
* @param string
124-
* @param string
123+
* @param string
124+
* @param string
125125
*/
126126
public function addAlias($alias, $service)
127127
{
@@ -206,7 +206,7 @@ public function getByType($class)
206206

207207
/**
208208
* Gets the service names and definitions of the specified type.
209-
* @param string
209+
* @param string
210210
* @return ServiceDefinition[]
211211
*/
212212
public function findByType($class)
@@ -489,7 +489,7 @@ private function checkCase($class)
489489

490490

491491
/**
492-
* @param string[]
492+
* @param string[]
493493
* @return self
494494
*/
495495
public function addExcludedClasses(array $classes)

tests/DI/Container.getByType.phpt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\DI\Container::getByType() and findByType()
5+
*/
6+
7+
use Nette\DI,
8+
Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
14+
class Service extends Nette\Object
15+
{
16+
}
17+
18+
class Service2 extends Nette\Object
19+
{
20+
}
21+
22+
23+
$builder = new DI\ContainerBuilder;
24+
$one = $builder->addDefinition('one')
25+
->setClass('Service');
26+
$two = $builder->addDefinition('two')
27+
->setClass('Service2');
28+
$three = $builder->addDefinition('three')
29+
->setClass('Service2')
30+
->setAutowired(FALSE);
31+
32+
$container = createContainer($builder);
33+
34+
Assert::type( 'Service', $container->getByType('Service') );
35+
Assert::null( $container->getByType('unknown', FALSE) );
36+
37+
Assert::exception(function() use ($container) {
38+
$container->getByType('unknown');
39+
}, 'Nette\DI\MissingServiceException', 'Service of type unknown not found.');
40+
41+
Assert::exception(function() use ($container) {
42+
$container->getByType('Nette\Object');
43+
}, 'Nette\DI\MissingServiceException', 'Multiple services of type Nette\Object found: one, two, container.');
44+
45+
46+
Assert::same( array('one'), $container->findByType('Service') );
47+
Assert::same( array('two', 'three'), $container->findByType('Service2') );
48+
Assert::same( array(), $container->findByType('unknown') );

tests/DI/ContainerBuilder.getByType.phpt

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* Test: Nette\DI\ContainerBuilder and Container: getByType()
4+
* Test: Nette\DI\ContainerBuilder::getByType() and findByType()
55
*/
66

77
use Nette\DI,
@@ -30,7 +30,8 @@ $three = $builder->addDefinition('three')
3030
->setAutowired(FALSE);
3131

3232

33-
// compile-time
33+
Assert::null( $builder->getByType('\Service') );
34+
3435
$builder->prepareClassList();
3536

3637
Assert::same( 'one', $builder->getByType('\Service') );
@@ -40,19 +41,9 @@ Assert::exception(function() use ($builder) {
4041
}, 'Nette\DI\ServiceCreationException', 'Multiple services of type Nette\Object found: one, two, container');
4142

4243

43-
$container = createContainer($builder);
44-
45-
Assert::type( 'Service', $container->getByType('Service') );
46-
Assert::null( $container->getByType('unknown', FALSE) );
47-
48-
Assert::same( array('one'), $container->findByType('Service') );
49-
Assert::same( array('two', 'three'), $container->findByType('Service2') );
50-
Assert::same( array(), $container->findByType('unknown') );
51-
52-
Assert::exception(function() use ($container) {
53-
$container->getByType('unknown');
54-
}, 'Nette\DI\MissingServiceException', 'Service of type unknown not found.');
55-
56-
Assert::exception(function() use ($container) {
57-
$container->getByType('Nette\Object');
58-
}, 'Nette\DI\MissingServiceException', 'Multiple services of type Nette\Object found: one, two, container.');
44+
Assert::same( array('one' => $builder->getDefinition('one')), $builder->findByType('Service') );
45+
Assert::same(
46+
array('two' => $builder->getDefinition('two'), 'three' => $builder->getDefinition('three')),
47+
$builder->findByType('Service2')
48+
);
49+
Assert::same( array(), $builder->findByType('unknown') );

0 commit comments

Comments
 (0)