Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documents changes v2 #344

Merged
merged 5 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/Contracts/DocumentsQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace MeiliSearch\Contracts;

class DocumentsQuery
{
private int $offset;
private int $limit;
private array $fields;

public function setOffset(int $offset): DocumentsQuery
{
$this->offset = $offset;

return $this;
}

public function setLimit(int $limit): DocumentsQuery
{
$this->limit = $limit;

return $this;
}

public function setFields(array $fields): DocumentsQuery
{
$this->fields = $fields;

return $this;
}

public function toArray(): array
{
return array_filter([
'offset' => $this->offset ?? null,
'limit' => $this->limit ?? null,
'fields' => isset($this->fields) ? implode(',', $this->fields) : null,
], function ($item) { return null != $item || is_numeric($item); });
}
}
59 changes: 59 additions & 0 deletions src/Contracts/DocumentsResults.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace MeiliSearch\Contracts;

class DocumentsResults extends Data
{
private int $offset;
private int $limit;
private int $total;

public function __construct(array $params)
{
parent::__construct($params['results'] ?? []);

$this->offset = $params['offset'];
$this->limit = $params['limit'];
$this->total = $params['total'] ?? 0;
}

/**
* @return array<int, array>
*/
public function getResults(): array
{
return $this->data;
}

public function getOffset(): int
{
return $this->offset;
}

public function getLimit(): int
{
return $this->limit;
}

public function getTotal(): int
{
return $this->total;
}

public function toArray(): array
{
return [
'results' => $this->data,
'offset' => $this->offset,
'limit' => $this->limit,
'total' => $this->total,
];
}

public function count(): int
{
return \count($this->data);
}
}
9 changes: 7 additions & 2 deletions src/Endpoints/Delegates/HandlesDocuments.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace MeiliSearch\Endpoints\Delegates;

use Generator;
use MeiliSearch\Contracts\DocumentsQuery;
use MeiliSearch\Contracts\DocumentsResults;
use MeiliSearch\Exceptions\InvalidArgumentException;

trait HandlesDocuments
Expand All @@ -16,9 +18,12 @@ public function getDocument($documentId)
return $this->http->get(self::PATH.'/'.$this->uid.'/documents/'.$documentId);
}

public function getDocuments(array $query = [])
public function getDocuments(DocumentsQuery $options = null): DocumentsResults
{
return $this->http->get(self::PATH.'/'.$this->uid.'/documents', $query);
$query = isset($options) ? $options->toArray() : [];
$response = $this->http->get(self::PATH.'/'.$this->uid.'/documents', $query);

return new DocumentsResults($response);
}

public function addDocuments(array $documents, ?string $primaryKey = null)
Expand Down
39 changes: 39 additions & 0 deletions tests/Contracts/DocumentsQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Tests\Contracts;

use MeiliSearch\Contracts\DocumentsQuery;
use PHPUnit\Framework\TestCase;

class DocumentsQueryTest extends TestCase
{
public function testSetFields(): void
{
$data = (new DocumentsQuery())->setLimit(10)->setFields(['abc', 'xyz']);

$this->assertEquals($data->toArray(), ['limit' => 10, 'fields' => 'abc,xyz']);
}

public function testToArrayWithoutSetFields(): void
{
$data = (new DocumentsQuery())->setLimit(10);

$this->assertEquals($data->toArray(), ['limit' => 10]);
}

public function testToArrayWithoutSetOffset(): void
{
$data = (new DocumentsQuery())->setOffset(10);

$this->assertEquals($data->toArray(), ['offset' => 10]);
}

public function testToArrayWithZeros(): void
{
$data = (new DocumentsQuery())->setLimit(0)->setOffset(0);

$this->assertEquals($data->toArray(), ['limit' => 0, 'offset' => 0]);
}
}
51 changes: 32 additions & 19 deletions tests/Endpoints/DocumentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\Endpoints;

use MeiliSearch\Contracts\DocumentsQuery;
use MeiliSearch\Exceptions\ApiException;
use MeiliSearch\Exceptions\InvalidArgumentException;
use MeiliSearch\Exceptions\JsonEncodingException;
Expand All @@ -19,7 +20,7 @@ public function testAddDocuments(): void
$this->assertIsValidPromise($promise);

$index->waitForTask($promise['taskUid']);
$response = $index->getDocuments()['results'];
$response = $index->getDocuments();
$this->assertCount(\count(self::DOCUMENTS), $response);
}

Expand All @@ -35,7 +36,7 @@ public function testAddDocumentsInBatches(): void
$index->waitForTask($promise['taskUid']);
}

$response = $index->getDocuments()['results'];
$response = $index->getDocuments();
$this->assertCount(\count(self::DOCUMENTS), $response);
}

Expand All @@ -53,7 +54,7 @@ public function testAddDocumentWithSpecialChars(): void
$this->assertIsValidPromise($promise);
$index->waitForTask($promise['taskUid']);

$response = $index->getDocuments()['results'];
$response = $index->getDocuments();
$this->assertCount(\count($documents), $response);

foreach ($documents as $k => $document) {
Expand All @@ -79,7 +80,7 @@ public function testAddDocumentsCsv(): void
$this->assertEquals($update['status'], 'succeeded');
$this->assertNotEquals($update['details']['receivedDocuments'], 0);

$response = $index->getDocuments()['results'];
$response = $index->getDocuments();
$this->assertCount(20, $response);
}

Expand All @@ -100,7 +101,7 @@ public function testAddDocumentsJson(): void
$this->assertEquals($update['status'], 'succeeded');
$this->assertNotEquals($update['details']['receivedDocuments'], 0);

$response = $index->getDocuments()['results'];
$response = $index->getDocuments();
$this->assertCount(20, $response);
}

Expand All @@ -121,7 +122,7 @@ public function testAddDocumentsNdJson(): void
$this->assertEquals($update['status'], 'succeeded');
$this->assertNotEquals($update['details']['receivedDocuments'], 0);

$response = $index->getDocuments()['results'];
$response = $index->getDocuments();
$this->assertCount(20, $response);
}

Expand Down Expand Up @@ -180,7 +181,7 @@ public function testReplaceDocuments(): void
$this->assertSame($replacement['id'], $response['id']);
$this->assertSame($replacement['title'], $response['title']);
$this->assertFalse(array_search('comment', $response, true));
$response = $index->getDocuments()['results'];
$response = $index->getDocuments();
$this->assertCount(\count(self::DOCUMENTS), $response);
}

Expand All @@ -204,7 +205,7 @@ public function testUpdateDocuments(): void
$this->assertSame($replacement['title'], $response['title']);
$this->assertArrayHasKey('comment', $response);

$response = $index->getDocuments()['results'];
$response = $index->getDocuments();

$this->assertCount(\count(self::DOCUMENTS), $response);
}
Expand Down Expand Up @@ -238,7 +239,7 @@ public function testUpdateDocumentsInBatches(): void
$this->assertArrayHasKey('comment', $response);
}

$response = $index->getDocuments()['results'];
$response = $index->getDocuments();
$this->assertCount(\count(self::DOCUMENTS), $response);
}

Expand All @@ -262,7 +263,7 @@ public function testAddWithUpdateDocuments(): void
$this->assertSame($document['title'], $response['title']);
$this->assertFalse(array_search('comment', $response, true));

$response = $index->getDocuments()['results'];
$response = $index->getDocuments();

$this->assertCount(\count(self::DOCUMENTS) + 1, $response);
}
Expand All @@ -279,7 +280,7 @@ public function testDeleteNonExistingDocument(): void
$this->assertIsValidPromise($promise);

$index->waitForTask($promise['taskUid']);
$response = $index->getDocuments()['results'];
$response = $index->getDocuments();

$this->assertCount(\count(self::DOCUMENTS), $response);
$this->assertNull($this->findDocumentWithId($response, $documentId));
Expand All @@ -297,7 +298,7 @@ public function testDeleteSingleExistingDocumentWithDocumentIdAsInteger(): void
$this->assertIsValidPromise($promise);

$index->waitForTask($promise['taskUid']);
$response = $index->getDocuments()['results'];
$response = $index->getDocuments();

$this->assertCount(\count(self::DOCUMENTS) - 1, $response);
$this->assertNull($this->findDocumentWithId($response, $documentId));
Expand All @@ -313,7 +314,7 @@ public function testDeleteSingleExistingDocumentWithDocumentIdAsString(): void
$promise = $index->deleteDocument($stringDocumentId);
$index->waitForTask($promise['taskUid']);

$response = $index->getDocuments()['results'];
$response = $index->getDocuments();

$this->assertEmpty($response);
}
Expand All @@ -329,7 +330,7 @@ public function testDeleteMultipleDocumentsWithDocumentIdAsInteger(): void
$this->assertIsValidPromise($promise);

$index->waitForTask($promise['taskUid']);
$response = $index->getDocuments()['results'];
$response = $index->getDocuments();

$this->assertCount(\count(self::DOCUMENTS) - 2, $response);
$this->assertNull($this->findDocumentWithId($response, $documentIds[0]));
Expand All @@ -350,9 +351,9 @@ public function testDeleteMultipleDocumentsWithDocumentIdAsString(): void
$promise = $index->deleteDocuments(['myUniqueId1', 'myUniqueId3']);
$index->waitForTask($promise['taskUid']);

$response = $index->getDocuments()['results'];
$response = $index->getDocuments();
$this->assertCount(1, $response);
$this->assertSame([['id' => 'myUniqueId2']], $response);
$this->assertSame([['id' => 'myUniqueId2']], $response->getResults());
}

public function testDeleteAllDocuments(): void
Expand All @@ -365,7 +366,7 @@ public function testDeleteAllDocuments(): void
$this->assertIsValidPromise($promise);

$index->waitForTask($promise['taskUid']);
$response = $index->getDocuments()['results'];
$response = $index->getDocuments();

$this->assertCount(0, $response);
}
Expand Down Expand Up @@ -395,7 +396,7 @@ public function testAddDocumentWithPrimaryKey(): void
$index->waitForTask($response['taskUid']);

$this->assertSame('unique', $index->fetchPrimaryKey());
$this->assertCount(1, $index->getDocuments()['results']);
$this->assertCount(1, $index->getDocuments());
}

public function testUpdateDocumentWithPrimaryKey(): void
Expand All @@ -415,7 +416,19 @@ public function testUpdateDocumentWithPrimaryKey(): void
$index->waitForTask($promise['taskUid']);

$this->assertSame('unique', $index->fetchPrimaryKey());
$this->assertCount(1, $index->getDocuments()['results']);
$this->assertCount(1, $index->getDocuments());
}

public function testGetDocumentsWithPagination(): void
{
$index = $this->createEmptyIndex('documents');
$promise = $index->addDocuments(self::DOCUMENTS);
$this->assertIsValidPromise($promise);
$index->waitForTask($promise['taskUid']);

$response = $index->getDocuments((new DocumentsQuery())->setLimit(3));

$this->assertCount(3, $response);
}

/**
Expand Down