1 | <?php |
||
2 | /** |
||
3 | * Created by PhpStorm. |
||
4 | * User: james |
||
5 | * Date: 23/07/2018 |
||
6 | * Time: 20:43 |
||
7 | */ |
||
8 | |||
9 | namespace Specter\Tests; |
||
10 | |||
11 | use PHPUnit\Framework\TestCase; |
||
12 | use Psr\Container\ContainerInterface; |
||
13 | use Specter\Container\Container; |
||
14 | use Specter\Container\Exceptions\DuplicateServiceException; |
||
15 | use Specter\Container\Exceptions\ServiceNotFoundException; |
||
16 | |||
17 | class ContainerTest extends TestCase |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * @covers \Specter\Container\Container |
||
22 | */ |
||
23 | public function testIsInstanceOfPSR11() |
||
24 | { |
||
25 | $container = new Container(); |
||
26 | |||
27 | $this->assertInstanceOf(ContainerInterface::class, $container); |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @covers \Specter\Container\Container::set() |
||
32 | * @covers \Specter\Container\Container::get() |
||
33 | * |
||
34 | * @throws \Specter\Container\Exceptions\DuplicateServiceException |
||
35 | * @throws \Specter\Container\Exceptions\ServiceNotFoundException |
||
36 | */ |
||
37 | public function testSet() |
||
38 | { |
||
39 | $container = new Container(); |
||
40 | |||
41 | $container->set('foo', false); |
||
42 | $container->set('baz', 132321); |
||
43 | $container->set('boz', 838.22); |
||
44 | |||
45 | $this->assertFalse($container->get('foo')); |
||
46 | $this->assertEquals(838.22, $container->get('boz')); |
||
47 | $this->assertEquals(132321, $container->get('baz')); |
||
48 | } |
||
49 | |||
50 | |||
51 | /** |
||
52 | * @covers \Specter\Container\Container::has() |
||
53 | * |
||
54 | * @throws \Specter\Container\Exceptions\DuplicateServiceException |
||
55 | */ |
||
56 | public function testHas() |
||
57 | { |
||
58 | $container = new Container(); |
||
59 | |||
60 | $container->set(\DateTime::class, new \DateTime()); |
||
61 | $container->set('foo', false); |
||
62 | |||
63 | $this->assertTrue($container->has('foo')); |
||
64 | $this->assertFalse($container->has('baz')); |
||
65 | $this->assertTrue($container->has(\DateTime::class)); |
||
66 | |||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @covers \Specter\Container\Exceptions\ServiceNotFoundException |
||
71 | * |
||
72 | * @throws ServiceNotFoundException |
||
73 | */ |
||
74 | public function testThrowsExceptionOnNotFoundService() |
||
75 | { |
||
76 | $container = new Container(); |
||
77 | |||
78 | $this->expectException(ServiceNotFoundException::class); |
||
79 | $this->expectExceptionMessage('the service foo was not found in the container'); |
||
80 | |||
81 | $container->get('foo', false); |
||
0 ignored issues
–
show
|
|||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @covers \Specter\Container\Exceptions\DuplicateServiceException |
||
86 | * |
||
87 | * @throws DuplicateServiceException |
||
88 | */ |
||
89 | public function testThrowsExceptionOnDuplicateService() |
||
90 | { |
||
91 | $container = new Container(); |
||
92 | |||
93 | $container->set(\DateTime::class, new \DateTime()); |
||
94 | |||
95 | $this->expectException(DuplicateServiceException::class); |
||
96 | $this->expectExceptionMessage(sprintf('the service %s already exists', \DateTime::class)); |
||
97 | |||
98 | $container->set(\DateTime::class, new \DateTime()); |
||
99 | } |
||
100 | } |
||
101 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.