ConfigTest::testEnumConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
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/11/16 10:41 PM
7
*/
8
9
namespace Youshido\Tests\Library\Config;
10
11
use Youshido\GraphQL\Type\Enum\EnumType;
12
use Youshido\GraphQL\Type\Object\ObjectType;
13
use Youshido\GraphQL\Type\Scalar\IdType;
14
use Youshido\GraphQL\Type\Scalar\IntType;
15
use Youshido\GraphQL\Type\TypeService;
16
use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidator;
17
use Youshido\Tests\DataProvider\TestConfig;
18
use Youshido\Tests\DataProvider\TestConfigExtraFields;
19
use Youshido\Tests\DataProvider\TestConfigInvalidRule;
20
21
class ConfigTest extends \PHPUnit_Framework_TestCase
22
{
23
24
    /**
25
     * @expectedException Youshido\GraphQL\Exception\ConfigurationException
26
     */
27
    public function testEmptyParams()
28
    {
29
        new TestConfig([]);
30
    }
31
32
    /**
33
     * @expectedException Youshido\GraphQL\Exception\ConfigurationException
34
     */
35
    public function testInvalidParams()
36
    {
37
        ConfigValidator::getInstance()->assertValidConfig(new TestConfig(['id' => 1]));
38
    }
39
40
    /**
41
     * @expectedException \Exception
42
     */
43
    public function testInvalidMethod()
44
    {
45
        $config = new TestConfig(['name' => 'test']);
46
        $config->doSomethingStrange();
0 ignored issues
show
Documentation Bug introduced by
The method doSomethingStrange does not exist on object<Youshido\Tests\DataProvider\TestConfig>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
47
    }
48
49
    public function testMethods()
50
    {
51
        $name  = 'Test';
52
        $rules = [
53
            'name'    => ['type' => TypeService::TYPE_ANY, 'required' => true],
54
            'resolve' => ['type' => TypeService::TYPE_CALLABLE, 'final' => true],
55
        ];
56
57
        $config = new TestConfig(['name' => $name]);
58
        $this->assertEquals($config->getName(), $name);
59
        $this->assertEquals($config->get('name'), $name);
60
        $this->assertEquals($config->get('non existing key'), null);
61
        $this->assertEquals($config->set('name', 'StrangeName'), $config);
62
        $this->assertEquals($config->get('name'), 'StrangeName');
63
        $this->assertEquals($config->get('non existing', 'default'), 'default');
64
        $this->assertEquals($config->isName(), 'StrangeName');
0 ignored issues
show
Documentation Bug introduced by
The method isName does not exist on object<Youshido\Tests\DataProvider\TestConfig>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
65
        $this->assertEquals($config->setName('StrangeName 2'), $config);
0 ignored issues
show
Documentation Bug introduced by
The method setName does not exist on object<Youshido\Tests\DataProvider\TestConfig>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
66
67
        $config->set('var', 'value');
68
        $this->assertEquals($config->getVar(), 'value');
0 ignored issues
show
Documentation Bug introduced by
The method getVar does not exist on object<Youshido\Tests\DataProvider\TestConfig>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
69
70
        $this->assertEquals($config->getRules(), $rules);
71
        $this->assertEquals($config->getContextRules(), $rules);
72
        $this->assertNull($config->getResolveFunction());
73
74
        $object = new ObjectType([
75
            'name'   => 'TestObject',
76
            'fields' => [
77
                'id' => [
78
                    'type' => new IntType()
79
                ]
80
            ]
81
        ]);
82
83
        $finalConfig = new TestConfig(['name' => $name . 'final', 'resolve' => function () { return []; }], $object, true);
84
        $this->assertEquals($finalConfig->getType(), null);
85
86
        $rules['resolve']['required'] = true;
87
        $this->assertEquals($finalConfig->getContextRules(), $rules);
88
89
        $this->assertNotNull($finalConfig->getResolveFunction());
90
91
        $configExtraFields = new TestConfigExtraFields([
92
            'name'       => 'Test',
93
            'extraField' => 'extraValue'
94
        ]);
95
        $this->assertEquals('extraValue', $configExtraFields->get('extraField'));
96
    }
97
98
    /**
99
     * @expectedException Youshido\GraphQL\Exception\ConfigurationException
100
     */
101
    public function testFinalRule()
102
    {
103
        ConfigValidator::getInstance()->assertValidConfig(new TestConfig(['name' => 'Test' . 'final'], null, true));
104
    }
105
106
    /**
107
     * @expectedException Youshido\GraphQL\Exception\ConfigurationException
108
     */
109
    public function testInvalidRule()
110
    {
111
        ConfigValidator::getInstance()->assertValidConfig(
112
            new TestConfigInvalidRule(['name' => 'Test', 'invalidRuleField' => 'test'], null, null)
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a boolean.

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...
113
        );
114
    }
115
116
    /**
117
     * @expectedException Youshido\GraphQL\Exception\ConfigurationException
118
     */
119
    public function testEnumConfig()
120
    {
121
        $enumType = new EnumType([
122
            'name'   => 'Status',
123
            'values' => [
124
                [
125
                    'name'   => 'ACTIVE',
126
                    'values' => 1
127
                ]
128
            ]
129
        ]);
130
        $object   = new ObjectType([
131
            'name' => 'Project',
132
            'fields' => [
133
                'id' => new IdType(),
134
                'status' => $enumType
135
            ]
136
        ]);
137
        ConfigValidator::getInstance()->assertValidConfig($object->getConfig());
138
    }
139
140
}
141