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

Add chainable setReverse() method to get tasks in reverse order #698

Merged
merged 9 commits into from
Dec 5, 2024
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
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
Loading