From d431e751494f76a5050b189e78bf036b6fc41d2f Mon Sep 17 00:00:00 2001 From: Tom H Anderson Date: Thu, 25 Jan 2024 11:45:26 -0700 Subject: [PATCH 1/2] Date and DateImmutable values are now set to time T00:00:00+00:00 --- src/Type/Date.php | 2 +- src/Type/DateImmutable.php | 2 +- test/Feature/Type/DateImmutableTest.php | 31 ++++++++++++++++++++++--- test/Feature/Type/DateTest.php | 28 ++++++++++++++++++++-- 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/Type/Date.php b/src/Type/Date.php index 9323225..6948198 100644 --- a/src/Type/Date.php +++ b/src/Type/Date.php @@ -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 diff --git a/src/Type/DateImmutable.php b/src/Type/DateImmutable.php index 6a18a12..bac74e8 100644 --- a/src/Type/DateImmutable.php +++ b/src/Type/DateImmutable.php @@ -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 diff --git a/test/Feature/Type/DateImmutableTest.php b/test/Feature/Type/DateImmutableTest.php index f1deaf1..8c4adfd 100644 --- a/test/Feature/Type/DateImmutableTest.php +++ b/test/Feature/Type/DateImmutableTest.php @@ -78,9 +78,34 @@ public function testBetween(): void ]), ]); - $now = (new PHPDateTime())->format('Y-m-d'); - $query = '{ typetest ( filter: { testDateImmutable: { between: { from: "2022-08-06" to: "' . $now . '" } } } ) { edges { node { id testDateImmutable } } } }'; - $result = GraphQL::executeQuery($schema, $query); + $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']; diff --git a/test/Feature/Type/DateTest.php b/test/Feature/Type/DateTest.php index ae67db7..1672f29 100644 --- a/test/Feature/Type/DateTest.php +++ b/test/Feature/Type/DateTest.php @@ -80,8 +80,32 @@ 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 } } } }'; - $result = GraphQL::executeQuery($schema, $query); + $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']; From 86f842757d3d6a17a7073c00cb3b0bc2666cec24 Mon Sep 17 00:00:00 2001 From: Tom H Anderson Date: Thu, 25 Jan 2024 12:03:11 -0700 Subject: [PATCH 2/2] Added literal filters to date types --- test/Feature/Type/DateImmutableTest.php | 45 +++++++++++++++++++++++++ test/Feature/Type/DateTest.php | 45 +++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/test/Feature/Type/DateImmutableTest.php b/test/Feature/Type/DateImmutableTest.php index 8c4adfd..1aa1c3f 100644 --- a/test/Feature/Type/DateImmutableTest.php +++ b/test/Feature/Type/DateImmutableTest.php @@ -112,4 +112,49 @@ public function testBetween(): void $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 + } + } + } + } + '; + $result = GraphQL::executeQuery($schema, $query); + + $data = $result->toArray()['data']; + + $this->assertEquals(1, count($data['typetest']['edges'])); + $this->assertEquals(1, $data['typetest']['edges'][0]['node']['id']); + } } diff --git a/test/Feature/Type/DateTest.php b/test/Feature/Type/DateTest.php index 1672f29..144140f 100644 --- a/test/Feature/Type/DateTest.php +++ b/test/Feature/Type/DateTest.php @@ -112,4 +112,49 @@ public function testBetween(): void $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']; + + $this->assertEquals(1, count($data['typetest']['edges'])); + $this->assertEquals(1, $data['typetest']['edges'][0]['node']['id']); + } }