Completed
Push — master ( 5fafbf...34c1d6 )
by Portey
02:57
created

ScalarTypeTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 9
Bugs 5 Features 3
Metric Value
wmc 10
c 9
b 5
f 3
lcom 1
cbo 3
dl 0
loc 46
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A checkDescription() 0 4 1
A checkSerialization() 0 4 1
A testTypeName() 0 9 3
B testScalarPrimitives() 0 21 5
1
<?php
2
/*
3
* This file is a part of graphql-youshido project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 11/27/15 1:11 AM
7
*/
8
9
namespace Youshido\Tests;
10
11
use Youshido\GraphQL\Type\TypeInterface;
12
use Youshido\GraphQL\Type\TypeMap;
13
use Youshido\Tests\DataProvider\TestScalarDataProvider;
14
15
class ScalarTypeTest extends \PHPUnit_Framework_TestCase
16
{
17
18
    public function testTypeName()
19
    {
20
        foreach (TypeMap::getScalarTypes() as $typeName) {
21
            $className = 'Youshido\GraphQL\Type\Scalar\\' . ucfirst($typeName) . 'Type';
22
            /** @var TypeInterface $object */
23
            $object = new $className();
24
            $this->assertEquals(ucfirst($typeName) == 'Datetime' ? 'DateTime' : ucfirst($typeName), $object->getName());
25
        }
26
    }
27
28
    public function testScalarPrimitives()
29
    {
30
        foreach (TypeMap::getScalarTypes() as $typeName) {
31
            $className = 'Youshido\GraphQL\Type\Scalar\\' . (ucfirst($typeName) == 'Datetime' ? 'DateTime' : ucfirst($typeName)) . 'Type';
32
            /** @var TypeInterface $object */
33
            $object         = new $className();
34
            $testDataMethod = 'get' . $typeName . 'TestData';
35
            $this->checkDescription($object);
36
37
            foreach (call_user_func(['Youshido\Tests\DataProvider\TestScalarDataProvider', $testDataMethod]) as list($data, $serialized, $isValid)) {
38
39
                $this->checkSerialization($object, $data, $serialized);
40
41
                if ($isValid) {
42
                    $this->assertTrue($object->isValidValue($data), $typeName . ' validation for :' . serialize($data));
43
                } else {
44
                    $this->assertFalse($object->isValidValue($data), $typeName . ' validation for :' . serialize($data));
45
                }
46
            }
47
        }
48
    }
49
50
    private function checkDescription(TypeInterface $object)
51
    {
52
        $this->assertNotEmpty($object->getDescription());
53
    }
54
55
    private function checkSerialization(TypeInterface $object, $input, $expected)
56
    {
57
        $this->assertEquals($expected, $object->serialize($input), $object->getName() . ' serialize for: ' . serialize($input));
58
    }
59
60
}
61