diff --git a/src/Contracts/TasksQuery.php b/src/Contracts/TasksQuery.php index 174fdffe..d3301017 100644 --- a/src/Contracts/TasksQuery.php +++ b/src/Contracts/TasksQuery.php @@ -22,6 +22,8 @@ class TasksQuery */ private ?array $canceledBy = null; + private ?bool $reverse = null; + /** * @return $this */ @@ -54,6 +56,16 @@ public function setLimit(int $limit): self return $this; } + /** + * @return $this + */ + public function setReverse(bool $reverse): self + { + $this->reverse = $reverse; + + return $this; + } + public function toArray(): array { return array_filter( @@ -63,8 +75,12 @@ public function toArray(): array 'from' => $this->from, 'limit' => $this->limit, 'canceledBy' => $this->formatArray($this->canceledBy), + 'reverse' => (null !== $this->reverse ? ($this->reverse ? 'true' : 'false') : null), ] - ), static function ($item) { return null !== $item; } + ), + static function ($item) { + return null !== $item; + } ); } } diff --git a/tests/Contracts/TasksQueryTest.php b/tests/Contracts/TasksQueryTest.php index 73dc2f99..30ac7011 100644 --- a/tests/Contracts/TasksQueryTest.php +++ b/tests/Contracts/TasksQueryTest.php @@ -4,6 +4,8 @@ namespace Tests\Contracts; +// TODO: refactor to make sure these tests clean up after themselves + use Meilisearch\Contracts\TasksQuery; use PHPUnit\Framework\TestCase; @@ -109,4 +111,15 @@ public function testSetFrom(): void self::assertSame(['from' => 1], $data->toArray()); } + + /** + * @testWith [true, "true"] + * [false, "false"] + */ + public function testSetReverse(bool $reverse, string $expected): void + { + $data = (new TasksQuery())->setReverse($reverse); + + self::assertSame(['reverse' => $expected], $data->toArray()); + } } diff --git a/tests/Endpoints/TasksTest.php b/tests/Endpoints/TasksTest.php index c3bb6405..a30b5955 100644 --- a/tests/Endpoints/TasksTest.php +++ b/tests/Endpoints/TasksTest.php @@ -140,6 +140,22 @@ public function testCancelTasksWithFilter(): void self::assertSame('succeeded', $response['status']); } + public function testGetAllTasksInReverseOrder(): void + { + $startDate = new \DateTimeImmutable('now'); + + $tasks = $this->client->getTasks((new TasksQuery()) + ->setAfterEnqueuedAt($startDate) + ); + $reversedTasks = $this->client->getTasks((new TasksQuery()) + ->setAfterEnqueuedAt($startDate) + ->setReverse(true) + ); + + self::assertSameSize($tasks->getResults(), $reversedTasks->getResults()); + self::assertSame($tasks->getResults(), array_reverse($reversedTasks->getResults())); + } + public function testExceptionIfNoTaskIdWhenGetting(): void { $this->expectException(ApiException::class);