1
|
|
|
<?php |
2
|
|
|
namespace Skrz\Bundle\AutowiringBundle\Tests\DependencyInjection; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
use Skrz\Bundle\AutowiringBundle\DependencyInjection\ClassMultiMap; |
6
|
|
|
use Skrz\Bundle\AutowiringBundle\Exception\MultipleValuesException; |
7
|
|
|
use Skrz\Bundle\AutowiringBundle\Exception\NoValueException; |
8
|
|
|
use Skrz\Bundle\AutowiringBundle\Tests\DependencyInjection\ClassMultipleMapSource\SomeClass; |
9
|
|
|
use Skrz\Bundle\AutowiringBundle\Tests\DependencyInjection\ClassMultipleMapSource\SomeInterface; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
11
|
|
|
|
12
|
|
|
class ClassMultiMapTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
View Code Duplication |
public function testGetSingleForSimpleClass() |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
$map = new ClassMultiMap(new ContainerBuilder()); |
18
|
|
|
|
19
|
|
|
$map->put(SomeClass::class, "someValue"); |
20
|
|
|
$this->assertSame( |
21
|
|
|
"someValue", |
22
|
|
|
$map->getSingle(SomeClass::class) |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
View Code Duplication |
public function testGetSingleForSimpleForInterface() |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$map = new ClassMultiMap(new ContainerBuilder()); |
29
|
|
|
|
30
|
|
|
$map->put(SomeClass::class, "someValue"); |
31
|
|
|
$this->assertSame("someValue", $map->getSingle(SomeInterface::class)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testGetSingleForNonAddedClass() |
35
|
|
|
{ |
36
|
|
|
$this->expectException(NoValueException::class); |
37
|
|
|
|
38
|
|
|
$map = new ClassMultiMap(new ContainerBuilder()); |
39
|
|
|
$map->getSingle(SomeClass::class); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testGetSingleForClassAddedTwice() |
43
|
|
|
{ |
44
|
|
|
$this->expectException(MultipleValuesException::class); |
45
|
|
|
|
46
|
|
|
$map = new ClassMultiMap(new ContainerBuilder()); |
47
|
|
|
|
48
|
|
|
$map->put(SomeClass::class, "someValue"); |
49
|
|
|
$map->put(SomeClass::class, "someOtherValue"); |
50
|
|
|
$map->getSingle(SomeClass::class); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testGetMultiForNonAddedClass() |
54
|
|
|
{ |
55
|
|
|
$map = new ClassMultiMap(new ContainerBuilder()); |
56
|
|
|
|
57
|
|
|
$this->assertSame([], $map->getMulti(SomeClass::class)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testGetMultiForClassAddedTwice() |
61
|
|
|
{ |
62
|
|
|
$map = new ClassMultiMap(new ContainerBuilder()); |
63
|
|
|
|
64
|
|
|
$map->put(SomeClass::class, "someValue"); |
65
|
|
|
$map->put(SomeClass::class, "someOtherValue"); |
66
|
|
|
$this->assertSame(["someValue", "someOtherValue"], $map->getMulti(SomeClass::class)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.