|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is a part of GraphQL project. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
|
6
|
|
|
* created: 5/12/16 5:02 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Youshido\Tests\Library\Type; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Youshido\GraphQL\Field\Field; |
|
13
|
|
|
use Youshido\GraphQL\Type\InterfaceType\InterfaceType; |
|
14
|
|
|
use Youshido\GraphQL\Type\Object\ObjectType; |
|
15
|
|
|
use Youshido\GraphQL\Type\Scalar\StringType; |
|
16
|
|
|
use Youshido\Tests\DataProvider\TestExtendedType; |
|
17
|
|
|
use Youshido\Tests\DataProvider\TestInterfaceType; |
|
18
|
|
|
|
|
19
|
|
|
class InterfaceTypeTest extends \PHPUnit_Framework_TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
public function testInterfaceMethods() |
|
23
|
|
|
{ |
|
24
|
|
|
$interface = new TestInterfaceType(); |
|
25
|
|
|
$this->assertEquals($interface->getNamedType(), $interface->getType()); |
|
26
|
|
|
$this->assertEquals(['name' => new Field(['name' => 'name', 'type' => new StringType()])], |
|
27
|
|
|
$interface->getFields()); |
|
28
|
|
|
|
|
29
|
|
|
$object = new ObjectType([ |
|
30
|
|
|
'name' => 'Test', |
|
31
|
|
|
'fields' => [ |
|
32
|
|
|
'name' => new StringType() |
|
33
|
|
|
], |
|
34
|
|
|
'interfaces' => [$interface], |
|
35
|
|
|
]); |
|
36
|
|
|
$this->assertEquals([$interface], $object->getInterfaces()); |
|
37
|
|
|
$this->assertTrue($interface->isValidValue($object)); |
|
38
|
|
|
$this->assertFalse($interface->isValidValue('invalid object')); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertEquals($interface->serialize($object), $object); |
|
41
|
|
|
|
|
42
|
|
|
$interfaceType = new InterfaceType([ |
|
43
|
|
|
'name' => 'UserInterface', |
|
44
|
|
|
'fields' => [ |
|
45
|
|
|
'name' => new StringType() |
|
46
|
|
|
], |
|
47
|
|
|
'resolveType' => function ($object) { |
|
48
|
|
|
return $object; |
|
49
|
|
|
} |
|
50
|
|
|
]); |
|
51
|
|
|
$this->assertEquals('UserInterface', $interfaceType->getName()); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertEquals($object, $interfaceType->resolveType($object)); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertFalse($interfaceType->isValidValue($object)); |
|
56
|
|
|
$this->assertFalse($interfaceType->isValidValue('invalid object')); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testApplyInterface() |
|
60
|
|
|
{ |
|
61
|
|
|
$extendedType = new TestExtendedType(); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertArrayHasKey('ownField', $extendedType->getFields()); |
|
64
|
|
|
$this->assertArrayHasKey('name', $extendedType->getFields()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|