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 deleteTasks method #423

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
12 changes: 12 additions & 0 deletions src/Contracts/DeleteTasksQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace MeiliSearch\Contracts;

use MeiliSearch\Delegates\TasksQueryTrait;

class DeleteTasksQuery
{
use TasksQueryTrait;
}
6 changes: 6 additions & 0 deletions src/Endpoints/Delegates/HandlesTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MeiliSearch\Endpoints\Delegates;

use MeiliSearch\Contracts\CancelTasksQuery;
use MeiliSearch\Contracts\DeleteTasksQuery;
use MeiliSearch\Contracts\TasksQuery;
use MeiliSearch\Contracts\TasksResults;

Expand All @@ -24,6 +25,11 @@ public function getTasks(TasksQuery $options = null): TasksResults
return new TasksResults($response);
}

public function deleteTasks(DeleteTasksQuery $options = null): array
{
return $this->tasks->deleteTasks($options);
}

public function cancelTasks(CancelTasksQuery $options = null): array
{
return $this->tasks->cancelTasks($options);
Expand Down
9 changes: 9 additions & 0 deletions src/Endpoints/Tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MeiliSearch\Endpoints;

use MeiliSearch\Contracts\CancelTasksQuery;
use MeiliSearch\Contracts\DeleteTasksQuery;
use MeiliSearch\Contracts\Endpoint;
use MeiliSearch\Exceptions\TimeOutException;

Expand All @@ -30,6 +31,14 @@ public function cancelTasks(?CancelTasksQuery $options): array
return $response;
}

public function deleteTasks(?DeleteTasksQuery $options): array
{
$options = $options ?? new DeleteTasksQuery();
$response = $this->http->delete(self::PATH, $options->toArray());

return $response;
}

/**
* @throws TimeOutException
*/
Expand Down
49 changes: 49 additions & 0 deletions tests/Contracts/DeleteTasksQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Tests\Contracts;

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

class DeleteTasksQueryTest extends TestCase
{
public function testSetTypes(): void
{
$data = (new TasksQuery())->setTypes(['abc', 'xyz']);

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

public function testSetNext(): void
{
$data = (new TasksQuery())->setNext(99);

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

public function testSetAnyDateFilter(): void
{
$date = new \DateTime();
$data = (new TasksQuery())->setBeforeEnqueuedAt($date);

$this->assertEquals($data->toArray(), ['beforeEnqueuedAt' => $date->format(\DateTime::RFC3339)]);
}

public function testToArrayWithSetNextWithZero(): void
{
$data = (new TasksQuery())->setNext(0);

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

public function testToArrayWithDifferentSets(): void
{
$data = (new TasksQuery())->setFrom(10)->setLimit(9)->setNext(99)->setStatuses(['enqueued']);

$this->assertEquals($data->toArray(), [
'limit' => 9, 'next' => 99, 'from' => 10, 'statuses' => 'enqueued',
]);
}
}
10 changes: 10 additions & 0 deletions tests/Endpoints/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\Endpoints;

use MeiliSearch\Contracts\DeleteTasksQuery;
use MeiliSearch\Contracts\TasksQuery;
use MeiliSearch\Endpoints\Indexes;
use MeiliSearch\Exceptions\TimeOutException;
Expand Down Expand Up @@ -276,6 +277,15 @@ public function testSwapIndexes(): void
$this->assertSame($response['details']['swaps'], [['indexes' => ['indexA', 'indexB']], ['indexes' => ['indexC', 'indexD']]]);
}

public function testDeleteTasks(): void
{
$promise = $this->client->deleteTasks((new DeleteTasksQuery())->setUids([1, 2]));
$response = $this->client->waitForTask($promise['taskUid']);

$this->assertSame($response['details']['originalFilter'], '?uids=1%2C2');
$this->assertIsNumeric($response['details']['matchedTasks']);
}

public function testParseDate(): void
{
$date = '2021-01-01T01:23:45.123456Z';
Expand Down