Skip to content

Commit

Permalink
Setup test suite calling actual meili instance
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Aug 20, 2024
1 parent 8403c5e commit c2e1573
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 69 deletions.
40 changes: 16 additions & 24 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: Tests

# on: [push, pull_request]
on: []
on: [push, pull_request]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand All @@ -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 }}

Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
44 changes: 17 additions & 27 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
verbose="true"
>
<testsuites>
<testsuite name="Spatie Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<php>
<env name="APP_DEBUG" value="true" />
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Spatie Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<php>
<env name="APP_DEBUG" value="true"/>
</php>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
5 changes: 5 additions & 0 deletions src/Meilisearch/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,9 @@ public function getCount()
{
return $this->getIndex()->stats()['numberOfDocuments'] ?? 0;
}

public function client()
{
return $this->client;
}
}
38 changes: 22 additions & 16 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
<?php

namespace StatamicRadPack\meilisearch\Tests;
namespace StatamicRadPack\Meilisearch\Tests;

use Orchestra\Testbench\TestCase as Orchestra;
use StatamicRadPack\meilisearch\ServiceProvider;
use Statamic\Testing\AddonTestCase;
use StatamicRadPack\Meilisearch\ServiceProvider;

class TestCase extends Orchestra
class TestCase extends AddonTestCase
{
public function setUp(): void
{
parent::setUp();
}
protected string $addonServiceProvider = ServiceProvider::class;

protected function getPackageProviders($app)
{
return [
ServiceProvider::class,
];
}
protected $shouldFakeVersion = true;

public function getEnvironmentSetUp($app)
protected function resolveApplicationConfiguration($app)
{
// ...
parent::resolveApplicationConfiguration($app);

// add driver
$app['config']->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'],
]);
}
}
113 changes: 113 additions & 0 deletions tests/Unit/IndexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace StatamicRadPack\Meilisearch\Tests\Unit;

use Meilisearch\Client;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades;
use StatamicRadPack\Meilisearch\Tests\TestCase;

class IndexTest extends TestCase
{
#[Test]
public function it_sets_up_the_client_correctly()
{
$index = Facades\Search::index('meilisearch_index');

$this->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'));
}
}

0 comments on commit c2e1573

Please sign in to comment.