Skip to content

Commit f2d178f

Browse files
Merge #454
454: Remove raw methods & Fix query classes r=brunoocasali a=brunoocasali - Add `canceledBy` and remove `next` from toArray in `TasksQuery` - Correct `DeleteTasksQuery` allowed attributes: removed `next`. - Correct `CancelTasksQuery` allowed attributes: removed `next` and `canceledBy`. - Correct `TasksQuery` allowed attributes: removed `next`. - Remove non-generic methods from `TasksQueryTrait` - Remove `deleteAllIndexes`, `getAllRawIndexes`. - Rename `getAllIndexes` to `getIndexes`. - Refactor `tearDown` after removing `deleteAllIndexes` Co-authored-by: Bruno Casali <[email protected]>
2 parents 756f867 + c8bbe6a commit f2d178f

File tree

12 files changed

+87
-206
lines changed

12 files changed

+87
-206
lines changed

src/Contracts/DeleteTasksQuery.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,23 @@
99
class DeleteTasksQuery
1010
{
1111
use TasksQueryTrait;
12+
13+
private array $canceledBy;
14+
15+
public function setCanceledBy(array $canceledBy)
16+
{
17+
$this->canceledBy = $canceledBy;
18+
19+
return $this;
20+
}
21+
22+
public function toArray(): array
23+
{
24+
return array_filter(
25+
array_merge(
26+
$this->baseArray(),
27+
['canceledBy' => $this->formatArray($this->canceledBy ?? null)]
28+
), function ($item) { return null != $item || is_numeric($item); }
29+
);
30+
}
1231
}

src/Contracts/TasksQuery.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class TasksQuery
1212

1313
private int $from;
1414
private int $limit;
15+
private array $canceledBy;
1516

1617
public function setFrom(int $from): TasksQuery
1718
{
@@ -20,6 +21,13 @@ public function setFrom(int $from): TasksQuery
2021
return $this;
2122
}
2223

24+
public function setCanceledBy(array $canceledBy): TasksQuery
25+
{
26+
$this->canceledBy = $canceledBy;
27+
28+
return $this;
29+
}
30+
2331
public function setLimit(int $limit): TasksQuery
2432
{
2533
$this->limit = $limit;
@@ -29,21 +37,15 @@ public function setLimit(int $limit): TasksQuery
2937

3038
public function toArray(): array
3139
{
32-
return array_filter([
33-
'from' => $this->from ?? null,
34-
'limit' => $this->limit ?? null,
35-
'next' => $this->next ?? null,
36-
'beforeEnqueuedAt' => $this->formatDate($this->beforeEnqueuedAt ?? null),
37-
'afterEnqueuedAt' => $this->formatDate($this->afterEnqueuedAt ?? null),
38-
'beforeStartedAt' => $this->formatDate($this->beforeStartedAt ?? null),
39-
'afterStartedAt' => $this->formatDate($this->afterStartedAt ?? null),
40-
'beforeFinishedAt' => $this->formatDate($this->beforeFinishedAt ?? null),
41-
'afterFinishedAt' => $this->formatDate($this->afterFinishedAt ?? null),
42-
'statuses' => $this->formatArray($this->statuses ?? null),
43-
'uids' => $this->formatArray($this->uids ?? null),
44-
'canceledBy' => $this->formatArray($this->canceledBy ?? null),
45-
'types' => $this->formatArray($this->types ?? null),
46-
'indexUids' => $this->formatArray($this->indexUids ?? null),
47-
], function ($item) { return null != $item || is_numeric($item); });
40+
return array_filter(
41+
array_merge(
42+
$this->baseArray(),
43+
[
44+
'from' => $this->from ?? null,
45+
'limit' => $this->limit ?? null,
46+
'canceledBy' => $this->formatArray($this->canceledBy ?? null),
47+
]
48+
), function ($item) { return null != $item || is_numeric($item); }
49+
);
4850
}
4951
}

src/Delegates/HandlesIndex.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,11 @@
1010

1111
trait HandlesIndex
1212
{
13-
public function getAllIndexes(IndexesQuery $options = null): IndexesResults
13+
public function getIndexes(IndexesQuery $options = null): IndexesResults
1414
{
1515
return $this->index->all($options ?? null);
1616
}
1717

18-
public function getAllRawIndexes(IndexesQuery $options = null): array
19-
{
20-
return $this->index->allRaw($options ?? []);
21-
}
22-
2318
public function getRawIndex(string $uid): array
2419
{
2520
return $this->index($uid)->fetchRawInfo();
@@ -40,17 +35,6 @@ public function deleteIndex(string $uid): array
4035
return $this->index($uid)->delete();
4136
}
4237

43-
public function deleteAllIndexes(): array
44-
{
45-
$tasks = [];
46-
$indexes = $this->getAllIndexes();
47-
foreach ($indexes as $index) {
48-
$tasks[] = $index->delete();
49-
}
50-
51-
return $tasks;
52-
}
53-
5438
public function createIndex(string $uid, array $options = []): array
5539
{
5640
return $this->index->create($uid, $options);

src/Delegates/TasksQueryTrait.php

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,17 @@
66

77
trait TasksQueryTrait
88
{
9-
private int $next;
109
private array $types;
1110
private array $statuses;
1211
private array $indexUids;
1312
private array $uids;
14-
private array $canceledBy;
1513
private \DateTime $beforeEnqueuedAt;
1614
private \DateTime $afterEnqueuedAt;
1715
private \DateTime $beforeStartedAt;
1816
private \DateTime $afterStartedAt;
1917
private \DateTime $beforeFinishedAt;
2018
private \DateTime $afterFinishedAt;
2119

22-
public function setNext(int $next)
23-
{
24-
$this->next = $next;
25-
26-
return $this;
27-
}
28-
2920
public function setTypes(array $types)
3021
{
3122
$this->types = $types;
@@ -59,13 +50,6 @@ public function setUids(array $uids)
5950
return $this;
6051
}
6152

62-
public function setCanceledBy(array $canceledBy)
63-
{
64-
$this->canceledBy = $canceledBy;
65-
66-
return $this;
67-
}
68-
6953
public function setBeforeEnqueuedAt(\DateTime $date)
7054
{
7155
$this->beforeEnqueuedAt = $date;
@@ -110,8 +94,15 @@ public function setAfterFinishedAt(\DateTime $date)
11094

11195
public function toArray(): array
11296
{
113-
return array_filter([
114-
'next' => $this->next ?? null,
97+
return array_filter(
98+
$this->baseArray(),
99+
function ($item) { return null != $item || is_numeric($item); }
100+
);
101+
}
102+
103+
protected function baseArray(): array
104+
{
105+
return [
115106
'beforeEnqueuedAt' => $this->formatDate($this->beforeEnqueuedAt ?? null),
116107
'afterEnqueuedAt' => $this->formatDate($this->afterEnqueuedAt ?? null),
117108
'beforeStartedAt' => $this->formatDate($this->beforeStartedAt ?? null),
@@ -120,10 +111,9 @@ public function toArray(): array
120111
'afterFinishedAt' => $this->formatDate($this->afterFinishedAt ?? null),
121112
'statuses' => $this->formatArray($this->statuses ?? null),
122113
'uids' => $this->formatArray($this->uids ?? null),
123-
'canceledBy' => $this->formatArray($this->canceledBy ?? null),
124114
'types' => $this->formatArray($this->types ?? null),
125115
'indexUids' => $this->formatArray($this->indexUids ?? null),
126-
], function ($item) { return null != $item || is_numeric($item); });
116+
];
127117
}
128118

129119
private function formatDate(?\DateTime $date)

src/Endpoints/Indexes.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class Indexes extends Endpoint
2727

2828
private ?string $uid;
2929
private ?string $primaryKey;
30-
private ?string $createdAt;
31-
private ?string $updatedAt;
30+
private ?\DateTime $createdAt;
31+
private ?\DateTime $updatedAt;
3232
private Tasks $tasks;
3333

3434
public function __construct(Http $http, $uid = null, $primaryKey = null, $createdAt = null, $updatedAt = null)
@@ -48,8 +48,8 @@ protected function newInstance(array $attributes): self
4848
$this->http,
4949
$attributes['uid'],
5050
$attributes['primaryKey'],
51-
$attributes['createdAt'],
52-
$attributes['updatedAt'],
51+
static::parseDate($attributes['createdAt']),
52+
static::parseDate($attributes['updatedAt']),
5353
);
5454
}
5555

@@ -60,8 +60,8 @@ protected function fill(array $attributes): self
6060
{
6161
$this->uid = $attributes['uid'];
6262
$this->primaryKey = $attributes['primaryKey'];
63-
$this->createdAt = $attributes['createdAt'];
64-
$this->updatedAt = $attributes['updatedAt'];
63+
$this->createdAt = static::parseDate($attributes['createdAt']);
64+
$this->updatedAt = static::parseDate($attributes['updatedAt']);
6565

6666
return $this;
6767
}
@@ -112,21 +112,11 @@ public function getUid(): ?string
112112
}
113113

114114
public function getCreatedAt(): ?\DateTime
115-
{
116-
return static::parseDate($this->createdAt);
117-
}
118-
119-
public function getCreatedAtString(): ?string
120115
{
121116
return $this->createdAt;
122117
}
123118

124119
public function getUpdatedAt(): ?\DateTime
125-
{
126-
return static::parseDate($this->updatedAt);
127-
}
128-
129-
public function getUpdatedAtString(): ?string
130120
{
131121
return $this->updatedAt;
132122
}

tests/Contracts/CancelTasksQueryTest.php

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,32 @@
44

55
namespace Tests\Contracts;
66

7-
use Meilisearch\Contracts\TasksQuery;
7+
use Meilisearch\Contracts\CancelTasksQuery;
88
use PHPUnit\Framework\TestCase;
99

1010
class CancelTasksQueryTest extends TestCase
1111
{
1212
public function testSetTypes(): void
1313
{
14-
$data = (new TasksQuery())->setTypes(['abc', 'xyz']);
14+
$data = (new CancelTasksQuery())->setTypes(['abc', 'xyz']);
1515

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

19-
public function testSetNext(): void
20-
{
21-
$data = (new TasksQuery())->setNext(99);
22-
23-
$this->assertEquals($data->toArray(), ['next' => 99]);
24-
}
25-
2619
public function testSetAnyDateFilter(): void
2720
{
2821
$date = new \DateTime();
29-
$data = (new TasksQuery())->setBeforeEnqueuedAt($date);
22+
$data = (new CancelTasksQuery())->setBeforeEnqueuedAt($date);
3023

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

34-
public function testToArrayWithSetNextWithZero(): void
35-
{
36-
$data = (new TasksQuery())->setNext(0);
37-
38-
$this->assertEquals($data->toArray(), ['next' => 0]);
39-
}
40-
4127
public function testToArrayWithDifferentSets(): void
4228
{
43-
$data = (new TasksQuery())->setFrom(10)->setLimit(9)->setNext(99)->setStatuses(['enqueued']);
29+
$data = (new CancelTasksQuery())->setUids([1, 2, 3])->setStatuses(['enqueued']);
4430

4531
$this->assertEquals($data->toArray(), [
46-
'limit' => 9, 'next' => 99, 'from' => 10, 'statuses' => 'enqueued',
32+
'uids' => '1,2,3', 'statuses' => 'enqueued',
4733
]);
4834
}
4935
}

tests/Contracts/DeleteTasksQueryTest.php

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,32 @@
44

55
namespace Tests\Contracts;
66

7-
use Meilisearch\Contracts\TasksQuery;
7+
use Meilisearch\Contracts\DeleteTasksQuery;
88
use PHPUnit\Framework\TestCase;
99

1010
class DeleteTasksQueryTest extends TestCase
1111
{
1212
public function testSetTypes(): void
1313
{
14-
$data = (new TasksQuery())->setTypes(['abc', 'xyz']);
14+
$data = (new DeleteTasksQuery())->setTypes(['abc', 'xyz']);
1515

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

19-
public function testSetNext(): void
20-
{
21-
$data = (new TasksQuery())->setNext(99);
22-
23-
$this->assertEquals($data->toArray(), ['next' => 99]);
24-
}
25-
2619
public function testSetAnyDateFilter(): void
2720
{
2821
$date = new \DateTime();
29-
$data = (new TasksQuery())->setBeforeEnqueuedAt($date);
22+
$data = (new DeleteTasksQuery())->setCanceledBy([null])->setBeforeEnqueuedAt($date);
3023

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

34-
public function testToArrayWithSetNextWithZero(): void
35-
{
36-
$data = (new TasksQuery())->setNext(0);
37-
38-
$this->assertEquals($data->toArray(), ['next' => 0]);
39-
}
40-
4127
public function testToArrayWithDifferentSets(): void
4228
{
43-
$data = (new TasksQuery())->setFrom(10)->setLimit(9)->setNext(99)->setStatuses(['enqueued']);
29+
$data = (new DeleteTasksQuery())->setCanceledBy([1, 2])->setStatuses(['enqueued']);
4430

4531
$this->assertEquals($data->toArray(), [
46-
'limit' => 9, 'next' => 99, 'from' => 10, 'statuses' => 'enqueued',
32+
'canceledBy' => '1,2', 'statuses' => 'enqueued',
4733
]);
4834
}
4935
}

tests/Contracts/TasksQueryTest.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ public function testSetTypes(): void
1616
$this->assertEquals($data->toArray(), ['types' => 'abc,xyz']);
1717
}
1818

19-
public function testSetNext(): void
20-
{
21-
$data = (new TasksQuery())->setNext(99);
22-
23-
$this->assertEquals($data->toArray(), ['next' => 99]);
24-
}
25-
2619
public function testSetAnyDateFilter(): void
2720
{
2821
$date = new \DateTime();
@@ -47,10 +40,10 @@ public function testToArrayWithSetLimitWithZero(): void
4740

4841
public function testToArrayWithDifferentSets(): void
4942
{
50-
$data = (new TasksQuery())->setFrom(10)->setLimit(9)->setNext(99)->setStatuses(['enqueued']);
43+
$data = (new TasksQuery())->setFrom(10)->setLimit(9)->setCanceledBy([1, 4])->setStatuses(['enqueued']);
5144

5245
$this->assertEquals($data->toArray(), [
53-
'limit' => 9, 'next' => 99, 'from' => 10, 'statuses' => 'enqueued',
46+
'limit' => 9, 'from' => 10, 'statuses' => 'enqueued', 'canceledBy' => '1,4',
5447
]);
5548
}
5649
}

0 commit comments

Comments
 (0)