Completed
Pull Request — master (#214)
by Thomas
04:52
created

ContentBlockInterface::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Youshido\Tests\Issues\Issue193;
4
5
use Youshido\GraphQL\Config\Schema\SchemaConfig;
6
use Youshido\GraphQL\Execution\Processor;
7
use Youshido\GraphQL\Schema\AbstractSchema;
8
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType;
9
use Youshido\GraphQL\Type\NonNullType;
10
use Youshido\GraphQL\Type\Object\AbstractObjectType;
11
use Youshido\GraphQL\Type\Scalar\IntType;
12
use Youshido\GraphQL\Type\Scalar\StringType;
13
14
class Issue193Test extends \PHPUnit_Framework_TestCase
15
{
16
    public function testResolvedInterfacesShouldBeRegistered()
17
    {
18
        $schema    = new Issue193Schema();
19
        $processor = new Processor($schema);
20
21
        $processor->processPayload($this->getIntrospectionQuery(), []);
22
        $resp = $processor->getResponseData();
23
24
        $typeNames = array_map(function ($type) {
25
            return $type['name'];
26
        }, $resp['data']['__schema']['types']);
27
28
        $this->assertContains('ContentBlockInterface', $typeNames);
29
        $this->assertContains('Post', $typeNames);
30
    }
31
32
    private function getIntrospectionQuery()
33
    {
34
        return <<<TEXT
35
query IntrospectionQuery {
36
    __schema {
37
        types {
38
            kind
39
          	name
40
        }
41
    }
42
}
43
TEXT;
44
    }
45
}
46
47
class Issue193Schema extends AbstractSchema
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
48
{
49
    public function build(SchemaConfig $config)
50
    {
51
        $config->getQuery()->addField(
52
            'post',
53
            [
54
                'type' => new ContentBlockInterface(),
55
            ]
56
        );
57
    }
58
}
59
60
class PostType extends AbstractObjectType
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
61
{
62
63
    public function build($config)
64
    {
65
        $config->applyInterface(new ContentBlockInterface());
66
        $config->addFields([
67
            'title'      => new NonNullType(new StringType()),
68
            'summary'    => new StringType(),
69
            'likesCount' => new IntType(),
70
        ]);
71
    }
72
73
    public function getInterfaces()
74
    {
75
        return [new ContentBlockInterface()];
76
    }
77
}
78
79
class ContentBlockInterface extends AbstractInterfaceType
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
80
{
81
    public function build($config)
82
    {
83
        $config->addField('title', new NonNullType(new StringType()));
84
        $config->addField('summary', new StringType());
85
    }
86
87
    public function resolveType($object)
88
    {
89
        // since there's only one type right now this interface will always resolve PostType
90
        return new PostType();
91
    }
92
}