11<?php
22
3-
43namespace DotTest \AnnotatedServices ;
54
6-
75use Doctrine \Common \Annotations \Reader ;
6+ use Dot \AnnotatedServices \Annotation \Inject ;
87use Dot \AnnotatedServices \Exception \RuntimeException ;
98use Dot \AnnotatedServices \Factory \AnnotatedServiceFactory as Subject ;
10- use DotTest \AnnotatedServices \Stubs \TestService ;
119use PHPUnit \Framework \TestCase ;
1210use Psr \Container \ContainerInterface ;
11+ use ReflectionMethod ;
12+ use ReflectionClass ;
1313
1414/**
1515 * Class AnnotatedServiceFactoryTest
@@ -29,7 +29,10 @@ public function setUp(): void
2929
3030 $ this ->container = $ this ->createMock (ContainerInterface::class);
3131 $ this ->annotationReader = $ this ->createMock (Reader::class);
32- $ this ->subject = $ this ->createPartialMock (Subject::class, ['createAnnotationReader ' ]);
32+ $ this ->subject = $ this ->createPartialMock (Subject::class, [
33+ 'createAnnotationReader ' ,
34+ 'getReflectionClass '
35+ ]);
3336 }
3437
3538 public function testThrowsExceptionClassNotFound ()
@@ -42,11 +45,74 @@ public function testThrowsExceptionClassNotFound()
4245 $ this ->subject ->__invoke ($ this ->container , $ requestedName );
4346 }
4447
45- public function testReturnsSimpleService ()
48+ public function testReturnServiceWithNoDependencies ()
4649 {
47- $ requestedName = TestService::class;
50+ $ requestedName = 'TestService ' ;
51+ $ this ->getMockBuilder ($ requestedName )->allowMockingUnknownTypes ()->getMock ();
52+ $ refClass = $ this ->createMock (ReflectionClass::class);
53+
54+ $ refClass ->method ('getConstructor ' )->willReturn (null );
55+ $ refClass ->method ('getMethods ' )->willReturn ([]);
56+
57+ $ this ->annotationReader ->method ('getMethodAnnotation ' )->willReturn (null );
58+ $ this ->subject
59+ ->method ('createAnnotationReader ' )
60+ ->willReturn ($ this ->annotationReader );
61+ $ this ->subject ->method ('getReflectionClass ' )->willReturn ($ refClass );
62+
4863 $ object = $ this ->subject ->__invoke ($ this ->container , $ requestedName );
4964
5065 $ this ->assertInstanceOf ($ requestedName , $ object );
5166 }
67+
68+ public function testThrowsExceptionAnnotationNotFound ()
69+ {
70+ $ requestedName = 'TestService ' ;
71+ $ this ->getMockBuilder ($ requestedName )->allowMockingUnknownTypes ()->getMock ();
72+ $ refClass = $ this ->createMock (ReflectionClass::class);
73+ $ refConstructor = $ this ->createMock (ReflectionMethod::class);
74+
75+ $ refClass ->method ('getConstructor ' )->willReturn ($ refConstructor );
76+ $ refConstructor ->method ('getNumberOfRequiredParameters ' )->willReturn (100 );
77+
78+ $ this ->annotationReader ->method ('getMethodAnnotation ' )->willReturn (null );
79+ $ this ->subject
80+ ->method ('createAnnotationReader ' )
81+ ->willReturn ($ this ->annotationReader );
82+ $ this ->subject ->method ('getReflectionClass ' )->willReturn ($ refClass );
83+
84+ $ this ->expectException (RuntimeException::class);
85+ $ this ->expectExceptionMessage (RuntimeException::annotationNotFound (
86+ Inject::class,
87+ $ requestedName ,
88+ get_class ($ this ->subject ),
89+ )->getMessage ());
90+
91+ $ this ->subject ->__invoke ($ this ->container , $ requestedName );
92+ }
93+
94+ public function testReturnService ()
95+ {
96+ $ requestedName = 'TestService ' ;
97+ $ this ->getMockBuilder ($ requestedName )->allowMockingUnknownTypes ()->getMock ();
98+ $ refClass = $ this ->createMock (ReflectionClass::class);
99+ $ refConstructor = $ this ->createMock (ReflectionMethod::class);
100+
101+ $ refClass ->method ('getConstructor ' )->willReturn ($ refConstructor );
102+ $ refClass ->method ('getMethods ' )->willReturn ([]);
103+ $ refConstructor ->method ('getNumberOfRequiredParameters ' )->willReturn (1 );
104+
105+ $ inject = new Inject (['test ' ]);
106+ $ this ->annotationReader ->method ('getMethodAnnotation ' )->willReturn ($ inject );
107+
108+ $ this ->subject
109+ ->method ('createAnnotationReader ' )
110+ ->willReturn ($ this ->annotationReader );
111+ $ this ->subject ->method ('getReflectionClass ' )->willReturn ($ refClass );
112+
113+ $ service = $ this ->subject ->__invoke ($ this ->container , $ requestedName );
114+
115+ $ this ->assertInstanceOf ($ requestedName , $ service );
116+ }
52117}
118+
0 commit comments