Completed
Push — master ( 0b7ef3...f36d8f )
by Alexandr
03:51
created

ScalarTypeTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 6
dl 0
loc 56
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B testScalarPrimitives() 0 34 5
A testDateTimeType() 0 5 1
A assertSerialization() 0 4 1
A assertParse() 0 4 1
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\Library\Type;
10
11
use Youshido\GraphQL\Type\Scalar\AbstractScalarType;
12
use Youshido\GraphQL\Type\Scalar\DateTimeType;
13
use Youshido\GraphQL\Type\Scalar\StringType;
14
use Youshido\GraphQL\Type\TypeFactory;
15
use Youshido\GraphQL\Type\TypeMap;
16
use Youshido\GraphQL\Type\TypeService;
17
use Youshido\Tests\DataProvider\TestScalarDataProvider;
18
19
class ScalarTypeTest extends \PHPUnit_Framework_TestCase
20
{
21
22
    public function testScalarPrimitives()
23
    {
24
        foreach (TypeFactory::getScalarTypesNames() as $typeName) {
25
            $scalarType     = TypeFactory::getScalarType($typeName);
26
            $testDataMethod = 'get' . $typeName . 'TestData';
27
28
            $this->assertNotEmpty($scalarType->getDescription());
29
            $this->assertEquals($scalarType->getKind(), TypeMap::KIND_SCALAR);
30
            $this->assertEquals($scalarType->isCompositeType(), false);
31
            $this->assertEquals(TypeService::isAbstractType($scalarType), false);
32
            $this->assertEquals($scalarType->getType(), $scalarType);
33
            $this->assertEquals($scalarType->getType(), $scalarType->getNamedType());
34
            $this->assertNull($scalarType->getConfig());
35
36
            foreach (call_user_func(['Youshido\Tests\DataProvider\TestScalarDataProvider', $testDataMethod]) as list($data, $serialized, $isValid)) {
37
38
                $this->assertSerialization($scalarType, $data, $serialized);
39
                $this->assertParse($scalarType, $data, $serialized);
40
41
                if ($isValid) {
42
                    $this->assertTrue($scalarType->isValidValue($data), $typeName . ' validation for :' . serialize($data));
43
                } else {
44
                    $this->assertFalse($scalarType->isValidValue($data), $typeName . ' validation for :' . serialize($data));
45
                }
46
            }
47
        }
48
        try {
49
            TypeFactory::getScalarType('invalid type');
50
        } catch (\Exception $e) {
51
            $this->assertEquals('Configuration problem with type invalid type', $e->getMessage());
52
        }
53
        $this->assertEquals('String', (string)new StringType());
54
55
    }
56
57
    public function testDateTimeType()
58
    {
59
        $dateType = new DateTimeType('Y/m/d H:i:s');
60
        $this->assertEquals('2016/05/31 12:00:00', $dateType->serialize(new \DateTime('2016-05-31 12:00pm')));
61
    }
62
63
64
    private function assertSerialization(AbstractScalarType $object, $input, $expected)
65
    {
66
        $this->assertEquals($expected, $object->serialize($input), $object->getName() . ' serialize for: ' . serialize($input));
67
    }
68
69
    private function assertParse(AbstractScalarType $object, $input, $expected)
70
    {
71
        $this->assertEquals($expected, $object->parseValue($input), $object->getName() . ' serialize for: ' . serialize($input));
72
    }
73
74
}
75