Skip to content

Commit

Permalink
Merge pull request #50 from TomHAnderson/hotfix/date-types
Browse files Browse the repository at this point in the history
Date and DateImmutable values are now set to time T00:00:00+00:00
  • Loading branch information
TomHAnderson authored Jan 25, 2024
2 parents 6a0ea1c + 86f8427 commit b47c7d9
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Type/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function parseValue(mixed $value): DateTime
throw new Error('Date format does not match Y-m-d e.g. 2004-02-12.');
}

return DateTime::createFromFormat('Y-m-d', $value);
return DateTime::createFromFormat(DateTime::ATOM, $value . 'T00:00:00+00:00');
}

public function serialize(mixed $value): string|null
Expand Down
2 changes: 1 addition & 1 deletion src/Type/DateImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function parseValue(mixed $value): DateTimeImmutable|false
throw new Error('Date format does not match Y-m-d e.g. 2004-02-12.');
}

return DateTimeImmutable::createFromFormat('Y-m-d', $value);
return DateTimeImmutable::createFromFormat(DateTimeImmutable::ATOM, $value . 'T00:00:00+00:00');
}

public function serialize(mixed $value): string|null
Expand Down
72 changes: 71 additions & 1 deletion test/Feature/Type/DateImmutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,78 @@ public function testBetween(): void
]),
]);

$now = (new PHPDateTime())->format('Y-m-d');
$query = '
query TypeTest($from: DateImmutable!, $to: DateImmutable!) {
typetest (
filter: {
testDateImmutable: {
between: { from: $from, to: $to }
}
}
) {
edges {
node {
id
testDateImmutable
}
}
}
}
';

$result = GraphQL::executeQuery(
schema: $schema,
source: $query,
variableValues: [
'from' => '2022-08-06',
'to' => $now,
],
);

$data = $result->toArray()['data'];

$this->assertEquals(1, count($data['typetest']['edges']));
$this->assertEquals(1, $data['typetest']['edges'][0]['node']['id']);
}

public function testBetweenLiteral(): void
{
$driver = new Driver($this->getEntityManager(), new Config(['group' => 'DataTypesTest']));
$schema = new Schema([
'query' => new ObjectType([
'name' => 'query',
'fields' => [
'typetest' => [
'type' => $driver->connection($driver->type(TypeTest::class)),
'args' => [
'filter' => $driver->filter(TypeTest::class),
],
'resolve' => $driver->resolve(TypeTest::class),
],
],
]),
]);

$now = (new PHPDateTime())->format('Y-m-d');
$query = '{ typetest ( filter: { testDateImmutable: { between: { from: "2022-08-06" to: "' . $now . '" } } } ) { edges { node { id testDateImmutable } } } }';
$query = '
{
typetest (
filter: {
testDateImmutable: {
between: { from: "2022-08-06" to: "' . $now . '" }
}
}
) {
edges {
node {
id
testDateImmutable
}
}
}
}
';
$result = GraphQL::executeQuery($schema, $query);

$data = $result->toArray()['data'];
Expand Down
71 changes: 70 additions & 1 deletion test/Feature/Type/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,76 @@ public function testBetween(): void
]);

$now = (new PHPDateTime())->format('Y-m-d');
$query = '{ typetest ( filter: { testDate: { between: { from: "2022-08-06" to: "' . $now . '" } } } ) { edges { node { id testDate } } } }';
$query = '
query TypeTest ($from: Date!, $to: Date!) {
typetest (
filter: {
testDate: {
between: { from: $from to: $to }
}
}
) {
edges {
node {
id
testDate
}
}
}
}
';
$result = GraphQL::executeQuery(
schema: $schema,
source: $query,
variableValues: [
'from' => '2022-08-06',
'to' => $now,
],
);

$data = $result->toArray()['data'];

$this->assertEquals(1, count($data['typetest']['edges']));
$this->assertEquals(1, $data['typetest']['edges'][0]['node']['id']);
}

public function testBetweenLiteral(): void
{
$driver = new Driver($this->getEntityManager(), new Config(['group' => 'DataTypesTest']));
$schema = new Schema([
'query' => new ObjectType([
'name' => 'query',
'fields' => [
'typetest' => [
'type' => $driver->connection($driver->type(TypeTest::class)),
'args' => [
'filter' => $driver->filter(TypeTest::class),
],
'resolve' => $driver->resolve(TypeTest::class),
],
],
]),
]);

$now = (new PHPDateTime())->format('Y-m-d');
$query = '
{
typetest (
filter: {
testDate: {
between: { from: "2022-08-06" to: "' . $now . '" }
}
}
) {
edges {
node {
id
testDate
}
}
}
}
';
$result = GraphQL::executeQuery($schema, $query);

$data = $result->toArray()['data'];
Expand Down

0 comments on commit b47c7d9

Please sign in to comment.