Completed
Push — master ( 3853ec...f78c46 )
by Adrien
07:49
created

ServerTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 92
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A providerQuery() 0 25 3
A resultToArray() 0 20 5
A testQuery() 0 25 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Api;
6
7
use Application\Api\Server;
8
use Application\Model\User;
9
use ApplicationTest\Traits\TestWithTransaction;
10
use GraphQL\Executor\ExecutionResult;
11
use PHPUnit\Framework\TestCase;
12
use Zend\Diactoros\ServerRequest;
13
use Zend\Expressive\Session\Session;
14
use Zend\Expressive\Session\SessionMiddleware;
15
16
class ServerTest extends TestCase
17
{
18
    use TestWithTransaction;
19
20
    /**
21
     * @dataProvider providerQuery
22
     *
23
     * @param null|string $user
24
     * @param ServerRequest $request
25
     * @param array $expected
26
     * @param null|callable $dataPreparator
27
     */
28
    public function testQuery(?string $user, ServerRequest $request, array $expected, ?callable $dataPreparator = null): void
29
    {
30
        User::setCurrent($this->getEntityManager()->getRepository(User::class)->getOneByLogin($user));
31
32
        if ($dataPreparator) {
33
            $dataPreparator($this->getEntityManager()->getConnection());
34
        }
35
36
        // Use this flag to easily debug API test issues
37
        $debug = false;
38
39
        // Configure server
40
        $server = new Server($debug);
41
42
        // Execute query
43
        $result = $server->execute($request);
44
45
        $actual = $this->resultToArray($result, $debug);
46
47
        if ($debug) {
0 ignored issues
show
introduced by
The condition $debug is always false.
Loading history...
48
            ve($actual);
49
            unset($actual['errors'][0]['trace']);
50
        }
51
52
        self::assertEquals($expected, $actual);
53
    }
54
55
    public function providerQuery(): array
56
    {
57
        $data = [];
58
        foreach (glob('tests/data/query/*.php') as $file) {
59
            $name = str_replace('-', ' ', basename($file, '.php'));
60
            $user = preg_replace('/\d/', '', explode(' ', $name)[0]);
61
            if ($user === 'anonymous') {
62
                $user = null;
63
            }
64
65
            $args = require $file;
66
67
            // Convert arg into request
68
            $request = new ServerRequest();
69
            $args[0] = $request
70
                ->withMethod('POST')
71
                ->withHeader('content-type', ['application/json'])
72
                ->withParsedBody($args[0])
73
                ->withAttribute(SessionMiddleware::SESSION_ATTRIBUTE, new Session([]));
74
75
            array_unshift($args, $user);
76
            $data[$name] = $args;
77
        }
78
79
        return $data;
80
    }
81
82
    /**
83
     * @param ExecutionResult|ExecutionResult[] $result
84
     * @param bool $debug
85
     *
86
     * @return array
87
     */
88
    private function resultToArray($result, bool $debug): array
89
    {
90
        $isSingle = !is_array($result);
91
        if ($isSingle) {
92
            $result = [$result];
93
        }
94
95
        foreach ($result as &$one) {
96
            $one = $one->toArray();
97
            if ($debug) {
98
                ve($one);
99
                unset($one['errors'][0]['trace']);
100
            }
101
        }
102
103
        if ($isSingle) {
104
            $result = reset($result);
105
        }
106
107
        return $result;
108
    }
109
}
110