From c52aad39401992c6938eccce962ec763d2917a7f Mon Sep 17 00:00:00 2001 From: Marc Morera Date: Thu, 2 Jun 2022 11:09:44 +0200 Subject: [PATCH] Added some extra methods in model classes --- Model/Index.php | 12 ++++++++++- Query/Query.php | 33 ++++++++++++++++++++++++----- Tests/Model/IndexTest.php | 31 +++++++++++++++++++++++++++ Tests/Query/QueryTest.php | 44 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 6 deletions(-) diff --git a/Model/Index.php b/Model/Index.php index 45d2a20..1742fac 100644 --- a/Model/Index.php +++ b/Model/Index.php @@ -77,7 +77,7 @@ class Index implements HttpTransportable * * @var string[] */ - private $fields = []; + private $fields; /** * Metadata. @@ -199,6 +199,16 @@ public function getFields(): array return $this->fields; } + /** + * @param array $fields + * + * @return void + */ + public function withFields(array $fields) + { + $this->fields = $fields; + } + /** * Get Metadata. * diff --git a/Query/Query.php b/Query/Query.php index 234557d..5a3825c 100644 --- a/Query/Query.php +++ b/Query/Query.php @@ -257,11 +257,7 @@ public static function create( */ public static function createMatchAll(): self { - return static::create( - '', - self::DEFAULT_PAGE, - self::DEFAULT_SIZE - ); + return static::create(''); } /** @@ -342,6 +338,16 @@ public function getFields(): array return $this->fields; } + /** + * @param int $size + * + * @return void + */ + public function forceSize(int $size) + { + $this->size = $size; + } + /** * Filter universe by types. * @@ -867,6 +873,23 @@ public function getAggregation(string $aggregationName): ? Aggregation return $this->aggregations[$aggregationName] ?? null; } + /** + * @param string $aggregationField + * + * @return void + */ + public function deleteAggregationByField(string $aggregationField): void + { + $aggregationField = Item::getPathByField($aggregationField); + foreach ($this->aggregations as $key => $aggregation) { + if ($aggregation->getField() === $aggregationField) { + unset($this->aggregations[$key]); + + return; + } + } + } + /** * Return Querytext. * diff --git a/Tests/Model/IndexTest.php b/Tests/Model/IndexTest.php index 50739f6..45d0beb 100644 --- a/Tests/Model/IndexTest.php +++ b/Tests/Model/IndexTest.php @@ -145,4 +145,35 @@ public function testWithMetadataValue() $index = Index::createFromArray($index->toArray()); $this->assertEquals('val1', $index->getMetadataValue('key1')); } + + public function testWithFields() + { + $index = Index::createFromArray([ + 'uuid' => [ + 'id' => 'testId', + ], + 'app_id' => [ + 'id' => 'testAppId', + ], + ]); + + $this->assertEquals([], $index->getFields()); + + $index = Index::createFromArray([ + 'uuid' => [ + 'id' => 'testId', + ], + 'app_id' => [ + 'id' => 'testAppId', + ], + 'is_ok' => true, + 'doc_count' => 10, + 'size' => '1kb', + 'fields' => ['f1', 'f2', 'f3'], + ]); + + $this->assertEquals(['f1', 'f2', 'f3'], $index->getFields()); + $index->withFields(['f4', 'f5']); + $this->assertEquals(['f4', 'f5'], $index->getFields()); + } } diff --git a/Tests/Query/QueryTest.php b/Tests/Query/QueryTest.php index 88d494d..ee9cfac 100644 --- a/Tests/Query/QueryTest.php +++ b/Tests/Query/QueryTest.php @@ -330,4 +330,48 @@ public function testAggregationPromoted() $this->assertEquals(['item1', 'item3'], $query->getAggregation('field2')->getPromoted()); $this->assertEquals(['item4'], $query->getAggregation('field3')->getPromoted()); } + + public function testDeleteAggregationByField() + { + $query = Query::createMatchAll() + ->aggregateBy('field1', 'field1', Filter::AT_LEAST_ONE, Aggregation::SORT_BY_COUNT_ASC) + ->aggregateBy('field2', 'field2', Filter::AT_LEAST_ONE, Aggregation::SORT_BY_COUNT_ASC); + + $this->assertCount(2, $query->getAggregations()); + $this->assertNotNull($query->getAggregation('field1')); + $this->assertNotNull($query->getAggregation('field2')); + $this->assertNull($query->getAggregation('field3')); + + $query->deleteAggregationByField('field1'); + $this->assertCount(1, $query->getAggregations()); + $this->assertNull($query->getAggregation('field1')); + $this->assertNotNull($query->getAggregation('field2')); + $this->assertNull($query->getAggregation('field3')); + + $query->deleteAggregationByField('field1'); + $this->assertCount(1, $query->getAggregations()); + $this->assertNull($query->getAggregation('field1')); + $this->assertNotNull($query->getAggregation('field2')); + $this->assertNull($query->getAggregation('field3')); + + $query->deleteAggregationByField('field2'); + $this->assertCount(0, $query->getAggregations()); + $this->assertNull($query->getAggregation('field1')); + $this->assertNull($query->getAggregation('field2')); + $this->assertNull($query->getAggregation('field3')); + + $query->deleteAggregationByField('field3'); + $this->assertCount(0, $query->getAggregations()); + $this->assertNull($query->getAggregation('field1')); + $this->assertNull($query->getAggregation('field2')); + $this->assertNull($query->getAggregation('field3')); + } + + public function testForceSize() + { + $query = Query::create('x', 1, 10); + $this->assertEquals(10, $query->getSize()); + $query->forceSize(7); + $this->assertEquals(7, $query->getSize()); + } }