FieldTest::testObjectFieldCreation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
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/12/16 7:14 PM
7
*/
8
9
namespace Youshido\Tests\Library\Field;
10
11
12
use Youshido\GraphQL\Config\Field\FieldConfig;
13
use Youshido\GraphQL\Execution\ResolveInfo;
14
use Youshido\GraphQL\Field\Field;
15
use Youshido\GraphQL\Field\InputField;
16
use Youshido\GraphQL\Type\Scalar\IdType;
17
use Youshido\GraphQL\Type\Scalar\IntType;
18
use Youshido\GraphQL\Type\Scalar\StringType;
19
use Youshido\GraphQL\Type\TypeMap;
20
use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidator;
21
use Youshido\Tests\DataProvider\TestField;
22
use Youshido\Tests\DataProvider\TestResolveInfo;
23
24
class FieldTest extends \PHPUnit_Framework_TestCase
25
{
26
27
    public function testInlineFieldCreation()
28
    {
29
        $field = new Field([
30
            'name' => 'id',
31
            'type' => new IdType()
32
        ]);
33
        $resolveInfo = TestResolveInfo::createTestResolveInfo($field);
34
        $this->assertEquals('id', $field->getName());
35
        $this->assertEquals(new IdType(), $field->getType());
36
        $this->assertEquals(null, $field->resolve('data', [], $resolveInfo));
37
38
        $fieldWithResolve = new Field([
39
            'name'    => 'title',
40
            'type'    => new StringType(),
41
            'resolve' => function ($value, array $args, ResolveInfo $info) {
42
                return $info->getReturnType()->serialize($value);
43
            }
44
        ]);
45
        $resolveInfo = TestResolveInfo::createTestResolveInfo($fieldWithResolve);
46
        $this->assertEquals('true', $fieldWithResolve->resolve(true, [], $resolveInfo), 'Resolve bool to string');
47
48
        $fieldWithResolve->setType(new IntType());
49
        $this->assertEquals(new StringType(), $fieldWithResolve->getType()->getName());
50
51
    }
52
53
    public function testObjectFieldCreation()
54
    {
55
        $field = new TestField();
56
        $resolveInfo = TestResolveInfo::createTestResolveInfo($field);
57
58
        $this->assertEquals('test', $field->getName());
59
        $this->assertEquals('description', $field->getDescription());
60
        $this->assertEquals(new IntType(), $field->getType());
61
        $this->assertEquals('test', $field->resolve('test', [], $resolveInfo));
62
    }
63
64
    public function testArgumentsTrait()
65
    {
66
        $testField = new TestField();
67
        $this->assertFalse($testField->hasArguments());
68
69
        $testField->addArgument(new InputField(['name' => 'id', 'type' => new IntType()]));
70
        $this->assertEquals([
71
            'id' => new InputField(['name' => 'id', 'type' => new IntType()])
72
        ], $testField->getArguments());
73
74
        $testField->addArguments([
75
            new InputField(['name' => 'name', 'type' => new StringType()])
76
        ]);
77
        $this->assertEquals([
78
            'id'   => new InputField(['name' => 'id', 'type' => new IntType()]),
79
            'name' => new InputField(['name' => 'name', 'type' => new StringType()]),
80
        ], $testField->getArguments());
81
82
        $testField->removeArgument('name');
83
        $this->assertFalse($testField->hasArgument('name'));
84
    }
85
86
    /**
87
     * @param $fieldConfig
88
     *
89
     * @dataProvider invalidFieldProvider
90
     * @expectedException Youshido\GraphQL\Exception\ConfigurationException
91
     */
92
    public function testInvalidFieldParams($fieldConfig)
93
    {
94
        $field = new Field($fieldConfig);
95
        ConfigValidator::getInstance()->assertValidConfig($field->getConfig());
96
    }
97
98
    public function invalidFieldProvider()
99
    {
100
        return [
101
            [
102
                [
103
                    'name' => 'id',
104
                    'type' => 'invalid type'
105
                ]
106
            ],
107
            [
108
                [
109
                    'type' => TypeMap::TYPE_FLOAT
110
                ]
111
            ]
112
        ];
113
    }
114
115
}
116