From ba5045663f084a69c3b774549cee02dcbc3b5161 Mon Sep 17 00:00:00 2001 From: Bruno Casali Date: Wed, 23 Nov 2022 16:37:49 -0300 Subject: [PATCH 1/2] Add DeleteTasksQuery query object to receive data to delete tasks method --- src/Contracts/DeleteTasksQuery.php | 12 ++++++ tests/Contracts/DeleteTasksQueryTest.php | 49 ++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/Contracts/DeleteTasksQuery.php create mode 100644 tests/Contracts/DeleteTasksQueryTest.php diff --git a/src/Contracts/DeleteTasksQuery.php b/src/Contracts/DeleteTasksQuery.php new file mode 100644 index 000000000..d9416f6c7 --- /dev/null +++ b/src/Contracts/DeleteTasksQuery.php @@ -0,0 +1,12 @@ +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', + ]); + } +} From 79d1a75738187e67a7315e0571c0d0eaa95bf2a3 Mon Sep 17 00:00:00 2001 From: Bruno Casali Date: Wed, 23 Nov 2022 16:40:29 -0300 Subject: [PATCH 2/2] Add deleteTasks method to Client --- src/Endpoints/Delegates/HandlesTasks.php | 6 ++++++ src/Endpoints/Tasks.php | 9 +++++++++ tests/Endpoints/IndexTest.php | 10 ++++++++++ 3 files changed, 25 insertions(+) diff --git a/src/Endpoints/Delegates/HandlesTasks.php b/src/Endpoints/Delegates/HandlesTasks.php index ab828bdf7..78276fa08 100644 --- a/src/Endpoints/Delegates/HandlesTasks.php +++ b/src/Endpoints/Delegates/HandlesTasks.php @@ -5,6 +5,7 @@ namespace MeiliSearch\Endpoints\Delegates; use MeiliSearch\Contracts\CancelTasksQuery; +use MeiliSearch\Contracts\DeleteTasksQuery; use MeiliSearch\Contracts\TasksQuery; use MeiliSearch\Contracts\TasksResults; @@ -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); diff --git a/src/Endpoints/Tasks.php b/src/Endpoints/Tasks.php index 2b98cd53a..aa52e2e18 100644 --- a/src/Endpoints/Tasks.php +++ b/src/Endpoints/Tasks.php @@ -5,6 +5,7 @@ namespace MeiliSearch\Endpoints; use MeiliSearch\Contracts\CancelTasksQuery; +use MeiliSearch\Contracts\DeleteTasksQuery; use MeiliSearch\Contracts\Endpoint; use MeiliSearch\Exceptions\TimeOutException; @@ -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 */ diff --git a/tests/Endpoints/IndexTest.php b/tests/Endpoints/IndexTest.php index ae60deba2..d91b53730 100644 --- a/tests/Endpoints/IndexTest.php +++ b/tests/Endpoints/IndexTest.php @@ -4,6 +4,7 @@ namespace Tests\Endpoints; +use MeiliSearch\Contracts\DeleteTasksQuery; use MeiliSearch\Contracts\TasksQuery; use MeiliSearch\Endpoints\Indexes; use MeiliSearch\Exceptions\TimeOutException; @@ -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';