|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Laravel\Tests; |
| 4 | + |
| 5 | +use MongoDB\Builder\Search; |
| 6 | +use MongoDB\Collection; |
| 7 | +use MongoDB\Driver\Exception\ServerException; |
| 8 | +use MongoDB\Laravel\Tests\Models\Book; |
| 9 | + |
| 10 | +use function assert; |
| 11 | +use function in_array; |
| 12 | +use function usleep; |
| 13 | + |
| 14 | +class AtlasSearchTest extends TestCase |
| 15 | +{ |
| 16 | + public function setUp(): void |
| 17 | + { |
| 18 | + parent::setUp(); |
| 19 | + |
| 20 | + Book::insert([ |
| 21 | + ['title' => 'Introduction to Algorithms'], |
| 22 | + ['title' => 'Clean Code: A Handbook of Agile Software Craftsmanship'], |
| 23 | + ['title' => 'Design Patterns: Elements of Reusable Object-Oriented Software'], |
| 24 | + ['title' => 'The Pragmatic Programmer: Your Journey to Mastery'], |
| 25 | + ['title' => 'Artificial Intelligence: A Modern Approach'], |
| 26 | + ['title' => 'Structure and Interpretation of Computer Programs'], |
| 27 | + ['title' => 'Code Complete: A Practical Handbook of Software Construction'], |
| 28 | + ['title' => 'The Art of Computer Programming'], |
| 29 | + ['title' => 'Computer Networks'], |
| 30 | + ['title' => 'Operating System Concepts'], |
| 31 | + ['title' => 'Database System Concepts'], |
| 32 | + ['title' => 'Compilers: Principles, Techniques, and Tools'], |
| 33 | + ['title' => 'Introduction to the Theory of Computation'], |
| 34 | + ['title' => 'Modern Operating Systems'], |
| 35 | + ['title' => 'Computer Organization and Design'], |
| 36 | + ['title' => 'The Mythical Man-Month: Essays on Software Engineering'], |
| 37 | + ['title' => 'Algorithms'], |
| 38 | + ['title' => 'Understanding Machine Learning: From Theory to Algorithms'], |
| 39 | + ['title' => 'Deep Learning'], |
| 40 | + ['title' => 'Pattern Recognition and Machine Learning'], |
| 41 | + ]); |
| 42 | + |
| 43 | + $collection = $this->getConnection('mongodb')->getCollection('books'); |
| 44 | + assert($collection instanceof Collection); |
| 45 | + try { |
| 46 | + $collection->createSearchIndex([ |
| 47 | + 'mappings' => [ |
| 48 | + 'fields' => [ |
| 49 | + 'title' => [ |
| 50 | + [ |
| 51 | + 'type' => 'string', |
| 52 | + 'analyzer' => 'lucene.english', |
| 53 | + ], |
| 54 | + [ |
| 55 | + 'type' => 'autocomplete', |
| 56 | + 'analyzer' => 'lucene.english', |
| 57 | + ], |
| 58 | + ], |
| 59 | + ], |
| 60 | + ], |
| 61 | + ]); |
| 62 | + } catch (ServerException $e) { |
| 63 | + if (in_array($e->getCode(), [59, 40324, 115, 6047401, 31082])) { |
| 64 | + self::markTestSkipped('Atlas Search not supported. ' . $e->getMessage()); |
| 65 | + } |
| 66 | + |
| 67 | + throw $e; |
| 68 | + } |
| 69 | + |
| 70 | + // Wait for the index to be ready |
| 71 | + do { |
| 72 | + usleep(10_000); |
| 73 | + $index = $collection->listSearchIndexes(['name' => 'default'])->current(); |
| 74 | + } while ($index['status'] !== 'READY'); |
| 75 | + } |
| 76 | + |
| 77 | + public function tearDown(): void |
| 78 | + { |
| 79 | + $this->getConnection('mongodb')->getCollection('books')->drop(); |
| 80 | + |
| 81 | + parent::tearDown(); |
| 82 | + } |
| 83 | + |
| 84 | + public function testEloquentBuilderSearch() |
| 85 | + { |
| 86 | + $results = Book::search(Search::text('title', 'systems')); |
| 87 | + |
| 88 | + self::assertInstanceOf(\Illuminate\Database\Eloquent\Collection::class, $results); |
| 89 | + self::assertCount(3, $results); |
| 90 | + self::assertInstanceOf(Book::class, $results->first()); |
| 91 | + self::assertSame([ |
| 92 | + 'Operating System Concepts', |
| 93 | + 'Database System Concepts', |
| 94 | + 'Modern Operating Systems', |
| 95 | + ], $results->pluck('title')->all()); |
| 96 | + } |
| 97 | + |
| 98 | + public function testDatabaseBuilderSearch() |
| 99 | + { |
| 100 | + $results = $this->getConnection('mongodb')->table('books') |
| 101 | + ->search(Search::text('title', 'systems')); |
| 102 | + |
| 103 | + self::assertInstanceOf(\Illuminate\Support\Collection::class, $results); |
| 104 | + self::assertCount(3, $results); |
| 105 | + self::assertIsArray($results->first()); |
| 106 | + self::assertSame([ |
| 107 | + 'Operating System Concepts', |
| 108 | + 'Database System Concepts', |
| 109 | + 'Modern Operating Systems', |
| 110 | + ], $results->pluck('title')->all()); |
| 111 | + } |
| 112 | + |
| 113 | + public function testEloquentBuilderAutocomplete() |
| 114 | + { |
| 115 | + $results = Book::autocomplete('title', 'system'); |
| 116 | + |
| 117 | + self::assertInstanceOf(\Illuminate\Support\Collection::class, $results); |
| 118 | + self::assertCount(3, $results); |
| 119 | + self::assertSame([ |
| 120 | + 'Operating System Concepts', |
| 121 | + 'Database System Concepts', |
| 122 | + 'Modern Operating Systems', |
| 123 | + ], $results->all()); |
| 124 | + } |
| 125 | + |
| 126 | + public function testDatabaseBuilderAutocomplete() |
| 127 | + { |
| 128 | + $results = $this->getConnection('mongodb')->table('books') |
| 129 | + ->autocomplete('title', 'system'); |
| 130 | + |
| 131 | + self::assertInstanceOf(\Illuminate\Support\Collection::class, $results); |
| 132 | + self::assertCount(3, $results); |
| 133 | + self::assertSame([ |
| 134 | + 'Operating System Concepts', |
| 135 | + 'Database System Concepts', |
| 136 | + 'Modern Operating Systems', |
| 137 | + ], $results->all()); |
| 138 | + } |
| 139 | +} |
0 commit comments