1111
1212namespace Cross \TestUtils \TestCase ;
1313
14+ use Cross \TestUtils \Exception \InvalidUsageException ;
15+ use Prophecy \Prophecy \ObjectProphecy ;
16+
1417/**
15- * Creates a service manager double with configured services.
18+ * Creates a service manager prophecy or double with configured services.
1619 *
1720 * @method \Prophecy\Prophecy\ObjectProphecy prophesize(string $classOrInterface)
1821 *
2124trait ContainerDoubleTrait
2225{
2326 /**
24- * Prohesizes a container interface double .
27+ * Prohesizes a container interface.
2528 *
2629 * Pass in services as iterable:
2730 * <pre>
@@ -36,21 +39,30 @@ trait ContainerDoubleTrait
3639 * ]
3740 * </pre>
3841 *
42+ * Passing the boolean value 'false' as service will cause the following:
43+ * - Calling has(service) will return false.
44+ * - Calling get(service) will throw an exception.
45+ *
3946 * @param iterable $services
4047 *
41- * @return object
48+ * @return ObjectProphecy
4249 */
43- public function createContainerDouble (iterable $ services = []): object
50+ public function createContainerProphecy (iterable $ services = []): ObjectProphecy
4451 {
45- /** @noinspection PhpUndefinedClassInspection */
46- /** @noinspection PhpUndefinedNamespaceInspection */
52+ if (!interface_exists (\Psr \Container \ContainerInterface::class)) {
53+ throw InvalidUsageException::fromTrait (
54+ __TRAIT__ ,
55+ get_class ($ this ),
56+ 'Cannot create container double. Interface %s does not exist. ' ,
57+ \Psr \Container \ContainerInterface::class
58+ );
59+ }
60+
4761 $ container = $ this ->prophesize (\Psr \Container \ContainerInterface::class);
4862
4963 foreach ($ services as $ name => $ spec ) {
5064 if (!is_array ($ spec )) {
51- $ container ->get ($ name )->willReturn ($ spec );
52- $ container ->has ($ name )->willReturn (true );
53- continue ;
65+ $ spec = ['service ' => $ spec ];
5466 }
5567
5668 $ countGet = $ spec ['count_get ' ] ?? $ spec [1 ] ?? 0 ;
@@ -59,20 +71,39 @@ public function createContainerDouble(iterable $services = []): object
5971
6072 /** @var \Prophecy\Prophecy\MethodProphecy $method */
6173 $ method = $ container ->get ($ name );
62- $ method ->willReturn ($ service );
74+
75+ if (false === $ service ) {
76+ $ ex = $ this ->prophesize (\Psr \Container \NotFoundExceptionInterface::class)->reveal ();
77+ $ method ->willThrow ($ ex );
78+ } else {
79+ $ method ->willReturn ($ service );
80+ }
6381
6482 if ($ countGet ) {
6583 $ method ->shouldBeCalledTimes ($ countGet );
6684 }
6785
6886 $ method = $ container ->has ($ name );
69- $ method ->willReturn (true );
87+ $ method ->willReturn (false !== $ service );
7088
7189 if ($ countHas ) {
7290 $ method ->shouldBeCalledTimes ($ countHas );
7391 }
7492 }
7593
76- return $ container ->reveal ();
94+ return $ container ;
95+ }
96+
97+ /**
98+ * Creates a container interface double.
99+ *
100+ * @param iterable $services The services the container should provide
101+ * see {@link createContainerProphecy()}
102+ *
103+ * @return object The revealed container double
104+ */
105+ public function createContainerDouble (iterable $ services = []): object
106+ {
107+ return $ this ->createContainerProphecy ($ services )->reveal ();
77108 }
78109}
0 commit comments