Skip to content

Commit

Permalink
Merge pull request #423 from meilisearch/bump-meilisearch-v0.30.0-add…
Browse files Browse the repository at this point in the history
…-delete-tasks

Add `deleteTasks` method
  • Loading branch information
brunoocasali authored Nov 24, 2022
2 parents 20c35c8 + 79d1a75 commit 384b3dd
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
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

0 comments on commit 384b3dd

Please sign in to comment.