Completed
Pull Request — master (#63)
by David
02:03
created

EndToEndTest.php$0 ➔ testEndToEnd()   A

Complexity

Conditions 1

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 9.0254
c 0
b 0
f 0
cc 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace TheCodingMachine\GraphQL\Controllers\Integration;
4
5
use function class_exists;
6
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
7
use Exception;
8
use GraphQL\Error\Debug;
9
use GraphQL\GraphQL;
10
use GraphQL\Type\Definition\InputType;
11
use Mouf\Picotainer\Picotainer;
12
use PhpParser\Comment\Doc;
13
use PHPUnit\Framework\TestCase;
14
use PHPUnit\Util\Type;
15
use function print_r;
16
use Psr\Container\ContainerInterface;
17
use Psr\Container\NotFoundExceptionInterface;
18
use Symfony\Component\Cache\Simple\ArrayCache;
19
use TheCodingMachine\GraphQL\Controllers\AnnotationReader;
20
use TheCodingMachine\GraphQL\Controllers\ControllerQueryProviderFactory;
21
use TheCodingMachine\GraphQL\Controllers\Fixtures\Integration\Models\Contact;
22
use TheCodingMachine\GraphQL\Controllers\GlobControllerQueryProvider;
23
use TheCodingMachine\GraphQL\Controllers\HydratorInterface;
24
use TheCodingMachine\GraphQL\Controllers\Hydrators\FactoryHydrator;
25
use TheCodingMachine\GraphQL\Controllers\InputTypeGenerator;
26
use TheCodingMachine\GraphQL\Controllers\Mappers\GlobTypeMapper;
27
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapper;
28
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface;
29
use TheCodingMachine\GraphQL\Controllers\Mappers\TypeMapperInterface;
30
use TheCodingMachine\GraphQL\Controllers\NamingStrategy;
31
use TheCodingMachine\GraphQL\Controllers\NamingStrategyInterface;
32
use TheCodingMachine\GraphQL\Controllers\QueryProviderInterface;
33
use TheCodingMachine\GraphQL\Controllers\Containers\BasicAutoWiringContainer;
34
use TheCodingMachine\GraphQL\Controllers\Containers\EmptyContainer;
35
use TheCodingMachine\GraphQL\Controllers\Reflection\CachedDocBlockFactory;
36
use TheCodingMachine\GraphQL\Controllers\Schema;
37
use TheCodingMachine\GraphQL\Controllers\Security\AuthenticationServiceInterface;
38
use TheCodingMachine\GraphQL\Controllers\Security\AuthorizationServiceInterface;
39
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService;
40
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService;
41
use TheCodingMachine\GraphQL\Controllers\TypeGenerator;
42
43
class EndToEndTest extends TestCase
44
{
45
    /**
46
     * @var ContainerInterface
47
     */
48
    private $mainContainer;
49
50
    public function setUp()
51
    {
52
        $this->mainContainer = new Picotainer([
53
            Schema::class => function(ContainerInterface $container) {
54
                return new Schema($container->get(QueryProviderInterface::class), $container->get(RecursiveTypeMapperInterface::class));
55
            },
56
            QueryProviderInterface::class => function(ContainerInterface $container) {
57
                return new GlobControllerQueryProvider('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration\\Controllers', $container->get(ControllerQueryProviderFactory::class),
58
                    $container->get(RecursiveTypeMapperInterface::class), $container->get(BasicAutoWiringContainer::class), new ArrayCache());
59
            },
60
            ControllerQueryProviderFactory::class => function(ContainerInterface $container) {
61
                return new ControllerQueryProviderFactory(
62
                    $container->get(AnnotationReader::class),
63
                    $container->get(HydratorInterface::class),
64
                    $container->get(AuthenticationServiceInterface::class),
65
                    $container->get(AuthorizationServiceInterface::class),
66
                    $container->get(BasicAutoWiringContainer::class),
67
                    $container->get(CachedDocBlockFactory::class)
68
                );
69
            },
70
            BasicAutoWiringContainer::class => function(ContainerInterface $container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

70
            BasicAutoWiringContainer::class => function(/** @scrutinizer ignore-unused */ ContainerInterface $container) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
                return new BasicAutoWiringContainer(new EmptyContainer());
72
            },
73
            AuthorizationServiceInterface::class => function(ContainerInterface $container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

73
            AuthorizationServiceInterface::class => function(/** @scrutinizer ignore-unused */ ContainerInterface $container) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
                return new VoidAuthorizationService();
75
            },
76
            AuthenticationServiceInterface::class => function(ContainerInterface $container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

76
            AuthenticationServiceInterface::class => function(/** @scrutinizer ignore-unused */ ContainerInterface $container) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
                return new VoidAuthenticationService();
78
            },
79
            RecursiveTypeMapperInterface::class => function(ContainerInterface $container) {
80
                return new RecursiveTypeMapper($container->get(TypeMapperInterface::class), $container->get(NamingStrategyInterface::class), new ArrayCache());
81
            },
82
            TypeMapperInterface::class => function(ContainerInterface $container) {
83
                return new GlobTypeMapper('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration\\Types',
84
                    $container->get(TypeGenerator::class),
85
                    $container->get(InputTypeGenerator::class),
86
                    $container->get(BasicAutoWiringContainer::class),
87
                    $container->get(AnnotationReader::class),
88
                    $container->get(NamingStrategyInterface::class),
89
                    new ArrayCache()
90
                    );
91
            },
92
            TypeGenerator::class => function(ContainerInterface $container) {
93
                return new TypeGenerator(
94
                    $container->get(AnnotationReader::class),
95
                    $container->get(ControllerQueryProviderFactory::class),
96
                    $container->get(NamingStrategyInterface::class)
97
                );
98
            },
99
            InputTypeGenerator::class => function(ContainerInterface $container) {
100
                return new InputTypeGenerator(
101
                    $container->get(AnnotationReader::class),
102
                    $container->get(ControllerQueryProviderFactory::class),
103
                    $container->get(NamingStrategyInterface::class),
104
                    $container->get(HydratorInterface::class)
105
                );
106
            },
107
            AnnotationReader::class => function(ContainerInterface $container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

107
            AnnotationReader::class => function(/** @scrutinizer ignore-unused */ ContainerInterface $container) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
                return new AnnotationReader(new DoctrineAnnotationReader());
109
            },
110
            HydratorInterface::class => function(ContainerInterface $container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

110
            HydratorInterface::class => function(/** @scrutinizer ignore-unused */ ContainerInterface $container) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
                return new FactoryHydrator();
112
            },
113
            NamingStrategyInterface::class => function() {
114
                return new NamingStrategy();
115
            },
116
            CachedDocBlockFactory::class => function() {
117
                return new CachedDocBlockFactory(new ArrayCache());
118
            }
119
        ]);
120
    }
121
122
    public function testEndToEnd()
123
    {
124
        /**
125
         * @var Schema $schema
126
         */
127
        $schema = $this->mainContainer->get(Schema::class);
128
        $queryString = '
129
        query {
130
            getContacts {
131
                name
132
                ... on User {
133
                    email
134
                }
135
            }
136
        }
137
        ';
138
139
        $result = GraphQL::executeQuery(
140
            $schema,
141
            $queryString
142
        );
143
144
        $this->assertSame([
145
            'getContacts' => [
146
                [
147
                    'name' => 'Joe'
148
                ],
149
                [
150
                    'name' => 'Bill',
151
                    'email' => '[email protected]'
152
                ]
153
154
            ]
155
        ], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
156
157
        // Let's redo this to test cache.
158
        $result = GraphQL::executeQuery(
159
            $schema,
160
            $queryString
161
        );
162
163
        $this->assertSame([
164
            'getContacts' => [
165
                [
166
                    'name' => 'Joe'
167
                ],
168
                [
169
                    'name' => 'Bill',
170
                    'email' => '[email protected]'
171
                ]
172
173
            ]
174
        ], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
175
    }
176
177
    public function testEndToEndInputType()
178
    {
179
        /**
180
         * @var Schema $schema
181
         */
182
        $schema = $this->mainContainer->get(Schema::class);
183
        $queryString = '
184
        mutation {
185
          saveContact(
186
            contact: {
187
                name: "foo"
188
                relations: [
189
                    {
190
                        name: "bar"
191
                    }
192
                ]
193
            }
194
          ) {
195
            name,
196
            relations {
197
              name
198
            }
199
          }
200
        }
201
        ';
202
203
        $result = GraphQL::executeQuery(
204
            $schema,
205
            $queryString
206
        );
207
208
        $this->assertSame([
209
            'saveContact' => [
210
                'name' => 'foo',
211
                'relations' => [
212
                    [
213
                        'name' => 'bar'
214
                    ]
215
                ]
216
            ]
217
        ], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
218
    }
219
}
220