TypeValidationRuleTest::simpleRulesProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
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 10:37 PM
7
*/
8
9
namespace Youshido\Tests\Library\Validator;
10
11
12
use Youshido\GraphQL\Field\Field;
13
use Youshido\GraphQL\Type\Scalar\StringType;
14
use Youshido\GraphQL\Type\TypeMap;
15
use Youshido\GraphQL\Type\TypeService;
16
use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidator;
17
use Youshido\GraphQL\Validator\ConfigValidator\Rules\TypeValidationRule;
18
use Youshido\Tests\DataProvider\TestInputField;
19
use Youshido\Tests\DataProvider\TestInputObjectType;
20
use Youshido\Tests\DataProvider\TestObjectType;
21
22
class TypeValidationRuleTest extends \PHPUnit_Framework_TestCase
23
{
24
25
    /**
26
     * @var TypeValidationRule
27
     */
28
    protected $rule;
29
30
    protected function setUp()
31
    {
32
        $this->rule = new TypeValidationRule(ConfigValidator::getInstance());
33
    }
34
35
36
    /**
37
     * @param      $ruleInfo
38
     * @param      $data
39
     * @param bool $isValid
40
     *
41
     * @dataProvider simpleRulesProvider
42
     */
43
    public function testSimpleRules($ruleInfo, $data, $isValid = true)
44
    {
45
        $this->assertEquals($isValid, $this->rule->validate($data, $ruleInfo));
46
    }
47
48
    public function simpleRulesProvider()
49
    {
50
        return [
51
            [TypeService::TYPE_ARRAY_OF_FIELDS_CONFIG, ["fieldName" => new StringType()]],
52
53
            [TypeService::TYPE_ANY, null],
54
55
            [TypeService::TYPE_ANY_OBJECT, new StringType()],
56
            [TypeService::TYPE_ANY_OBJECT, null, false],
57
58
            [TypeService::TYPE_CALLABLE, function () { }],
59
            [TypeService::TYPE_CALLABLE, null, false],
60
61
            [TypeService::TYPE_BOOLEAN, true],
62
            [TypeService::TYPE_BOOLEAN, false],
63
            [TypeService::TYPE_BOOLEAN, null, false],
64
65
            [TypeService::TYPE_ARRAY, []],
66
            [TypeService::TYPE_ARRAY, null, false],
67
68
            [TypeService::TYPE_OBJECT_TYPE, new TestObjectType()],
69
            [TypeService::TYPE_OBJECT_TYPE, new StringType(), false],
70
71
            [TypeService::TYPE_ARRAY_OF_FIELDS_CONFIG, ["fieldName" => TypeMap::TYPE_STRING]],
72
            [TypeService::TYPE_ARRAY_OF_FIELDS_CONFIG, [new Field(['name' => 'id', 'type' => new StringType()])]],
73
            [TypeService::TYPE_ARRAY_OF_FIELDS_CONFIG, [], false],
74
75
            [null, null, false],
76
            ['invalid rule', null, false],
77
        ];
78
    }
79
80
    /**
81
     * @param      $ruleInfo
82
     * @param      $data
83
     * @param bool $isValid
84
     *
85
     * @dataProvider complexRuleProvider
86
     */
87
    public function testComplexRules($ruleInfo, $data, $isValid = true)
88
    {
89
        $this->assertEquals($isValid, $this->rule->validate($data, $ruleInfo));
90
    }
91
92
    public static function complexRuleProvider()
93
    {
94
        return [
95
            [TypeService::TYPE_OBJECT_INPUT_TYPE, new TestInputObjectType()],
96
            [TypeService::TYPE_OBJECT_INPUT_TYPE, new StringType(), false],
97
98
            [TypeService::TYPE_ARRAY_OF_INPUT_FIELDS, [new TestInputObjectType(), new TestInputField()]],
99
            [TypeService::TYPE_ARRAY_OF_INPUT_FIELDS, [new StringType()]],
100
            [TypeService::TYPE_ARRAY_OF_INPUT_FIELDS, [['type' => TypeMap::TYPE_STRING]]],
101
            [TypeService::TYPE_ARRAY_OF_INPUT_FIELDS, [[]], false],
102
            [TypeService::TYPE_ARRAY_OF_INPUT_FIELDS, new StringType(), false],
103
104
            [TypeService::TYPE_ARRAY_OF_OBJECT_TYPES, [new TestObjectType()]],
105
            [TypeService::TYPE_ARRAY_OF_OBJECT_TYPES, [], false],
106
107
        ];
108
    }
109
110
}
111