diff --git a/test/functional/DataStore/DataStore/QueryTest/DateField/BaseTest.php b/test/functional/DataStore/DataStore/QueryTest/DateField/BaseTest.php new file mode 100644 index 0000000..3009aa6 --- /dev/null +++ b/test/functional/DataStore/DataStore/QueryTest/DateField/BaseTest.php @@ -0,0 +1,159 @@ + [ + $expectedRecord = [self::ID_NAME => 1, self::FIELD_NAME => $date = $this->format(new DateTimeImmutable())], + new EqNode(self::FIELD_NAME, $date), + [$expectedRecord], + ], + 'Do not equal to another date' => [ + [self::ID_NAME => 1, self::FIELD_NAME => $this->format(new DateTimeImmutable())], + new EqNode(self::FIELD_NAME, $this->format(new DateTimeImmutable('-1 day'))), + [], + ], +// 'Do not equal to ATOM date format' => [ +// [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())], +// new EqNode(self::FIELD_NAME, $date->format(DateTimeInterface::ATOM)), +// [], +// ], +// 'Do not equal to RFC822 date format' => [ +// [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())], +// new EqNode(self::FIELD_NAME, $date->format(DateTimeInterface::RFC822)), +// [], +// ], + + // Test greater than + 'Greater than test date is greater' => [ + [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())], + new GtNode(self::FIELD_NAME, $this->format($date->modify('+1 second'))), + [], + ], + 'Greater than test date is greater (short)' => [ + [self::ID_NAME => 1, self::FIELD_NAME => $this->shortFormat($date = new DateTimeImmutable())], + new GtNode(self::FIELD_NAME, $this->shortFormat($date->modify('+1 second'))), + [], + ], + 'Greater than test date is lower' => [ + $expectedRecord = [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())], + new GtNode(self::FIELD_NAME, $this->format($date->modify('-1 second'))), + [$expectedRecord], + ], + 'Greater than test date is same' => [ + [self::ID_NAME => 1, self::FIELD_NAME => $date = $this->format(new DateTimeImmutable())], + new GtNode(self::FIELD_NAME, $date), + [], + ], + 'Greater than test date is same (short)' => [ + [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable('2023-12-30 00:00:00'))], + new GtNode(self::FIELD_NAME, $this->format(new DateTimeImmutable('2024-12-30'))), + [], + ], + + // Test lower than + 'Lower than test date is greater' => [ + $expectedRecord = [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())], + new LtNode(self::FIELD_NAME, $this->format($date->modify('+1 second'))), + [$expectedRecord], + ], + 'Lower than test date is lower' => [ + [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())], + new LtNode(self::FIELD_NAME, $this->format($date->modify('-1 second'))), + [], + ], + 'Lower than test date is same' => [ + [self::ID_NAME => 1, self::FIELD_NAME => $date = $this->format(new DateTimeImmutable())], + new LtNode(self::FIELD_NAME, $date), + [], + ], + + // Test greater than or equals + 'Greater than or equal test date is greater' => [ + [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())], + new GeNode(self::FIELD_NAME, $this->format($date->modify('+1 second'))), + [], + ], + 'Greater than or equal test date is lower' => [ + $expectedRecord = [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())], + new GeNode(self::FIELD_NAME, $this->format($date->modify('-1 second'))), + [$expectedRecord], + ], + 'Greater than or equal test date is same' => [ + $expectedRecord = [self::ID_NAME => 1, self::FIELD_NAME => $date = $this->format(new DateTimeImmutable())], + new GeNode(self::FIELD_NAME, $date), + [$expectedRecord], + ], + 'Greater than or equal test date is same (short)' => [ + [self::ID_NAME => 1, self::FIELD_NAME => $this->format(new DateTimeImmutable('2023-12-30 00:00:00'))], + new GtNode(self::FIELD_NAME, $this->format(new DateTimeImmutable('2024-12-30'))), + [], + ], + + // Test lower than or equals + 'Lower than or equal test date is greater' => [ + $expectedRecord = [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())], + new LeNode(self::FIELD_NAME, $this->format($date->modify('+1 second'))), + [$expectedRecord], + ], + 'Lower than or equal test date is lower' => [ + [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())], + new LeNode(self::FIELD_NAME, $this->format($date->modify('-1 second'))), + [], + ], + 'Lower than or equal test date is same' => [ + $expectedRecord = [self::ID_NAME => 1, self::FIELD_NAME => $date = $this->format(new DateTimeImmutable())], + new LeNode(self::FIELD_NAME, $date), + [$expectedRecord], + ], + ]; + } + + /** + * @dataProvider filterDataProvider + */ + public function testFilter(array $record, AbstractQueryNode $queryNode, array $expectedRecords): void + { + $dataStore = $this->getDataStore(); + $dataStore->create($record); + + $query = new Query(); + $query->setQuery($queryNode); + $records = $dataStore->query($query); + + self::assertEquals($expectedRecords, $records); + } + + private function format(DateTimeInterface $dateTime): string + { + return $dateTime->format('Y-m-d H:i:s'); + } + + private function shortFormat(DateTimeInterface $dateTime): string + { + return $dateTime->format('Y-m-d'); + } + + abstract protected function getDataStore(): DataStoreInterface; +} \ No newline at end of file diff --git a/test/functional/DataStore/DataStore/QueryTest/DateField/CsvBaseTest.php b/test/functional/DataStore/DataStore/QueryTest/DateField/CsvBaseTest.php new file mode 100644 index 0000000..8a40da3 --- /dev/null +++ b/test/functional/DataStore/DataStore/QueryTest/DateField/CsvBaseTest.php @@ -0,0 +1,48 @@ +csvBase === null) { + $this->csvBase = $this->setUpCsvBase(); + } + return $this->csvBase; + } + + protected function tearDown(): void + { + parent::tearDown(); + $path = __DIR__ . '/' . self::TABLE_NAME . '.csv'; + if (file_exists($path)) { + unlink($path); + } + } + + private function setUpCsvBase(): CsvBase + { + $path = __DIR__ . '/' . self::TABLE_NAME . '.csv'; + if (file_exists($path)) { + unlink($path); + } + + $file = fopen($path, 'w'); + fputcsv($file, [self::ID_NAME, self::FIELD_NAME]); + fclose($file); + + $lockFactory = new LockFactory(new FlockStore()); + return new CsvBase($path, ',', $lockFactory->createLock($path)); + } +} diff --git a/test/functional/DataStore/DataStore/QueryTest/DateField/DbTableTest.php b/test/functional/DataStore/DataStore/QueryTest/DateField/DbTableTest.php new file mode 100644 index 0000000..397559c --- /dev/null +++ b/test/functional/DataStore/DataStore/QueryTest/DateField/DbTableTest.php @@ -0,0 +1,71 @@ +dbTable === null) { + $this->dbTable = $this->setUpDbTable(); + } + return $this->dbTable; + } + + protected function tearDown(): void + { + parent::tearDown(); + $this->getMysqlManager()->deleteTable(self::TABLE_NAME); + } + + private function setUpDbTable(): DbTable + { + $mysqlManager = $this->getMysqlManager(); + + if ($mysqlManager->hasTable(self::TABLE_NAME)) { + $mysqlManager->deleteTable(self::TABLE_NAME); + } + + $mysqlManager->createTable(self::TABLE_NAME, [ + self::ID_NAME => [ + 'field_type' => TableManagerMysql::TYPE_INTEGER, + ], + self::FIELD_NAME => [ + 'field_type' => TableManagerMysql::TYPE_DATETIME, + ], + ]); + $tableGateway = new TableGateway(self::TABLE_NAME, $this->getDbAdapter()); + + return new DbTable($tableGateway); + } + + private function getMysqlManager(): TableManagerMysql + { + if ($this->mysqlManager === null) { + $this->mysqlManager = new TableManagerMysql($this->getDbAdapter()); + } + return $this->mysqlManager; + } + + private function getDbAdapter(): Adapter + { + return $this->getContainer()->get('db'); + } +} diff --git a/test/functional/DataStore/DataStore/QueryTest/DateField/MemoryTest.php b/test/functional/DataStore/DataStore/QueryTest/DateField/MemoryTest.php new file mode 100644 index 0000000..91d61af --- /dev/null +++ b/test/functional/DataStore/DataStore/QueryTest/DateField/MemoryTest.php @@ -0,0 +1,22 @@ +dataStore === null) { + $this->dataStore = new Memory([self::ID_NAME, self::FIELD_NAME]); + } + return $this->dataStore; + } +}