Skip to content

Commit 4de10e9

Browse files
committed
Rewrite adapter tests with Pest
1 parent c2826d2 commit 4de10e9

17 files changed

Lines changed: 1638 additions & 1877 deletions

tests/Unit/Documents/DocumentManagerTest.php

Lines changed: 250 additions & 278 deletions
Large diffs are not rendered by default.

tests/Unit/Documents/DocumentTest.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,20 @@
33
namespace DirectoryTree\OpenSearchAdapter\Tests\Unit\Documents;
44

55
use DirectoryTree\OpenSearchAdapter\Documents\Document;
6-
use PHPUnit\Framework\TestCase;
76

8-
class DocumentTest extends TestCase
9-
{
10-
public function test_document_getters(): void
11-
{
12-
$document = new Document('123456', ['title' => 'book', 'price' => 10]);
7+
test('document getters', function () {
8+
$document = new Document('123456', ['title' => 'book', 'price' => 10]);
139

14-
$this->assertSame('123456', $document->id());
15-
$this->assertSame(['title' => 'book', 'price' => 10], $document->content());
16-
$this->assertSame('book', $document->content('title'));
17-
}
10+
$this->assertSame('123456', $document->id());
11+
$this->assertSame(['title' => 'book', 'price' => 10], $document->content());
12+
$this->assertSame('book', $document->content('title'));
13+
});
1814

19-
public function test_array_casting(): void
20-
{
21-
$document = new Document('1', ['title' => 'test']);
15+
test('array casting', function () {
16+
$document = new Document('1', ['title' => 'test']);
2217

23-
$this->assertSame([
24-
'id' => '1',
25-
'content' => ['title' => 'test'],
26-
], $document->toArray());
27-
}
28-
}
18+
$this->assertSame([
19+
'id' => '1',
20+
'content' => ['title' => 'test'],
21+
], $document->toArray());
22+
});

tests/Unit/Documents/RoutingTest.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,16 @@
33
namespace DirectoryTree\OpenSearchAdapter\Tests\Unit\Documents;
44

55
use DirectoryTree\OpenSearchAdapter\Documents\Routing;
6-
use PHPUnit\Framework\TestCase;
76

8-
class RoutingTest extends TestCase
9-
{
10-
public function test_routing_values_can_be_added_and_retrieved(): void
11-
{
12-
$routing = (new Routing)
13-
->add('1', 'user1')
14-
->add('2', 'user2');
7+
test('routing values can be added and retrieved', function () {
8+
$routing = (new Routing)
9+
->add('1', 'user1')
10+
->add('2', 'user2');
1511

16-
$this->assertTrue($routing->has('1'));
17-
$this->assertSame('user1', $routing->get('1'));
18-
$this->assertTrue($routing->has('2'));
19-
$this->assertSame('user2', $routing->get('2'));
20-
$this->assertFalse($routing->has('3'));
21-
$this->assertNull($routing->get('3'));
22-
}
23-
}
12+
$this->assertTrue($routing->has('1'));
13+
$this->assertSame('user1', $routing->get('1'));
14+
$this->assertTrue($routing->has('2'));
15+
$this->assertSame('user2', $routing->get('2'));
16+
$this->assertFalse($routing->has('3'));
17+
$this->assertNull($routing->get('3'));
18+
});

tests/Unit/Exceptions/BulkRequestExceptionTest.php

Lines changed: 131 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -3,156 +3,147 @@
33
namespace DirectoryTree\OpenSearchAdapter\Tests\Unit\Exceptions;
44

55
use DirectoryTree\OpenSearchAdapter\Exceptions\BulkRequestException;
6-
use PHPUnit\Framework\TestCase;
7-
8-
class BulkRequestExceptionTest extends TestCase
9-
{
10-
public function test_response_can_be_retrieved(): void
11-
{
12-
$response = [
13-
'took' => 486,
14-
'errors' => true,
15-
'items' => [
16-
[
17-
'update' => [
18-
'_index' => 'index1',
19-
'_type' => '_doc',
20-
'_id' => '5',
21-
'status' => 404,
22-
'error' => [
23-
'type' => 'document_missing_exception',
24-
'reason' => '[_doc][5]: document missing',
25-
'index_uuid' => 'aAsFqTI0Tc2W0LCWgPNrOA',
26-
'shard' => '0',
27-
'index' => 'index1',
28-
],
6+
7+
test('response can be retrieved', function () {
8+
$response = [
9+
'took' => 486,
10+
'errors' => true,
11+
'items' => [
12+
[
13+
'update' => [
14+
'_index' => 'index1',
15+
'_type' => '_doc',
16+
'_id' => '5',
17+
'status' => 404,
18+
'error' => [
19+
'type' => 'document_missing_exception',
20+
'reason' => '[_doc][5]: document missing',
21+
'index_uuid' => 'aAsFqTI0Tc2W0LCWgPNrOA',
22+
'shard' => '0',
23+
'index' => 'index1',
2924
],
3025
],
3126
],
32-
];
33-
34-
$exception = new BulkRequestException($response);
35-
36-
$this->assertSame($response, $exception->getResponse());
37-
}
38-
39-
public function test_first_error_message_from_response_is_given_in_exception_message(): void
40-
{
41-
$response = [
42-
'took' => 486,
43-
'errors' => true,
44-
'items' => [
45-
[
46-
'update' => [
47-
'_index' => 'index1',
48-
'_type' => '_doc',
49-
'_id' => '5',
50-
'status' => 404,
51-
'error' => [
52-
'type' => 'document_missing_exception',
53-
'reason' => '[_doc][5]: document missing',
54-
'index_uuid' => 'aAsFqTI0Tc2W0LCWgPNrOA',
55-
'shard' => '0',
56-
'index' => 'index1',
57-
],
27+
],
28+
];
29+
30+
$exception = new BulkRequestException($response);
31+
32+
$this->assertSame($response, $exception->getResponse());
33+
});
34+
35+
test('first error message from response is given in exception message', function () {
36+
$response = [
37+
'took' => 486,
38+
'errors' => true,
39+
'items' => [
40+
[
41+
'update' => [
42+
'_index' => 'index1',
43+
'_type' => '_doc',
44+
'_id' => '5',
45+
'status' => 404,
46+
'error' => [
47+
'type' => 'document_missing_exception',
48+
'reason' => '[_doc][5]: document missing',
49+
'index_uuid' => 'aAsFqTI0Tc2W0LCWgPNrOA',
50+
'shard' => '0',
51+
'index' => 'index1',
5852
],
5953
],
6054
],
61-
];
62-
63-
$exception = new BulkRequestException($response);
64-
65-
$this->assertEquals(
66-
'1 bulk operation(s) did not complete successfully. Error: document_missing_exception. Reason: [_doc][5]: document missing. Catch the exception and use the DirectoryTree\OpenSearchAdapter\Exceptions\BulkRequestException::getResponse() method to get more details.',
67-
$exception->getMessage()
68-
);
69-
}
70-
71-
public function test_exception_can_be_throw_with_many_errors_in_response(): void
72-
{
73-
$response = [
74-
'took' => 486,
75-
'errors' => true,
76-
'items' => [
77-
[
78-
'update' => [
79-
'_index' => 'index1',
80-
'_type' => '_doc',
81-
'_id' => '5',
82-
'status' => 404,
83-
'error' => [
84-
'type' => 'document_missing_exception',
85-
'reason' => '[_doc][5]: document missing',
86-
'index_uuid' => 'aAsFqTI0Tc2W0LCWgPNrOA',
87-
'shard' => '0',
88-
'index' => 'index1',
89-
],
55+
],
56+
];
57+
58+
$exception = new BulkRequestException($response);
59+
60+
$this->assertEquals(
61+
'1 bulk operation(s) did not complete successfully. Error: document_missing_exception. Reason: [_doc][5]: document missing. Catch the exception and use the DirectoryTree\OpenSearchAdapter\Exceptions\BulkRequestException::getResponse() method to get more details.',
62+
$exception->getMessage()
63+
);
64+
});
65+
66+
test('exception can be thrown with many errors in response', function () {
67+
$response = [
68+
'took' => 486,
69+
'errors' => true,
70+
'items' => [
71+
[
72+
'update' => [
73+
'_index' => 'index1',
74+
'_type' => '_doc',
75+
'_id' => '5',
76+
'status' => 404,
77+
'error' => [
78+
'type' => 'document_missing_exception',
79+
'reason' => '[_doc][5]: document missing',
80+
'index_uuid' => 'aAsFqTI0Tc2W0LCWgPNrOA',
81+
'shard' => '0',
82+
'index' => 'index1',
9083
],
9184
],
92-
[
93-
'index' => [
94-
'_index' => 'index1',
95-
'_type' => '_doc',
96-
'_id' => '5',
97-
'status' => 404,
98-
'error' => [
99-
'type' => 'mapper_parsing_exception',
100-
'reason' => 'failed to parse field',
101-
'index_uuid' => 'aAsFqTI0Tc2W0LCWgPNrOA',
102-
'shard' => '0',
103-
'index' => 'index1',
104-
],
85+
],
86+
[
87+
'index' => [
88+
'_index' => 'index1',
89+
'_type' => '_doc',
90+
'_id' => '5',
91+
'status' => 404,
92+
'error' => [
93+
'type' => 'mapper_parsing_exception',
94+
'reason' => 'failed to parse field',
95+
'index_uuid' => 'aAsFqTI0Tc2W0LCWgPNrOA',
96+
'shard' => '0',
97+
'index' => 'index1',
10598
],
10699
],
107100
],
108-
];
109-
110-
$exception = new BulkRequestException($response);
111-
112-
$this->assertEquals(
113-
'2 bulk operation(s) did not complete successfully. First error: document_missing_exception. Reason: [_doc][5]: document missing. Catch the exception and use the DirectoryTree\OpenSearchAdapter\Exceptions\BulkRequestException::getResponse() method to get more details.',
114-
$exception->getMessage()
115-
);
116-
}
117-
118-
public function test_exception_can_be_throw_with_missing_error_in_response(): void
119-
{
120-
$response = [
121-
'took' => 486,
122-
'errors' => true,
123-
'items' => [
124-
[
125-
'update' => [
126-
'_index' => 'index1',
127-
'_type' => '_doc',
128-
'_id' => '5',
129-
'status' => 404,
130-
],
101+
],
102+
];
103+
104+
$exception = new BulkRequestException($response);
105+
106+
$this->assertEquals(
107+
'2 bulk operation(s) did not complete successfully. First error: document_missing_exception. Reason: [_doc][5]: document missing. Catch the exception and use the DirectoryTree\OpenSearchAdapter\Exceptions\BulkRequestException::getResponse() method to get more details.',
108+
$exception->getMessage()
109+
);
110+
});
111+
112+
test('exception can be thrown with missing error in response', function () {
113+
$response = [
114+
'took' => 486,
115+
'errors' => true,
116+
'items' => [
117+
[
118+
'update' => [
119+
'_index' => 'index1',
120+
'_type' => '_doc',
121+
'_id' => '5',
122+
'status' => 404,
131123
],
132124
],
133-
];
134-
135-
$exception = new BulkRequestException($response);
136-
137-
$this->assertEquals(
138-
'1 bulk operation(s) did not complete successfully. Catch the exception and use the DirectoryTree\OpenSearchAdapter\Exceptions\BulkRequestException::getResponse() method to get more details.',
139-
$exception->getMessage()
140-
);
141-
}
142-
143-
public function test_exception_can_be_throw_with_missing_items_in_response(): void
144-
{
145-
$response = [
146-
'took' => 486,
147-
'errors' => true,
148-
'items' => [],
149-
];
150-
151-
$exception = new BulkRequestException($response);
152-
153-
$this->assertEquals(
154-
'One or more did not complete successfully. Catch the exception and use the DirectoryTree\OpenSearchAdapter\Exceptions\BulkRequestException::getResponse() method to get more details.',
155-
$exception->getMessage()
156-
);
157-
}
158-
}
125+
],
126+
];
127+
128+
$exception = new BulkRequestException($response);
129+
130+
$this->assertEquals(
131+
'1 bulk operation(s) did not complete successfully. Catch the exception and use the DirectoryTree\OpenSearchAdapter\Exceptions\BulkRequestException::getResponse() method to get more details.',
132+
$exception->getMessage()
133+
);
134+
});
135+
136+
test('exception can be thrown with missing items in response', function () {
137+
$response = [
138+
'took' => 486,
139+
'errors' => true,
140+
'items' => [],
141+
];
142+
143+
$exception = new BulkRequestException($response);
144+
145+
$this->assertEquals(
146+
'One or more did not complete successfully. Catch the exception and use the DirectoryTree\OpenSearchAdapter\Exceptions\BulkRequestException::getResponse() method to get more details.',
147+
$exception->getMessage()
148+
);
149+
});

tests/Unit/Indices/AliasTest.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@
33
namespace DirectoryTree\OpenSearchAdapter\Tests\Unit\Indices;
44

55
use DirectoryTree\OpenSearchAdapter\Indices\Alias;
6-
use PHPUnit\Framework\TestCase;
76

8-
class AliasTest extends TestCase
9-
{
10-
public function test_alias_getters(): void
11-
{
12-
$alias = new Alias('2030', ['term' => ['year' => 2030]], 'year');
7+
test('alias getters', function () {
8+
$alias = new Alias('2030', ['term' => ['year' => 2030]], 'year');
139

14-
$this->assertSame('2030', $alias->name());
15-
$this->assertSame(['term' => ['year' => 2030]], $alias->filter());
16-
$this->assertSame('year', $alias->routing());
17-
}
18-
}
10+
$this->assertSame('2030', $alias->name());
11+
$this->assertSame(['term' => ['year' => 2030]], $alias->filter());
12+
$this->assertSame('year', $alias->routing());
13+
});

0 commit comments

Comments
 (0)