RequestTest::testMethods()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 9.376
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: 5/15/16 3:51 PM
7
*/
8
9
namespace Youshido\Tests\Parser;
10
11
12
use Youshido\GraphQL\Execution\Request;
13
use Youshido\GraphQL\Parser\Ast\Fragment;
14
use Youshido\GraphQL\Parser\Location;
15
16
class RequestTest extends \PHPUnit_Framework_TestCase
17
{
18
19
    public function testMethods()
20
    {
21
        $fragment1     = new Fragment('fragmentName1', 'test', [], [], new Location(1,1));
22
        $fragment2     = new Fragment('fragmentName2', 'test', [], [], new Location(1,1));
23
        $queriesData   = ['query1', 'query2'];
24
        $mutationsData = ['mutation1', 'mutation2'];
25
        $fragmentsData = [$fragment1];
26
        $variables     = [
27
            'page' => 2
28
        ];
29
30
        $request = new Request([
31
            'queries'   => $queriesData,
32
            'mutations' => $mutationsData,
33
            'fragments' => $fragmentsData,
34
        ]);
35
        $request->setVariables($variables);
36
37
        $this->assertEquals($queriesData, $request->getQueries());
38
        $this->assertEquals($mutationsData, $request->getMutations());
39
        $this->assertEquals($fragmentsData, $request->getFragments());
40
        $this->assertEquals($variables, $request->getVariables());
41
42
        $this->assertTrue($request->hasFragments());
43
        $this->assertTrue($request->hasMutations());
44
        $this->assertTrue($request->hasQueries());
45
46
        $this->assertTrue($request->hasVariable('page'));
47
        $this->assertEquals(2, $request->getVariable('page'));
48
49
        $request->addFragment($fragment2);
50
        $this->assertEquals($fragment2, $request->getFragment('fragmentName2'));
51
        $this->assertNull($request->getFragment('unknown fragment'));
52
    }
53
54
    public function testSetVariableParseJson()
55
    {
56
        $variables = '{"foo": "bar"}';
57
        $expectedVariableArray = [ 'foo' => 'bar' ];
58
59
        $request = new Request([], $variables);
0 ignored issues
show
Documentation introduced by
$variables is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
        $this->assertEquals($expectedVariableArray, $request->getVariables());
61
62
        $request = new Request();
63
        $request->setVariables($variables);
64
        $this->assertEquals($expectedVariableArray, $request->getVariables());
65
    }
66
67
}
68