1616
1717class ControllerQueryProviderTest extends TestCase
1818{
19- public function testQueryProvider ()
19+
20+ private $ testObjectType ;
21+ private $ typeMapper ;
22+ private $ hydrator ;
23+
24+ private function getTestObjectType ()
2025 {
21- $ controller = new TestController ();
22- $ reader = new AnnotationReader ();
26+ if ($ this ->testObjectType === null ) {
27+ $ this ->testObjectType = new ObjectType ([
28+ 'name ' => 'TestObject ' ,
29+ 'fields ' => [
30+ 'test ' => new StringType (),
31+ ],
32+ ]);
33+ }
34+ return $ this ->testObjectType ;
35+ }
2336
24- $ typeMapper = new class implements TypeMapperInterface {
25- public function mapClassToType (string $ className ): TypeInterface
26- {
27- if ($ className === TestObject::class) {
28- return new ObjectType ([
29- 'name ' => 'TestObject ' ,
30- 'fields ' => [
31- 'test ' => new StringType (),
32- ],
33- ]);
34- } else {
35- throw new \RuntimeException ('Unexpected type ' );
37+ private function getTypeMapper ()
38+ {
39+ if ($ this ->typeMapper === null ) {
40+ $ this ->typeMapper = new class ($ this ->getTestObjectType ()) implements TypeMapperInterface {
41+ /**
42+ * @var ObjectType
43+ */
44+ private $ testObjectType ;
45+
46+ public function __construct (ObjectType $ testObjectType )
47+ {
48+
49+ $ this ->testObjectType = $ testObjectType ;
50+ }
51+
52+ public function mapClassToType (string $ className ): TypeInterface
53+ {
54+ if ($ className === TestObject::class) {
55+ return $ this ->testObjectType ;
56+ } else {
57+ throw new \RuntimeException ('Unexpected type ' );
58+ }
3659 }
37- }
38- };
60+ };
61+ }
62+ return $ this ->typeMapper ;
63+ }
3964
40- $ hydrator = new class implements HydratorInterface {
41- public function hydrate (array $ data , TypeInterface $ type )
65+ private function getHydrator ()
66+ {
67+ if ($ this ->hydrator === null ) {
68+ $ this ->hydrator = new class implements HydratorInterface
4269 {
43- return new TestObject ($ data ['test ' ]);
44- }
45- };
70+ public function hydrate (array $ data , TypeInterface $ type )
71+ {
72+ return new TestObject ($ data ['test ' ]);
73+ }
74+ };
75+ }
76+ return $ this ->hydrator ;
77+ }
78+
79+ public function testQueryProvider ()
80+ {
81+ $ controller = new TestController ();
82+ $ reader = new AnnotationReader ();
4683
47- $ queryProvider = new ControllerQueryProvider ($ controller , $ reader , $ typeMapper , $ hydrator );
84+ $ queryProvider = new ControllerQueryProvider ($ controller , $ reader , $ this -> getTypeMapper () , $ this -> getHydrator () );
4885
4986 $ queries = $ queryProvider ->getQueries ();
5087
@@ -62,12 +99,7 @@ public function hydrate(array $data, TypeInterface $type)
6299 $ this ->assertInstanceOf (ObjectType::class, $ usersQuery ->getArgument ('list ' )->getType ()->getTypeOf ()->getItemType ()->getTypeOf ());
63100 $ this ->assertSame ('TestObject ' , $ usersQuery ->getArgument ('list ' )->getType ()->getTypeOf ()->getItemType ()->getTypeOf ()->getName ());
64101
65- $ mockResolveInfo = $ this ->getMockBuilder (ResolveInfo::class)
66- ->disableOriginalConstructor ()
67- ->disableOriginalClone ()
68- ->disableArgumentCloning ()
69- ->disallowMockingUnknownTypes ()
70- ->getMock ();
102+ $ mockResolveInfo = $ this ->createMock (ResolveInfo::class);
71103
72104 $ result = $ usersQuery ->resolve ('foo ' , ['int ' =>42 , 'string ' =>'foo ' , 'list ' =>[
73105 ['test ' =>42 ],
@@ -77,4 +109,26 @@ public function hydrate(array $data, TypeInterface $type)
77109 $ this ->assertInstanceOf (TestObject::class, $ result );
78110 $ this ->assertSame ('foo424212 ' , $ result ->getTest ());
79111 }
112+
113+ public function testMutations ()
114+ {
115+ $ controller = new TestController ();
116+ $ reader = new AnnotationReader ();
117+
118+ $ queryProvider = new ControllerQueryProvider ($ controller , $ reader , $ this ->getTypeMapper (), $ this ->getHydrator ());
119+
120+ $ mutations = $ queryProvider ->getMutations ();
121+
122+ $ this ->assertCount (1 , $ mutations );
123+ $ mutation = $ mutations [0 ];
124+ $ this ->assertSame ('mutation ' , $ mutation ->getName ());
125+
126+ $ mockResolveInfo = $ this ->createMock (ResolveInfo::class);
127+
128+ $ result = $ mutation ->resolve ('foo ' , ['testObject ' =>['test ' =>42 ]], $ mockResolveInfo );
129+
130+ $ this ->assertInstanceOf (TestObject::class, $ result );
131+ $ this ->assertEquals ('42 ' , $ result ->getTest ());
132+
133+ }
80134}
0 commit comments