Completed
Push — master ( cbfed7...c39fb6 )
by Rafael
05:06
created

GraphQLHelperTrait::mutation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
17
/**
18
 * @method Client getClient()
19
 */
20
trait GraphQLHelperTrait
21
{
22
    private static $endpoint;
23
24
    /**
25
     * @param string $endpoint
26
     */
27
    protected static function endpoint($endpoint)
28
    {
29
        self::$endpoint = $endpoint;
30
    }
31
32
    /**
33
     * @param string     $name
34
     * @param array      $parameters (optional)
35
     * @param array|null $expected
36
     */
37 7
    protected static function query($name, array $parameters, array $expected = null)
38
    {
39 7
        if (null === $expected) {
40
            $expected = $parameters;
41
            $parameters = [];
42
        }
43 7
        self::graphqlQuery('query', $name, $parameters, $expected);
44 7
    }
45
46
    /**
47
     * @param string $name
48
     * @param array  $parameters
49
     * @param array  $expected
50
     */
51 7
    protected static function mutation($name, array $parameters = [], array $expected = [])
52
    {
53 7
        self::graphqlQuery('mutation', $name, $parameters, $expected);
54 7
    }
55
56
57
    /**
58
     * @param string $nodeType
59
     * @param string $databaseId
60
     *
61
     * @return string
62
     */
63 6
    protected static function encodeID($nodeType, $databaseId)
64
    {
65 6
        return ID::encode($nodeType, $databaseId);
66
    }
67
68
    /**
69
     * @param string $type
70
     * @param string $name
71
     * @param array  $parameters
72
     * @param array  $expected
73
     */
74 14
    private static function graphqlQuery($type, $name, array $parameters = [], array $expected = [])
75
    {
76 14
        $expectedStr = self::flattenExpectation($expected);
77
78 14
        $paramsStr = null;
79 14
        if ($parameters) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parameters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
80 14
            $paramsStr = self::flattenParameters($parameters);
81
        }
82
83
        $query = <<<GrahpQL
84 14
$type $name{
85 14
  $name$paramsStr{
86 14
    $expectedStr
87
  }
88
}
89
GrahpQL;
90 14
        $body = ['query' => $query];
91
92 14
        self::getClient()->request(Request::METHOD_POST, self::$endpoint, [], [], [], json_encode($body));
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
        self::/** @scrutinizer ignore-call */ 
93
              getClient()->request(Request::METHOD_POST, self::$endpoint, [], [], [], json_encode($body));
Loading history...
93 14
    }
94
95
    /**
96
     * @param array $parameters
97
     * @param array $wrappers
98
     *
99
     * @return string
100
     */
101 14
    private static function flattenParameters(array $parameters, $wrappers = ['(', ')'])
102
    {
103 14
        foreach ($parameters as $key => &$value) {
104 14
            if (\is_string($value)) {
105 11
                $value = "\"$value\"";
106
            }
107 14
            if (\is_bool($value)) {
108 2
                $value = $value ? 'true' : 'false';
109
            }
110 14
            if (\is_array($value)) {
111 10
                if (array_key_exists(0, $value)) {
112 2
                    $value = json_encode($value);
113
                } else {
114 8
                    $value = self::flattenParameters($value, ['{', '}']);
115
                }
116
            }
117 14
            $value = "$key: $value";
118
        }
119 14
        unset($value);
120
121 14
        list($start, $end) = $wrappers;
122
123 14
        return "$start ".implode(', ', $parameters)." $end";
124
    }
125
126
    /**
127
     * @param array $expectation
128
     *
129
     * @return string
130
     */
131 14
    private static function flattenExpectation(array $expectation)
132
    {
133
134
135 14
        $expectNormalized = [];
136 14
        foreach ($expectation as $path => $value) {
137 14
            if (\is_array($value)) {
138 11
                if (isset($value[0], $value[1])
139 11
                    && is_array($value[0])
140 11
                    && is_array($value[1])
141
                ) {
142 1
                    $params = self::flattenParameters($value[0]);
143 1
                    $expectation = self::flattenExpectation($value[1]);
144
145 1
                    $value = "$path $params {\n\t\t$expectation\n\t}";
146
                } else {
147 10
                    $value = $path." {\n\t\t".self::flattenExpectation($value)."\n\t}";
148
                }
149
            }
150 14
            $expectNormalized[] = $value;
151
        }
152
153 14
        return implode("\n\t", $expectNormalized);
154
    }
155
}
156