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