NPlusOneTest::testHigherResolver()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 9.36
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*
3
* This file is a part of GraphQL project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 2/5/17 12:23 PM
7
*/
8
9
namespace Youshido\Tests\Performance;
10
11
12
use Youshido\GraphQL\Execution\Processor;
13
use Youshido\GraphQL\Schema\Schema;
14
use Youshido\GraphQL\Type\ListType\ListType;
15
use Youshido\GraphQL\Type\Object\ObjectType;
16
use Youshido\GraphQL\Type\Scalar\IdType;
17
use Youshido\GraphQL\Type\Scalar\IntType;
18
use Youshido\GraphQL\Type\Scalar\StringType;
19
20
class NPlusOneTest extends \PHPUnit_Framework_TestCase
21
{
22
23
    private function getDataForPosts()
24
    {
25
        /**
26
         * We could make a DB request here, as a simplified version:
27
         * SELECT * FROM posts p LEFT JOIN authors a ON (a.id = p.author_id) LIMIT 10, 10
28
         */
29
        $authors = [
30
            ['id' => 1, 'name' => 'John'],
31
            ['id' => 2, 'name' => 'Alex'],
32
            ['id' => 3, 'name' => 'Mike'],
33
        ];
34
        $posts   = [];
35
        for ($i = 0; $i < 10; $i++) {
36
            $posts[] = [
37
                'id'     => $i + 1,
38
                'title'  => sprintf('Post title $%s', $i),
39
                'author' => $authors[$i % 3]
40
            ];
41
        }
42
43
        return $posts;
44
    }
45
46
    public function testHigherResolver()
47
    {
48
        $authorType = new ObjectType([
49
            'name'   => 'Author',
50
            'fields' => [
51
                'id'   => new IdType(),
52
                'name' => new StringType(),
53
            ]
54
        ]);
55
56
        $postType = new ObjectType([
57
            'name'   => 'Post',
58
            'fields' => [
59
                'id'     => new IntType(),
60
                'title'  => new StringType(),
61
                'author' => $authorType,
62
            ]
63
        ]);
64
65
        $processor = new Processor(new Schema([
66
            'query' => new ObjectType([
67
                'fields' => [
68
                    'posts' => [
69
                        'type'    => new ListType($postType),
70
                        'resolve' => function ($source, $args, $info) {
0 ignored issues
show
Unused Code introduced by
The parameter $source is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $info is not used and could be removed.

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

Loading history...
71
                            return $this->getDataForPosts();
72
                        }
73
                    ]
74
                ]
75
            ])
76
        ]));
77
78
        $data = $processor->processPayload('{ posts { id, title, author { id, name } } }')->getResponseData();
79
        $this->assertNotEmpty($data['data']['posts'][0]['author']);
80
    }
81
82
}