File tree Expand file tree Collapse file tree 5 files changed +61
-2
lines changed
tests/PHPStan/DependencyInjection/Nette Expand file tree Collapse file tree 5 files changed +61
-2
lines changed Original file line number Diff line number Diff line change @@ -58,6 +58,7 @@ parameters:
58
58
- 'PHPStan\Broker\ClassNotFoundException'
59
59
- 'PHPStan\Broker\FunctionNotFoundException'
60
60
- 'PHPStan\Broker\ConstantNotFoundException'
61
+ - 'PHPStan\DependencyInjection\MissingServiceException'
61
62
- 'PHPStan\Reflection\MissingMethodFromReflectionException'
62
63
- 'PHPStan\Reflection\MissingPropertyFromReflectionException'
63
64
- 'PHPStan\Reflection\MissingConstantFromReflectionException'
Original file line number Diff line number Diff line change @@ -10,13 +10,15 @@ public function hasService(string $serviceName): bool;
10
10
11
11
/**
12
12
* @return mixed
13
+ * @throws MissingServiceException
13
14
*/
14
15
public function getService (string $ serviceName );
15
16
16
17
/**
17
18
* @template T of object
18
19
* @param class-string<T> $className
19
20
* @return T
21
+ * @throws MissingServiceException
20
22
*/
21
23
public function getByType (string $ className );
22
24
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types = 1 );
2
+
3
+ namespace PHPStan \DependencyInjection ;
4
+
5
+ use Exception ;
6
+
7
+ final class MissingServiceException extends Exception
8
+ {
9
+
10
+ }
Original file line number Diff line number Diff line change 3
3
namespace PHPStan \DependencyInjection \Nette ;
4
4
5
5
use PHPStan \DependencyInjection \Container ;
6
+ use PHPStan \DependencyInjection \MissingServiceException ;
6
7
use PHPStan \DependencyInjection \ParameterNotFoundException ;
7
8
use function array_key_exists ;
8
9
use function array_keys ;
@@ -28,7 +29,11 @@ public function hasService(string $serviceName): bool
28
29
*/
29
30
public function getService (string $ serviceName )
30
31
{
31
- return $ this ->container ->getService ($ serviceName );
32
+ try {
33
+ return $ this ->container ->getService ($ serviceName );
34
+ } catch (\Nette \DI \MissingServiceException $ e ) {
35
+ throw new MissingServiceException ($ e ->getMessage (), previous: $ e );
36
+ }
32
37
}
33
38
34
39
/**
@@ -38,7 +43,11 @@ public function getService(string $serviceName)
38
43
*/
39
44
public function getByType (string $ className )
40
45
{
41
- return $ this ->container ->getByType ($ className );
46
+ try {
47
+ return $ this ->container ->getByType ($ className );
48
+ } catch (\Nette \DI \MissingServiceException $ e ) {
49
+ throw new MissingServiceException ($ e ->getMessage (), previous: $ e );
50
+ }
42
51
}
43
52
44
53
/**
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types = 1 );
2
+
3
+ namespace PHPStan \DependencyInjection \Nette ;
4
+
5
+ use PHPStan \DependencyInjection \MissingServiceException ;
6
+ use PHPStan \Testing \PHPStanTestCase ;
7
+ use PHPStan \TrinaryLogic ;
8
+ use PHPStan \Type \Php \ReflectionGetAttributesMethodReturnTypeExtension ;
9
+
10
+ class NetteContainerTest extends PHPStanTestCase
11
+ {
12
+
13
+ public function testGetServiceThrows (): void
14
+ {
15
+ $ container = self ::getContainer ();
16
+
17
+ $ this ->expectException (MissingServiceException::class);
18
+ $ container ->getService ('nonexistent ' );
19
+ }
20
+
21
+ public function testGetByTypeNotFoundThrows (): void
22
+ {
23
+ $ container = self ::getContainer ();
24
+
25
+ $ this ->expectException (MissingServiceException::class);
26
+ $ container ->getByType (TrinaryLogic::class);
27
+ }
28
+
29
+ public function testGetByTypeNotUniqueThrows (): void
30
+ {
31
+ $ container = self ::getContainer ();
32
+
33
+ $ this ->expectException (MissingServiceException::class);
34
+ $ container ->getByType (ReflectionGetAttributesMethodReturnTypeExtension::class);
35
+ }
36
+
37
+ }
You can’t perform that action at this time.
0 commit comments