Skip to content

Commit

Permalink
Merge pull request #698 from meilisearch/feat/add-tasks-reverse
Browse files Browse the repository at this point in the history
Add parameter to get tasks in reverse order
  • Loading branch information
Strift authored Dec 5, 2024
2 parents ff24520 + 05c441c commit 77cd241
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Contracts/TasksQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class TasksQuery
*/
private ?array $canceledBy = null;

private ?bool $reverse = null;

/**
* @return $this
*/
Expand Down Expand Up @@ -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(
Expand All @@ -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;
}
);
}
}
13 changes: 13 additions & 0 deletions tests/Contracts/TasksQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
16 changes: 16 additions & 0 deletions tests/Endpoints/TasksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 77cd241

Please sign in to comment.