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:
5858 - 'PHPStan\Broker\ClassNotFoundException'
5959 - 'PHPStan\Broker\FunctionNotFoundException'
6060 - 'PHPStan\Broker\ConstantNotFoundException'
61+ - 'PHPStan\DependencyInjection\MissingServiceException'
6162 - 'PHPStan\Reflection\MissingMethodFromReflectionException'
6263 - 'PHPStan\Reflection\MissingPropertyFromReflectionException'
6364 - 'PHPStan\Reflection\MissingConstantFromReflectionException'
Original file line number Diff line number Diff line change @@ -10,13 +10,15 @@ public function hasService(string $serviceName): bool;
1010
1111 /**
1212 * @return mixed
13+ * @throws MissingServiceException
1314 */
1415 public function getService (string $ serviceName );
1516
1617 /**
1718 * @template T of object
1819 * @param class-string<T> $className
1920 * @return T
21+ * @throws MissingServiceException
2022 */
2123 public function getByType (string $ className );
2224
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 33namespace PHPStan \DependencyInjection \Nette ;
44
55use PHPStan \DependencyInjection \Container ;
6+ use PHPStan \DependencyInjection \MissingServiceException ;
67use PHPStan \DependencyInjection \ParameterNotFoundException ;
78use function array_key_exists ;
89use function array_keys ;
@@ -28,7 +29,11 @@ public function hasService(string $serviceName): bool
2829 */
2930 public function getService (string $ serviceName )
3031 {
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+ }
3237 }
3338
3439 /**
@@ -38,7 +43,11 @@ public function getService(string $serviceName)
3843 */
3944 public function getByType (string $ className )
4045 {
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+ }
4251 }
4352
4453 /**
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