Completed
Pull Request — master (#204)
by Ryan
11:34
created

NPlusOneTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 0
cbo 8
dl 0
loc 63
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Copyright (c) 2015–2018 Alexandr Viniychuk <http://youshido.com>.
4
 * Copyright (c) 2015–2018 Portey Vasil <https://github.com/portey>.
5
 * Copyright (c) 2018 Ryan Parman <https://github.com/skyzyx>.
6
 * Copyright (c) 2018 Ashley Hutson <https://github.com/asheliahut>.
7
 * Copyright (c) 2015–2018 Contributors.
8
 *
9
 * http://opensource.org/licenses/MIT
10
 */
11
12
declare(strict_types=1);
13
/*
14
 * This file is a part of GraphQL project.
15
 *
16
 * @author Alexandr Viniychuk <[email protected]>
17
 * created: 2/5/17 12:23 PM
18
 */
19
20
namespace Youshido\Tests\Performance;
21
22
use Youshido\GraphQL\Execution\Processor;
23
use Youshido\GraphQL\Schema\Schema;
24
use Youshido\GraphQL\Type\ListType\ListType;
25
use Youshido\GraphQL\Type\Object\ObjectType;
26
use Youshido\GraphQL\Type\Scalar\IdType;
27
use Youshido\GraphQL\Type\Scalar\IntType;
28
use Youshido\GraphQL\Type\Scalar\StringType;
29
30
class NPlusOneTest extends \PHPUnit_Framework_TestCase
31
{
32
    public function testHigherResolver(): void
33
    {
34
        $authorType = new ObjectType([
35
            'name'   => 'Author',
36
            'fields' => [
37
                'id'   => new IdType(),
38
                'name' => new StringType(),
39
            ],
40
        ]);
41
42
        $postType = new ObjectType([
43
            'name'   => 'Post',
44
            'fields' => [
45
                'id'     => new IntType(),
46
                'title'  => new StringType(),
47
                'author' => $authorType,
48
            ],
49
        ]);
50
51
        $processor = new Processor(new Schema([
52
            'query' => new ObjectType([
53
                'fields' => [
54
                    'posts' => [
55
                        'type'    => new ListType($postType),
56
                        'resolve' => function ($source, $args, $info) {
57
                            return $this->getDataForPosts();
58
                        },
59
                    ],
60
                ],
61
            ]),
62
        ]));
63
64
        $data = $processor->processPayload('{ posts { id, title, author { id, name } } }')->getResponseData();
65
        $this->assertNotEmpty($data['data']['posts'][0]['author']);
66
    }
67
68
    private function getDataForPosts()
69
    {
70
        /**
71
         * We could make a DB request here, as a simplified version:
72
         * SELECT * FROM posts p LEFT JOIN authors a ON (a.id = p.author_id) LIMIT 10, 10.
73
         */
74
        $authors = [
75
            ['id' => 1, 'name' => 'John'],
76
            ['id' => 2, 'name' => 'Alex'],
77
            ['id' => 3, 'name' => 'Mike'],
78
        ];
79
        $posts = [];
80
81
        for ($i = 0; $i < 10; $i++) {
82
            $posts[] = [
83
                'id'     => $i + 1,
84
                'title'  => \sprintf('Post title $%s', $i),
85
                'author' => $authors[$i % 3],
86
            ];
87
        }
88
89
        return $posts;
90
    }
91
}
92