1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is a part of GraphQL project. |
4
|
|
|
* |
5
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
6
|
|
|
* created: 5/12/16 10:11 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Youshido\Tests\Schema; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Youshido\GraphQL\Schema\Schema; |
13
|
|
|
use Youshido\GraphQL\Type\NonNullType; |
14
|
|
|
use Youshido\GraphQL\Type\Object\ObjectType; |
15
|
|
|
use Youshido\GraphQL\Type\Scalar\IntType; |
16
|
|
|
use Youshido\GraphQL\Type\Scalar\StringType; |
17
|
|
|
use Youshido\Tests\DataProvider\TestEmptySchema; |
18
|
|
|
use Youshido\Tests\DataProvider\TestObjectType; |
19
|
|
|
use Youshido\Tests\DataProvider\TestSchema; |
20
|
|
|
|
21
|
|
|
class SchemaTest extends \PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
public function testInlineSchema() |
25
|
|
|
{ |
26
|
|
|
$queryType = new ObjectType([ |
|
|
|
|
27
|
|
|
'name' => 'RootQueryType', |
28
|
|
|
'fields' => [ |
29
|
|
|
'currentTime' => [ |
30
|
|
|
'type' => new StringType(), |
31
|
|
|
'resolve' => function ($value, $args, $type) { |
|
|
|
|
32
|
|
|
return 'May 5, 9:00am'; |
33
|
|
|
}, |
34
|
|
|
'args' => [ |
35
|
|
|
'gmt' => [ |
36
|
|
|
'type' => new IntType(), |
37
|
|
|
'default' => -5 |
38
|
|
|
], |
39
|
|
|
], |
40
|
|
|
] |
41
|
|
|
] |
42
|
|
|
]); |
43
|
|
|
// $this->assertEquals('May 5, 9:00am', $queryType->getField('currentTime')->resolve([], [],)); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testStandaloneEmptySchema() |
47
|
|
|
{ |
48
|
|
|
$schema = new TestEmptySchema(); |
49
|
|
|
$this->assertFalse($schema->getQueryType()->hasFields()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testStandaloneSchema() |
53
|
|
|
{ |
54
|
|
|
$schema = new TestSchema(); |
55
|
|
|
$this->assertTrue($schema->getQueryType()->hasFields()); |
56
|
|
|
$this->assertTrue($schema->getMutationType()->hasFields()); |
57
|
|
|
|
58
|
|
|
$this->assertEquals(1, count($schema->getMutationType()->getFields())); |
59
|
|
|
|
60
|
|
|
$schema->addMutationField('changeUser', ['type' => new TestObjectType(), 'resolve' => function () { }]); |
61
|
|
|
$this->assertEquals(2, count($schema->getMutationType()->getFields())); |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testSchemaWithoutClosuresSerializable() |
66
|
|
|
{ |
67
|
|
|
$schema = new TestEmptySchema(); |
68
|
|
|
$schema->getQueryType()->addField('randomInt', [ |
69
|
|
|
'type' => new NonNullType(new IntType()), |
70
|
|
|
'resolve' => 'rand', |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
$serialized = serialize($schema); |
74
|
|
|
/** @var Schema $unserialized */ |
75
|
|
|
$unserialized = unserialize($serialized); |
76
|
|
|
|
77
|
|
|
$this->assertTrue($unserialized->getQueryType()->hasFields()); |
78
|
|
|
$this->assertFalse($unserialized->getMutationType()->hasFields()); |
79
|
|
|
$this->assertEquals(1, count($unserialized->getQueryType()->getFields())); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.