From d28c4b25381574cbee94a6fe69987cf783afc691 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 10 Mar 2026 08:19:21 +0000 Subject: [PATCH] feat: integrate Utopia\Query for usage metrics queries and remove legacy Query class --- composer.json | 5 +- composer.lock | 48 +++- src/Usage/Adapter.php | 18 +- src/Usage/Adapter/ClickHouse.php | 2 +- src/Usage/Adapter/Database.php | 4 +- src/Usage/Query.php | 306 ------------------------- src/Usage/Usage.php | 18 +- tests/Usage/Adapter/ClickHouseTest.php | 32 +-- tests/Usage/QueryTest.php | 237 ------------------- tests/Usage/UsageBase.php | 2 +- 10 files changed, 88 insertions(+), 584 deletions(-) delete mode 100644 src/Usage/Query.php delete mode 100644 tests/Usage/QueryTest.php diff --git a/composer.json b/composer.json index ffe0ab4..d9f85c8 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ "require": { "php": ">=8.0", "utopia-php/fetch": "0.5.*", - "utopia-php/database": "5.*" + "utopia-php/database": "5.*", + "utopia-php/query": "0.1.*" }, "require-dev": { "phpunit/phpunit": "^9.5", @@ -42,4 +43,4 @@ "tbachert/spi": false } } -} \ No newline at end of file +} diff --git a/composer.lock b/composer.lock index 1f968a4..05990a1 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ef73f18c3db269b5a8348d996c5fdd75", + "content-hash": "212d86b0fadf4671a550e439d21bea38", "packages": [ { "name": "brick/math", @@ -2382,6 +2382,52 @@ }, "time": "2026-02-26T08:42:40+00:00" }, + { + "name": "utopia-php/query", + "version": "0.1.1", + "source": { + "type": "git", + "url": "https://github.com/utopia-php/query.git", + "reference": "964a10ed3185490505f4c0062f2eb7b89287fb27" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/utopia-php/query/zipball/964a10ed3185490505f4c0062f2eb7b89287fb27", + "reference": "964a10ed3185490505f4c0062f2eb7b89287fb27", + "shasum": "" + }, + "require": { + "php": ">=8.4" + }, + "require-dev": { + "laravel/pint": "*", + "phpstan/phpstan": "*", + "phpunit/phpunit": "^12.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Utopia\\Query\\": "src/Query" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A simple library providing a query abstraction for filtering, ordering, and pagination", + "keywords": [ + "framework", + "php", + "query", + "upf", + "utopia" + ], + "support": { + "issues": "https://github.com/utopia-php/query/issues", + "source": "https://github.com/utopia-php/query/tree/0.1.1" + }, + "time": "2026-03-03T09:05:14+00:00" + }, { "name": "utopia-php/telemetry", "version": "0.2.0", diff --git a/src/Usage/Adapter.php b/src/Usage/Adapter.php index d001c8f..e49c288 100644 --- a/src/Usage/Adapter.php +++ b/src/Usage/Adapter.php @@ -102,7 +102,7 @@ abstract public function setBatch(array $metrics, int $batchSize = 1000): bool; /** * Get usage metrics by period * - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries * @return array */ abstract public function getByPeriod(string $metric, string $period, array $queries = []): array; @@ -110,7 +110,7 @@ abstract public function getByPeriod(string $metric, string $period, array $quer /** * Get usage metrics between dates * - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries * @return array */ abstract public function getBetweenDates(string $metric, string $startDate, string $endDate, array $queries = []): array; @@ -118,14 +118,14 @@ abstract public function getBetweenDates(string $metric, string $startDate, stri /** * Count usage metrics by period * - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries */ abstract public function countByPeriod(string $metric, string $period, array $queries = []): int; /** * Sum usage metrics by period * - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries */ abstract public function sumByPeriod(string $metric, string $period, array $queries = []): int; @@ -136,7 +136,7 @@ abstract public function sumByPeriod(string $metric, string $period, array $quer * Metrics not found will have a value of 0. * * @param array $metrics List of metric names - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries * @return array */ abstract public function sumByPeriodBatch(array $metrics, string $period, array $queries = []): array; @@ -147,7 +147,7 @@ abstract public function sumByPeriodBatch(array $metrics, string $period, array * Returns an associative array keyed by metric name with arrays of Metric objects as values. * * @param array $metrics List of metric names - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries * @return array> */ abstract public function getByPeriodBatch(array $metrics, string $period, array $queries = []): array; @@ -156,14 +156,14 @@ abstract public function getByPeriodBatch(array $metrics, string $period, array * Purge usage metrics matching the given queries. * When no queries are provided, all metrics are deleted. * - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries */ abstract public function purge(array $queries = []): bool; /** * Find metrics using Query objects. * - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries * @return array */ abstract public function find(array $queries = []): array; @@ -171,7 +171,7 @@ abstract public function find(array $queries = []): array; /** * Count metrics using Query objects. * - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries * @return int */ abstract public function count(array $queries = []): int; diff --git a/src/Usage/Adapter/ClickHouse.php b/src/Usage/Adapter/ClickHouse.php index 0dd4f21..8e243d0 100644 --- a/src/Usage/Adapter/ClickHouse.php +++ b/src/Usage/Adapter/ClickHouse.php @@ -3,7 +3,7 @@ namespace Utopia\Usage\Adapter; use Exception; -use Utopia\Usage\Query; +use Utopia\Query\Query; use Utopia\Fetch\Client; use Utopia\Usage\Metric; use Utopia\Usage\Usage; diff --git a/src/Usage/Adapter/Database.php b/src/Usage/Adapter/Database.php index 5803014..83b8cab 100644 --- a/src/Usage/Adapter/Database.php +++ b/src/Usage/Adapter/Database.php @@ -7,7 +7,7 @@ use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Query as DatabaseQuery; use Utopia\Usage\Metric; -use Utopia\Usage\Query; +use Utopia\Query\Query; use Utopia\Usage\Usage; class Database extends SQL @@ -202,7 +202,7 @@ public function setBatch(array $metrics, int $batchSize = 1000): bool } /** - * Convert Utopia\Usage\Query to Utopia\Database\Query for use with the Database class. + * Convert Utopia\Query\Query to Utopia\Database\Query for use with the Database class. * * @param array $queries * @return array diff --git a/src/Usage/Query.php b/src/Usage/Query.php deleted file mode 100644 index 5de8443..0000000 --- a/src/Usage/Query.php +++ /dev/null @@ -1,306 +0,0 @@ - - */ - protected array $values = []; - - /** - * Construct a new query object - * - * @param string $method - * @param string $attribute - * @param array $values - */ - public function __construct(string $method, string $attribute = '', array $values = []) - { - $this->method = $method; - $this->attribute = $attribute; - $this->values = $values; - } - - /** - * @return string - */ - public function getMethod(): string - { - return $this->method; - } - - /** - * @return string - */ - public function getAttribute(): string - { - return $this->attribute; - } - - /** - * @return array - */ - public function getValues(): array - { - return $this->values; - } - - /** - * @param mixed $default - * @return mixed - */ - public function getValue(mixed $default = null): mixed - { - return $this->values[0] ?? $default; - } - - /** - * Filter by equal condition - * - * @param string $attribute - * @param array> $values - * @return self - */ - public static function equal(string $attribute, array $values): self - { - return new self(self::TYPE_EQUAL, $attribute, $values); - } - - /** - * Filter by less than condition - * - * @param string $attribute - * @param mixed $value - * @return self - */ - public static function lessThan(string $attribute, mixed $value): self - { - return new self(self::TYPE_LESSER, $attribute, [$value]); - } - - /** - * Filter by less than or equal condition - */ - public static function lessThanEqual(string $attribute, mixed $value): self - { - return new self(self::TYPE_LESSER_EQUAL, $attribute, [$value]); - } - - /** - * Filter by greater than condition - * - * @param string $attribute - * @param mixed $value - * @return self - */ - public static function greaterThan(string $attribute, mixed $value): self - { - return new self(self::TYPE_GREATER, $attribute, [$value]); - } - - /** - * Filter by greater than or equal condition - */ - public static function greaterThanEqual(string $attribute, mixed $value): self - { - return new self(self::TYPE_GREATER_EQUAL, $attribute, [$value]); - } - - /** - * Filter by BETWEEN condition - * - * @param string $attribute - * @param mixed $start - * @param mixed $end - * @return self - */ - public static function between(string $attribute, mixed $start, mixed $end): self - { - return new self(self::TYPE_BETWEEN, $attribute, [$start, $end]); - } - - /** - * Filter by IN condition - * - * @param string $attribute - * @param array $values - * @return self - */ - public static function contains(string $attribute, array $values): self - { - return new self(self::TYPE_CONTAINS, $attribute, $values); - } - - /** - * Order by descending - * - * @param string $attribute - * @return self - */ - public static function orderDesc(string $attribute): self - { - return new self(self::TYPE_ORDER_DESC, $attribute); - } - - /** - * Order by ascending - * - * @param string $attribute - * @return self - */ - public static function orderAsc(string $attribute): self - { - return new self(self::TYPE_ORDER_ASC, $attribute); - } - - /** - * Limit number of results - * - * @param int $limit - * @return self - */ - public static function limit(int $limit): self - { - return new self(self::TYPE_LIMIT, '', [$limit]); - } - - /** - * Offset results - * - * @param int $offset - * @return self - */ - public static function offset(int $offset): self - { - return new self(self::TYPE_OFFSET, '', [$offset]); - } - - /** - * Parse query from JSON string - * - * @param string $query - * @return self - * @throws \Exception - */ - public static function parse(string $query): self - { - try { - $query = \json_decode($query, true, flags: JSON_THROW_ON_ERROR); - } catch (\JsonException $e) { - throw new \Exception('Invalid query: ' . $e->getMessage()); - } - - if (!\is_array($query)) { - throw new \Exception('Invalid query. Must be an array, got ' . \gettype($query)); - } - - return self::parseQuery($query); - } - - /** - * Parse an array of queries - * - * @param array $queries - * @return array - * @throws \Exception - */ - public static function parseQueries(array $queries): array - { - $parsed = []; - - foreach ($queries as $query) { - $parsed[] = self::parse($query); - } - - return $parsed; - } - - /** - * Parse query from array - * - * @param array $query - * @return self - * @throws \Exception - */ - protected static function parseQuery(array $query): self - { - $method = $query['method'] ?? ''; - $attribute = $query['attribute'] ?? ''; - $values = $query['values'] ?? []; - - if (!\is_string($method)) { - throw new \Exception('Invalid query method. Must be a string, got ' . \gettype($method)); - } - - if (!\is_string($attribute)) { - throw new \Exception('Invalid query attribute. Must be a string, got ' . \gettype($attribute)); - } - - if (!\is_array($values)) { - throw new \Exception('Invalid query values. Must be an array, got ' . \gettype($values)); - } - - return new self($method, $attribute, $values); - } - - /** - * Convert query to array - * - * @return array - */ - public function toArray(): array - { - $array = ['method' => $this->method]; - - if (!empty($this->attribute)) { - $array['attribute'] = $this->attribute; - } - - $array['values'] = $this->values; - - return $array; - } - - /** - * Convert query to JSON string - * - * @return string - * @throws \Exception - */ - public function toString(): string - { - try { - return \json_encode($this->toArray(), flags: JSON_THROW_ON_ERROR); - } catch (\JsonException $e) { - throw new \Exception('Invalid Json: ' . $e->getMessage()); - } - } -} diff --git a/src/Usage/Usage.php b/src/Usage/Usage.php index 5949d7d..f30fab4 100644 --- a/src/Usage/Usage.php +++ b/src/Usage/Usage.php @@ -124,7 +124,7 @@ public function setBatch(array $metrics, int $batchSize = 1000): bool /** * Get usage metrics by period. * - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries * @return array * * @throws \Exception @@ -137,7 +137,7 @@ public function getByPeriod(string $metric, string $period, array $queries = []) /** * Get usage metrics between dates. * - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries * @return array * * @throws \Exception @@ -150,7 +150,7 @@ public function getBetweenDates(string $metric, string $startDate, string $endDa /** * Count usage metrics by period. * - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries * * @throws \Exception */ @@ -162,7 +162,7 @@ public function countByPeriod(string $metric, string $period, array $queries = [ /** * Sum usage metric values by period. * - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries * * @throws \Exception */ @@ -177,7 +177,7 @@ public function sumByPeriod(string $metric, string $period, array $queries = []) * Collapses N sumByPeriod() calls into 1 query using WHERE metric IN (...). * * @param array $metrics List of metric names - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries * @return array * * @throws \Exception @@ -193,7 +193,7 @@ public function sumByPeriodBatch(array $metrics, string $period, array $queries * Collapses N getByPeriod() calls into 1 query using WHERE metric IN (...). * * @param array $metrics List of metric names - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries * @return array> * * @throws \Exception @@ -207,7 +207,7 @@ public function getByPeriodBatch(array $metrics, string $period, array $queries * Purge usage metrics matching the given queries. * When no queries are provided, all metrics are deleted. * - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries * @throws \Exception */ public function purge(array $queries = []): bool @@ -218,7 +218,7 @@ public function purge(array $queries = []): bool /** * Find metrics using Query objects. * - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries * @return array * @throws \Exception */ @@ -230,7 +230,7 @@ public function find(array $queries = []): array /** * Count metrics using Query objects. * - * @param array<\Utopia\Usage\Query> $queries + * @param array<\Utopia\Query\Query> $queries * @return int * @throws \Exception */ diff --git a/tests/Usage/Adapter/ClickHouseTest.php b/tests/Usage/Adapter/ClickHouseTest.php index fd67210..ff57e6e 100644 --- a/tests/Usage/Adapter/ClickHouseTest.php +++ b/tests/Usage/Adapter/ClickHouseTest.php @@ -297,7 +297,7 @@ public function testMetricsWithSpecialCharacters(): void $this->assertTrue($this->usage->incrementBatch([['metric' => 'special-metric', 'value' => 1, 'period' => '1h', 'tags' => ['s' => $specialVal]]])); $results = $this->usage->find([ - \Utopia\Usage\Query::equal('metric', ['special-metric']), + \Utopia\Query\Query::equal('metric', ['special-metric']), ]); $this->assertEquals(1, count($results)); @@ -325,51 +325,51 @@ public function testFind(): void // 1. Array Equal (IN) $results = $this->usage->find([ - \Utopia\Usage\Query::equal('metric', ['metric-A', 'metric-B']), + \Utopia\Query\Query::equal('metric', ['metric-A', 'metric-B']), ]); $this->assertGreaterThanOrEqual(2, count($results)); // 2. Scalar Equal $results = $this->usage->find([ - \Utopia\Usage\Query::equal('value', [20]), + \Utopia\Query\Query::equal('value', [20]), ]); $this->assertGreaterThanOrEqual(1, count($results)); $this->assertEquals(20, $results[0]->getValue()); // 3. Less Than $results = $this->usage->find([ - \Utopia\Usage\Query::lessThan('value', 20), - \Utopia\Usage\Query::equal('metric', ['metric-A']), + \Utopia\Query\Query::lessThan('value', 20), + \Utopia\Query\Query::equal('metric', ['metric-A']), ]); $this->assertGreaterThanOrEqual(1, count($results)); $this->assertEquals(10, $results[0]->getValue()); // 4. Greater Than $results = $this->usage->find([ - \Utopia\Usage\Query::greaterThan('value', 10), - \Utopia\Usage\Query::equal('metric', ['metric-B']), + \Utopia\Query\Query::greaterThan('value', 10), + \Utopia\Query\Query::equal('metric', ['metric-B']), ]); $this->assertGreaterThanOrEqual(1, count($results)); $this->assertEquals(20, $results[0]->getValue()); // 5. Between $results = $this->usage->find([ - \Utopia\Usage\Query::between('value', 5, 25), - \Utopia\Usage\Query::equal('metric', ['metric-A', 'metric-B']), + \Utopia\Query\Query::between('value', 5, 25), + \Utopia\Query\Query::equal('metric', ['metric-A', 'metric-B']), ]); $this->assertGreaterThanOrEqual(2, count($results)); // 6. Contains (IN alias for non-array input logic in Query class) $results = $this->usage->find([ - \Utopia\Usage\Query::contains('metric', ['metric-A']), + \Utopia\Query\Query::contains('metric', ['metric-A']), ]); $this->assertGreaterThanOrEqual(1, count($results)); // 7. Order Desc $results = $this->usage->find([ - \Utopia\Usage\Query::equal('metric', ['metric-A', 'metric-B']), - \Utopia\Usage\Query::orderDesc('value'), - \Utopia\Usage\Query::limit(2), + \Utopia\Query\Query::equal('metric', ['metric-A', 'metric-B']), + \Utopia\Query\Query::orderDesc('value'), + \Utopia\Query\Query::limit(2), ]); $this->assertGreaterThanOrEqual(2, count($results)); // First should be B (20), Second A (10) @@ -377,9 +377,9 @@ public function testFind(): void // 8. Order Asc $results = $this->usage->find([ - \Utopia\Usage\Query::equal('metric', ['metric-A', 'metric-B']), - \Utopia\Usage\Query::orderAsc('value'), - \Utopia\Usage\Query::limit(2), + \Utopia\Query\Query::equal('metric', ['metric-A', 'metric-B']), + \Utopia\Query\Query::orderAsc('value'), + \Utopia\Query\Query::limit(2), ]); $this->assertGreaterThanOrEqual(2, count($results)); // First should be A (10), Second B (20) diff --git a/tests/Usage/QueryTest.php b/tests/Usage/QueryTest.php deleted file mode 100644 index 13e1af2..0000000 --- a/tests/Usage/QueryTest.php +++ /dev/null @@ -1,237 +0,0 @@ -assertEquals(Query::TYPE_EQUAL, $query->getMethod()); - $this->assertEquals('userId', $query->getAttribute()); - $this->assertEquals(['123'], $query->getValues()); - - // Test lessThan - $query = Query::lessThan('time', '2024-01-01'); - $this->assertEquals(Query::TYPE_LESSER, $query->getMethod()); - $this->assertEquals('time', $query->getAttribute()); - $this->assertEquals(['2024-01-01'], $query->getValues()); - - // Test greaterThan - $query = Query::greaterThan('time', '2023-01-01'); - $this->assertEquals(Query::TYPE_GREATER, $query->getMethod()); - $this->assertEquals('time', $query->getAttribute()); - $this->assertEquals(['2023-01-01'], $query->getValues()); - - // Test greaterThanEqual - $query = Query::greaterThanEqual('time', '2023-01-01'); - $this->assertEquals(Query::TYPE_GREATER_EQUAL, $query->getMethod()); - $this->assertEquals('time', $query->getAttribute()); - $this->assertEquals(['2023-01-01'], $query->getValues()); - - // Test lessThanEqual - $query = Query::lessThanEqual('time', '2024-01-01'); - $this->assertEquals(Query::TYPE_LESSER_EQUAL, $query->getMethod()); - $this->assertEquals('time', $query->getAttribute()); - $this->assertEquals(['2024-01-01'], $query->getValues()); - - // Test between - $query = Query::between('time', '2023-01-01', '2024-01-01'); - $this->assertEquals(Query::TYPE_BETWEEN, $query->getMethod()); - $this->assertEquals('time', $query->getAttribute()); - $this->assertEquals(['2023-01-01', '2024-01-01'], $query->getValues()); - - // Test contains - $query = Query::contains('event', ['create', 'update', 'delete']); - $this->assertEquals(Query::TYPE_CONTAINS, $query->getMethod()); - $this->assertEquals('event', $query->getAttribute()); - $this->assertEquals(['create', 'update', 'delete'], $query->getValues()); - - // Test orderDesc - $query = Query::orderDesc('time'); - $this->assertEquals(Query::TYPE_ORDER_DESC, $query->getMethod()); - $this->assertEquals('time', $query->getAttribute()); - $this->assertEquals([], $query->getValues()); - - // Test orderAsc - $query = Query::orderAsc('userId'); - $this->assertEquals(Query::TYPE_ORDER_ASC, $query->getMethod()); - $this->assertEquals('userId', $query->getAttribute()); - $this->assertEquals([], $query->getValues()); - - // Test limit - $query = Query::limit(10); - $this->assertEquals(Query::TYPE_LIMIT, $query->getMethod()); - $this->assertEquals('', $query->getAttribute()); - $this->assertEquals([10], $query->getValues()); - - // Test offset - $query = Query::offset(5); - $this->assertEquals(Query::TYPE_OFFSET, $query->getMethod()); - $this->assertEquals('', $query->getAttribute()); - $this->assertEquals([5], $query->getValues()); - } - - /** - * Test Query parse and toString methods - */ - public function testQueryParseAndToString(): void - { - // Test parsing equal query - $json = '{"method":"equal","attribute":"userId","values":["123"]}'; - $query = Query::parse($json); - $this->assertEquals(Query::TYPE_EQUAL, $query->getMethod()); - $this->assertEquals('userId', $query->getAttribute()); - $this->assertEquals(['123'], $query->getValues()); - - // Test toString - $query = Query::equal('event', ['create']); - $json = $query->toString(); - $this->assertJson($json); - - $parsed = Query::parse($json); - $this->assertEquals(Query::TYPE_EQUAL, $parsed->getMethod()); - $this->assertEquals('event', $parsed->getAttribute()); - $this->assertEquals(['create'], $parsed->getValues()); - - // Test toArray - $array = $query->toArray(); - $this->assertArrayHasKey('method', $array); - $this->assertArrayHasKey('attribute', $array); - $this->assertArrayHasKey('values', $array); - $this->assertEquals(Query::TYPE_EQUAL, $array['method']); - $this->assertEquals('event', $array['attribute']); - $this->assertEquals(['create'], $array['values']); - } - - /** - * Test Query parseQueries method - */ - public function testQueryParseQueries(): void - { - $queries = [ - '{"method":"equal","attribute":"userId","values":["123"]}', - '{"method":"greaterThan","attribute":"time","values":["2023-01-01"]}', - '{"method":"limit","values":[10]}' - ]; - - $parsed = Query::parseQueries($queries); - - $this->assertCount(3, $parsed); - $this->assertInstanceOf(Query::class, $parsed[0]); - $this->assertInstanceOf(Query::class, $parsed[1]); - $this->assertInstanceOf(Query::class, $parsed[2]); - - $this->assertEquals(Query::TYPE_EQUAL, $parsed[0]->getMethod()); - $this->assertEquals(Query::TYPE_GREATER, $parsed[1]->getMethod()); - $this->assertEquals(Query::TYPE_LIMIT, $parsed[2]->getMethod()); - } - - /** - * Test Query getValue method - */ - public function testGetValue(): void - { - $query = Query::equal('userId', ['123']); - $this->assertEquals('123', $query->getValue()); - - $query = Query::limit(10); - $this->assertEquals(10, $query->getValue()); - - // Test with default value - $query = Query::orderAsc('time'); - $this->assertNull($query->getValue()); - $this->assertEquals('default', $query->getValue('default')); - } - - /** - * Test Query with empty attribute - */ - public function testQueryWithEmptyAttribute(): void - { - $query = Query::limit(25); - $this->assertEquals('', $query->getAttribute()); - $this->assertEquals([25], $query->getValues()); - - $query = Query::offset(10); - $this->assertEquals('', $query->getAttribute()); - $this->assertEquals([10], $query->getValues()); - } - - /** - * Test Query parse with invalid JSON - */ - public function testQueryParseInvalidJson(): void - { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Invalid query'); - - Query::parse('{"method":"equal","attribute":"userId"'); // Invalid JSON - } - - /** - * Test Query parse with non-array value - */ - public function testQueryParseNonArray(): void - { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Invalid query. Must be an array'); - - Query::parse('"string"'); - } - - /** - * Test Query parse with invalid method type - */ - public function testQueryParseInvalidMethodType(): void - { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Invalid query method. Must be a string'); - - Query::parse('{"method":["array"],"attribute":"test","values":[]}'); - } - - /** - * Test Query parse with invalid attribute type - */ - public function testQueryParseInvalidAttributeType(): void - { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Invalid query attribute. Must be a string'); - - Query::parse('{"method":"equal","attribute":123,"values":[]}'); - } - - /** - * Test Query parse with invalid values type - */ - public function testQueryParseInvalidValuesType(): void - { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Invalid query values. Must be an array'); - - Query::parse('{"method":"equal","attribute":"test","values":"string"}'); - } - - /** - * Test Query toString with complex values - */ - public function testQueryToStringWithComplexValues(): void - { - $query = Query::between('time', '2023-01-01', '2024-12-31'); - $json = $query->toString(); - $this->assertJson($json); - - $parsed = Query::parse($json); - $this->assertEquals(Query::TYPE_BETWEEN, $parsed->getMethod()); - $this->assertEquals('time', $parsed->getAttribute()); - $this->assertEquals(['2023-01-01', '2024-12-31'], $parsed->getValues()); - } -} diff --git a/tests/Usage/UsageBase.php b/tests/Usage/UsageBase.php index 99a8724..6094b37 100644 --- a/tests/Usage/UsageBase.php +++ b/tests/Usage/UsageBase.php @@ -3,7 +3,7 @@ namespace Utopia\Tests\Usage; use Utopia\Database\DateTime; -use Utopia\Usage\Query; +use Utopia\Query\Query; use Utopia\Usage\Usage; trait UsageBase