Passed
Push — master ( e52344...df819b )
by Rafael
09:21
created

GraphQLHelperTrait.php$0 ➔ flattenExpectation()   B

Complexity

Conditions 6

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 6
rs 8
c 0
b 0
f 0
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Test;
12
13
use Symfony\Component\BrowserKit\Client;
14
use Symfony\Component\HttpFoundation\Request;
15
use Ynlo\GraphQLBundle\Model\ID;
16
use Ynlo\GraphQLBundle\Model\NodeInterface;
17
18
/**
19
 * @method Client getClient()
20
 */
21
trait GraphQLHelperTrait
22
{
23
    private static $endpoint;
24
25
    private static $query;
26
27
    /**
28
     * @param string $endpoint
29
     */
30
    protected static function endpoint($endpoint)
31
    {
32
        self::$endpoint = $endpoint;
33
    }
34
35
    /**
36
     * @param string $query
37
     * @param array  $variables
38
     */
39 21
    protected static function send($query, array $variables = [])
40
    {
41 21
        self::$query = ['query' => $query, 'variables' => $variables];
42 21
        self::getClient()->request(Request::METHOD_POST, self::$endpoint, [], [], [], json_encode(self::$query));
0 ignored issues
show
Bug Best Practice introduced by
The method Ynlo\GraphQLBundle\Test\...elperTrait::getClient() is not static, but was called statically. ( Ignorable by Annotation )

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

42
        self::/** @scrutinizer ignore-call */ 
43
              getClient()->request(Request::METHOD_POST, self::$endpoint, [], [], [], json_encode(self::$query));
Loading history...
43 21
    }
44
45
    /**
46
     * @param string               $nodeType
47
     * @param string|NodeInterface $databaseId
48
     *
49
     * @return string
50
     */
51 10
    protected static function encodeID($nodeType, $databaseId)
52
    {
53 10
        if ($databaseId instanceof NodeInterface) {
54 7
            $databaseId = $databaseId->getId();
55
        }
56
57 10
        return ID::encode($nodeType, $databaseId);
58
    }
59
60
    /**
61
     * @param string $globalID
62
     *
63
     * @return ID
64
     */
65
    protected static function decodeID($globalID): ID
66
    {
67
        return ID::createFromString($globalID);
68
    }
69
70
    /**
71
     * Print helpful debug information
72
     */
73
    protected static function debugInfo()
74
    {
75
        if (self::$query) {
76
            $query = self::$query['query'] ?? null;
77
78
            $type = 'QUERY';
79
            if (preg_match('/^\s*mutation/', $query)) {
80
                $type = 'MUTATION';
81
            }
82
83
            $variables = self::$query['variables'] ?? null;
84
85
            print_r("\n\n-------------- GraphQL $type ----------------\n\n");
86
            print_r($query ?? null);
87
            print_r("\n\n");
88
            print_r("------------------- VARIABLES-----------------------\n\n");
89
            print_r(json_encode($variables, JSON_PRETTY_PRINT));
90
            print_r("\n\n");
91
            print_r("-------------------- RESPONSE ----------------------\n\n");
92
            $content = self::getClient()->getResponse()->getContent();
0 ignored issues
show
Bug Best Practice introduced by
The method Ynlo\GraphQLBundle\Test\...elperTrait::getClient() is not static, but was called statically. ( Ignorable by Annotation )

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

92
            $content = self::/** @scrutinizer ignore-call */ getClient()->getResponse()->getContent();
Loading history...
93
            $json = @json_decode($content, true);
94
            if ($json) {
95
                print_r(json_encode($json, JSON_PRETTY_PRINT));
96
            } else {
97
                print_r($content);
98
            }
99
            print_r("\n\n");
100
            print_r("-----------------------------------------------------\n\n");
101
        }
102
    }
103
}
104