|
3 | 3 | namespace Feature; |
4 | 4 |
|
5 | 5 | use Tests\TestCase; |
| 6 | +use Typesense\Documents; |
6 | 7 |
|
7 | 8 | class DocumentsTest extends TestCase |
8 | 9 | { |
| 10 | + private ?Documents $testDocuments = null; |
| 11 | + private $document = [ |
| 12 | + "id" => "4", |
| 13 | + "title" => "Book 4", |
| 14 | + "description" => "Test...", |
| 15 | + "authors" => ["Hayden"], |
| 16 | + "isbn" => "1", |
| 17 | + "publisher" => "AwesomeBooks", |
| 18 | + "pages" => 9 |
| 19 | + ]; |
| 20 | + |
9 | 21 | protected function setUp(): void |
10 | 22 | { |
11 | 23 | parent::setUp(); |
12 | | - |
13 | 24 | $this->setUpCollection('books'); |
14 | 25 | $this->setUpDocuments('books'); |
| 26 | + |
| 27 | + $this->testDocuments = $this->client()->collections['books']->documents; |
| 28 | + } |
| 29 | + |
| 30 | + public function testCanSearchForDocumentsInACollection(): void |
| 31 | + { |
| 32 | + $response = $this->testDocuments->search([ |
| 33 | + 'q' => 'book', |
| 34 | + 'query_by' => 'title', |
| 35 | + ]); |
| 36 | + $this->assertEquals('book', $response['request_params']['q']); |
| 37 | + $this->assertGreaterThan(0, $response['hits']); |
| 38 | + } |
| 39 | + |
| 40 | + public function testCanCreateADocument(): void |
| 41 | + { |
| 42 | + $response = $this->testDocuments->create($this->document); |
| 43 | + $this->assertEquals($this->document, $response); |
| 44 | + } |
| 45 | + |
| 46 | + public function testCanUpsertADocument(): void |
| 47 | + { |
| 48 | + $documentWithDifferentId = [...$this->document, 'id' => '1']; // id 1 already exists in the collection |
| 49 | + |
| 50 | + $response = $this->testDocuments->upsert($documentWithDifferentId); |
| 51 | + $this->assertEquals($documentWithDifferentId, $response); |
| 52 | + } |
| 53 | + |
| 54 | + public function testCanImportJsonlDocumentsWithQueryParameter(): void |
| 55 | + { |
| 56 | + $documentWithDifferentId = [...$this->document, 'id' => '1']; |
| 57 | + $jsonlDocuments = join(PHP_EOL, array_map( |
| 58 | + fn ($item) => json_encode($item), |
| 59 | + [$this->document, $documentWithDifferentId] |
| 60 | + )); |
| 61 | + |
| 62 | + $response = $this->testDocuments->import($jsonlDocuments, [ |
| 63 | + 'action' => 'upsert' |
| 64 | + ]); |
| 65 | + $this->assertEquals("{\"success\":true}\n{\"success\":true}", $response); |
15 | 66 | } |
16 | 67 |
|
17 | | - public function testCanUpdateDocumentsByFilter(): void |
| 68 | + public function testCanImportArrayOfDocuments(): void |
| 69 | + { |
| 70 | + $documentWithDifferentId = [...$this->document, 'id' => '5']; |
| 71 | + |
| 72 | + $response = $this->testDocuments->import([$this->document, $documentWithDifferentId]); |
| 73 | + $this->assertEquals(1, $response[0]['success']); |
| 74 | + $this->assertEquals(1, $response[1]['success']); |
| 75 | + } |
| 76 | + |
| 77 | + public function testCanExportDocumentsWithControlParameter(): void |
| 78 | + { |
| 79 | + $this->testDocuments->create($this->document); |
| 80 | + |
| 81 | + $response = $this->testDocuments->export([ |
| 82 | + "filter_by" => "id:=4" |
| 83 | + ]); |
| 84 | + $this->assertEquals($this->document, json_decode($response, true)); |
| 85 | + } |
| 86 | + |
| 87 | + public function testCanUpdateDocumentsByQuery(): void |
18 | 88 | { |
19 | 89 | $document = ['publisher' => 'Renamed Publisher']; |
20 | 90 |
|
21 | | - $response = $this->client()->collections['books']->documents->update($document, [ |
| 91 | + $response = $this->testDocuments->update($document, [ |
22 | 92 | 'filter_by' => 'publisher:=AwesomeBooks' |
23 | 93 | ]); |
24 | | - |
25 | 94 | $this->assertGreaterThan(0, $response['num_updated']); |
26 | 95 | } |
| 96 | + |
| 97 | + public function testCanDeleteDocumentsByQuery(): void |
| 98 | + { |
| 99 | + $response = $this->testDocuments->delete([ |
| 100 | + 'filter_by' => 'publisher:=AwesomeBooks' |
| 101 | + ]); |
| 102 | + $this->assertGreaterThan(0, $response['num_deleted']); |
| 103 | + } |
27 | 104 | } |
0 commit comments