Skip to content

Commit 0bca91f

Browse files
committed
Adding unit tests for DateTimeType + minor fix
1 parent 1e15957 commit 0bca91f

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/Types/DateTimeType.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
use DateTime;
88
use DateTimeImmutable;
9+
use Exception;
910
use GraphQL\Error\InvariantViolation;
1011
use GraphQL\Language\AST\Node;
1112
use GraphQL\Language\AST\StringValueNode;
12-
use GraphQL\Language\AST\ValueNode;
1313
use GraphQL\Type\Definition\ScalarType;
1414
use GraphQL\Utils\Utils;
1515

@@ -75,6 +75,7 @@ public function parseLiteral($valueNode, array $variables = null)
7575
return $valueNode->value;
7676
}
7777

78-
return null;
78+
// Intentionally without message, as all information already in wrapped Exception
79+
throw new Exception();
7980
}
8081
}

tests/Types/DateTimeTypeTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace TheCodingMachine\GraphQLite\Types;
4+
5+
use DateTimeImmutable;
6+
use Exception;
7+
use GraphQL\Error\InvariantViolation;
8+
use GraphQL\Language\AST\StringValueNode;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class DateTimeTypeTest extends TestCase
12+
{
13+
14+
public function testSerialize(): void
15+
{
16+
$dateTimeType = DateTimeType::getInstance();
17+
18+
$this->assertSame('2019-05-05T10:10:10+00:00', $dateTimeType->serialize(new DateTimeImmutable('2019-05-05T10:10:10+00:00')));
19+
20+
$this->expectException(InvariantViolation::class);
21+
$dateTimeType->serialize('foo');
22+
}
23+
24+
public function testParseLiteral(): void
25+
{
26+
$dateTimeType = DateTimeType::getInstance();
27+
28+
$this->assertSame('2019-05-05T10:10:10+00:00', $dateTimeType->parseLiteral(new StringValueNode(['value' => '2019-05-05T10:10:10+00:00'])));
29+
30+
$this->expectException(Exception::class);
31+
$dateTimeType->parseLiteral(null);
32+
33+
}
34+
35+
public function testParseValue(): void
36+
{
37+
$dateTimeType = DateTimeType::getInstance();
38+
39+
40+
$this->assertNull($dateTimeType->parseValue(null));
41+
}
42+
}

0 commit comments

Comments
 (0)