From c2e15730a1c6c7f5aa6253d9f5380294914a32dd Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 20 Aug 2024 08:03:29 +0100 Subject: [PATCH] Setup test suite calling actual meili instance --- .github/workflows/run-tests.yml | 40 +++++------ composer.json | 5 +- phpunit.xml.dist | 44 +++++-------- src/Meilisearch/Index.php | 5 ++ tests/TestCase.php | 38 ++++++----- tests/Unit/IndexTest.php | 113 ++++++++++++++++++++++++++++++++ 6 files changed, 176 insertions(+), 69 deletions(-) create mode 100644 tests/Unit/IndexTest.php diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 917ef33..2073b55 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -1,7 +1,6 @@ name: Tests -# on: [push, pull_request] -on: [] +on: [push, pull_request] concurrency: group: ${{ github.workflow }}-${{ github.ref }} @@ -10,31 +9,24 @@ concurrency: jobs: test: runs-on: ${{ matrix.os }} + + services: + meilisearch: + image: getmeili/meilisearch:latest + ports: + - 7700:7700 + env: + MEILI_MASTER_KEY: masterKey + MEILI_NO_ANALYTICS: true + strategy: fail-fast: false matrix: - os: [ubuntu-latest, windows-latest] - php: [8.2, 8.1] - laravel: [10.*, 9.*] - statamic: [4.*, 3.4.*, 3.3.*] + os: [ubuntu-latest] + php: [8.2, 8.3] + laravel: [10.*, 11.*] + statamic: [5.*] dependency-version: [prefer-stable] - include: - - laravel: 10.* - testbench: 8.* - - laravel: 9.* - testbench: 7.* - exclude: - - laravel: 10.* - statamic: 3.4.* - - laravel: 10.* - statamic: 3.3.* - php: 8.2 - - laravel: 10.* - statamic: 3.3.* - php: 8.1 - - laravel: 9.* - statamic: 3.3.* - php: 8.2 name: P${{ matrix.php }} - L${{ matrix.laravel }} - S${{ matrix.statamic }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} @@ -56,7 +48,7 @@ jobs: - name: Install dependencies run: | - composer require "statamic/cms:${{ matrix.statamic }}" "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench-core:${{ matrix.testbench }}" --no-interaction --no-update + composer require "statamic/cms:${{ matrix.statamic }}" "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction - name: Execute tests diff --git a/composer.json b/composer.json index 16c6068..8abbae8 100644 --- a/composer.json +++ b/composer.json @@ -31,8 +31,9 @@ "statamic/cms": "^5.0" }, "require-dev": { - "orchestra/testbench": "^7.0 || ^8.0", - "phpunit/phpunit": "^9.3" + "laravel/pint": "^1.17", + "orchestra/testbench": "^8.14 || ^9.0", + "phpunit/phpunit": "^10.0" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d6b25be..b54e9dc 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,29 +1,19 @@ - - - - tests - - - - - ./src - - - - - - - - + + + + tests + + + + + + + + + + + ./src + + diff --git a/src/Meilisearch/Index.php b/src/Meilisearch/Index.php index 7ebb9d8..5b0fa55 100644 --- a/src/Meilisearch/Index.php +++ b/src/Meilisearch/Index.php @@ -169,4 +169,9 @@ public function getCount() { return $this->getIndex()->stats()['numberOfDocuments'] ?? 0; } + + public function client() + { + return $this->client; + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index a266e7e..58a3803 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,26 +1,32 @@ set('statamic.search.drivers.meilisearch', [ + 'credentials' => [ + 'url' => 'http://localhost:7700', + 'secret' => 'LARAVEL-HERD', + ], + ]); + + // add index + $app['config']->set('statamic.search.indexes.meilisearch_index', [ + 'driver' => 'meilisearch', + 'searchables' => ['collection:pages'], + ]); } } diff --git a/tests/Unit/IndexTest.php b/tests/Unit/IndexTest.php new file mode 100644 index 0000000..c5b8a36 --- /dev/null +++ b/tests/Unit/IndexTest.php @@ -0,0 +1,113 @@ +assertInstanceOf(Client::class, $index->client()); + } + + #[Test] + public function it_adds_documents_to_the_index() + { + $collection = Facades\Collection::make() + ->handle('pages') + ->title('Pages') + ->save(); + + $entry1 = Facades\Entry::make() + ->id('test-2') + ->collection('pages') + ->data(['title' => 'Entry 1']) + ->save(); + + $entry2 = Facades\Entry::make() + ->id('test-1') + ->collection('pages') + ->data(['title' => 'Entry 2']) + ->save(); + + sleep(1); // give meili some time to process + + $index = Facades\Search::index('meilisearch_index'); + + $this->assertCount(2, $index->searchUsingApi('Entry')); + } + + #[Test] + public function it_updates_documents_to_the_index() + { + $collection = Facades\Collection::make() + ->handle('pages') + ->title('Pages') + ->save(); + + $entry1 = Facades\Entry::make() + ->id('test-2') + ->collection('pages') + ->data(['title' => 'Entry 1']) + ->save(); + + $entry2 = tap(Facades\Entry::make() + ->id('test-1') + ->collection('pages') + ->data(['title' => 'Entry 2'])) + ->save(); + + sleep(1); // give meili some time to process + + $index = Facades\Search::index('meilisearch_index'); + + $results = collect($index->searchUsingApi('Entry'))->pluck('title'); + + $this->assertContains('Entry 1', $results); + $this->assertContains('Entry 2', $results); + + $entry2->merge(['title' => 'Entry 2 Updated'])->save(); + + sleep(1); // give meili some time to process + + $results = collect($index->searchUsingApi('Entry'))->pluck('title'); + + $this->assertContains('Entry 2 Updated', $results); + } + + #[Test] + public function it_removes_documents_from_the_index() + { + $collection = Facades\Collection::make() + ->handle('pages') + ->title('Pages') + ->save(); + + $entry1 = Facades\Entry::make() + ->id('test-2') + ->collection('pages') + ->data(['title' => 'Entry 1']) + ->save(); + + $entry2 = tap(Facades\Entry::make() + ->id('test-1') + ->collection('pages') + ->data(['title' => 'Entry 2'])) + ->save(); + + $entry2->delete(); + + $index = Facades\Search::index('meilisearch_index'); + + sleep(1); // give meili some time to process + + $this->assertCount(1, $index->searchUsingApi('Entry')); + } +}