Skip to content

Commit

Permalink
feat: support older php versions
Browse files Browse the repository at this point in the history
  • Loading branch information
faustoq committed Jul 7, 2024
1 parent b21b574 commit 1fe2661
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 272 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.1, 8.2, 8.3]
php: [7.3, 7.4, 8.0, 8.1, 8.2, 8.3]
stability: [prefer-dist]

name: php ${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}
Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
"OramaCloud\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"require": {
"php": ">=8.1",
"php": ">=7.3",
"guzzlehttp/guzzle": "^7.8"
},
"scripts": {
"test": "vendor/bin/pest"
"test": "vendor/bin/phpunit tests"
},
"require-dev": {
"pestphp/pest": "^2.34"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
"phpunit/phpunit": "^9.5"
}
}
6 changes: 0 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,4 @@
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
101 changes: 54 additions & 47 deletions tests/Feature/ClientTest.php
Original file line number Diff line number Diff line change
@@ -1,55 +1,62 @@
<?php

namespace Tests\Feature;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use OramaCloud\Client;
use OramaCloud\Client\Query;

const API_ENDPOINT = 'mock-endpoint';
const PUBLIC_API_KEY = 'mock-api-key';

test('basic fulltext search', function () {
// Create a mock handler and queue a response.
$mock = new MockHandler([
// initial request to get the collect URL
new Response(200, [], json_encode([
'collectUrl' => 'mock-url',
'deploymentID' => 'mock-deployment-id',
'index' => 'mock-index',
])),
// search request
new Response(200, [], json_encode([
'hits' => [['id' => 2]],
'elapsed' => 0.2,
'count' => 1,
])),
// telemetry data collection
new Response(200, [], json_encode([
'message' => 'Telemetry data collected successfully',
])),
]);

$handlerStack = HandlerStack::create($mock);
$mockClient = new GuzzleClient(['handler' => $handlerStack]);

$client = new Client([
'api_key' => PUBLIC_API_KEY,
'endpoint' => API_ENDPOINT,
], $mockClient);

$result = $client->search(
(new Query())
->term('red shoes')
->mode('fulltext')
->limit(10)
);

expect($result)->toHaveKey('hits');
expect($result)->toHaveKey('elapsed');
expect($result)->toHaveKey('count');

expect($result['count'])->toBeGreaterThan(0);
expect(count($result['hits']))->toBeLessThanOrEqual(10);
});
use Tests\TestCase;

class ClientTest extends TestCase
{
const API_ENDPOINT = 'mock-endpoint';
const PUBLIC_API_KEY = 'mock-api-key';

public function testBasicFulltextSearch()
{
// Create a mock handler and queue a response.
$mock = new MockHandler([
// initial request to get the collect URL
new Response(200, [], json_encode([
'collectUrl' => 'mock-url',
'deploymentID' => 'mock-deployment-id',
'index' => 'mock-index',
])),
// search request
new Response(200, [], json_encode([
'hits' => [['id' => 2]],
'elapsed' => 0.2,
'count' => 1,
])),
// telemetry data collection
new Response(200, [], json_encode([
'message' => 'Telemetry data collected successfully',
])),
]);

$handlerStack = HandlerStack::create($mock);
$mockClient = new GuzzleClient(['handler' => $handlerStack]);

$client = new Client([
'api_key' => self::PUBLIC_API_KEY,
'endpoint' => self::API_ENDPOINT,
], $mockClient);

$result = $client->search(
(new Query())
->term('red shoes')
->mode('fulltext')
->limit(10)
);

$this->assertArrayHasKey('hits', $result);
$this->assertArrayHasKey('elapsed', $result);
$this->assertArrayHasKey('count', $result);

$this->assertGreaterThan(0, $result['count']);
$this->assertLessThanOrEqual(10, count($result['hits']));
}
}
158 changes: 80 additions & 78 deletions tests/Feature/IndexManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,114 +1,116 @@
<?php

namespace Tests\Feature;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use OramaCloud\Manager\CloudManager;
use OramaCloud\Manager\IndexManager;

beforeEach(function () {
// Reset capturedRequests before each test
$this->capturedRequests = [];

// Middleware to capture requests
$captureMiddleware = function (callable $handler) {
return function ($request, array $options) use ($handler) {
$this->capturedRequests[] = $request;
return $handler($request, $options);
use Tests\TestCase;

class IndexManagerTest extends TestCase
{
protected $manager;
protected $index;
protected $capturedRequests;

protected function setUp(): void
{
// Reset capturedRequests before each test
$this->capturedRequests = [];

// Middleware to capture requests
$captureMiddleware = function ($handler) {
return function ($request, $options) use ($handler) {
$this->capturedRequests[] = $request;
return $handler($request, $options);
};
};
};

$mockResponse = new MockHandler([
new Response(200, [], json_encode(['success' => true]))
]);
$mockResponse = new MockHandler([
new Response(200, [], json_encode(['success' => true]))
]);

$handlerStack = HandlerStack::create($mockResponse);
$handlerStack->push($captureMiddleware, 'capture_middleware');
$mockClient = new GuzzleClient(['handler' => $handlerStack]);
$handlerStack = HandlerStack::create($mockResponse);
$handlerStack->push($captureMiddleware, 'capture_middleware');
$mockClient = new GuzzleClient(['handler' => $handlerStack]);

$this->manager = new CloudManager('mock-api-key', $mockClient);
$this->index = new IndexManager('mock-index', $this->manager);
});
$this->manager = new CloudManager('mock-api-key', $mockClient);
$this->index = new IndexManager('mock-index', $this->manager);
}

describe('Index manager', function () {
it('should empty the index', function () {
public function testShouldEmptyTheIndex()
{
// Empty index
$result = $this->index->empty();
expect($result)->toHaveKey('success');

$this->index->empty();
$lastRequest = end($this->capturedRequests);
expect($lastRequest->getMethod())->toBe('POST');
expect($lastRequest->getUri()->getPath())->toBe('/api/v1/webhooks/mock-index/snapshot');
expect(json_decode($lastRequest->getBody()->getContents(), true))->toBe(null);
});
$this->assertEquals('POST', $lastRequest->getMethod());
$this->assertEquals('/api/v1/webhooks/mock-index/snapshot', $lastRequest->getUri()->getPath());
$this->assertNull(json_decode($lastRequest->getBody()->getContents(), true));
}

it('should insert a document', function () {
public function testShouldInsertADocument()
{
$data = ['id' => 1, 'name' => 'John Doe'];
// Insert document
$result = $this->index->insert($data);
expect($result)->toHaveKey('success');

$this->index->insert($data);
$lastRequest = end($this->capturedRequests);
expect($lastRequest->getMethod())->toBe('POST');
expect($lastRequest->getUri()->getPath())->toBe('/api/v1/webhooks/mock-index/notify');
expect(json_decode($lastRequest->getBody()->getContents(), true))->toBe(['upsert' => $data]);
});
$this->assertEquals('POST', $lastRequest->getMethod());
$this->assertEquals('/api/v1/webhooks/mock-index/notify', $lastRequest->getUri()->getPath());
$this->assertEquals(['upsert' => $data], json_decode($lastRequest->getBody()->getContents(), true));
}

it('should update a document', function () {
public function testShouldUpdateADocument()
{
$data = ['id' => 1, 'name' => 'Jane Doe'];
// Update document
$result = $this->index->update($data);
expect($result)->toHaveKey('success');

$this->index->update($data);
$lastRequest = end($this->capturedRequests);
expect($lastRequest->getMethod())->toBe('POST');
expect($lastRequest->getUri()->getPath())->toBe('/api/v1/webhooks/mock-index/notify');
expect(json_decode($lastRequest->getBody()->getContents(), true))->toBe(['upsert' => $data]);
});
$this->assertEquals('POST', $lastRequest->getMethod());
$this->assertEquals('/api/v1/webhooks/mock-index/notify', $lastRequest->getUri()->getPath());
$this->assertEquals(['upsert' => $data], json_decode($lastRequest->getBody()->getContents(), true));
}

it('should delete a document', function () {
public function testShouldDeleteADocument()
{
// Delete index
$result = $this->index->delete(['id' => 1]);
expect($result)->toHaveKey('success');

$this->index->delete(['id' => 1]);
$lastRequest = end($this->capturedRequests);
expect($lastRequest->getMethod())->toBe('POST');
expect($lastRequest->getUri()->getPath())->toBe('/api/v1/webhooks/mock-index/notify');
expect(json_decode($lastRequest->getBody()->getContents(), true))->toBe(['remove' => ['id' => 1]]);
});
$this->assertEquals('POST', $lastRequest->getMethod());
$this->assertEquals('/api/v1/webhooks/mock-index/notify', $lastRequest->getUri()->getPath());
$this->assertEquals(['remove' => ['id' => 1]], json_decode($lastRequest->getBody()->getContents(), true));
}

it('should snapshot the index', function () {
public function testShouldSnapshotTheIndex()
{
$data = ['id' => 1, 'name' => 'John Doe'];
// Snapshot index
$result = $this->index->snapshot($data);
expect($result)->toHaveKey('success');

$this->index->snapshot($data);
$lastRequest = end($this->capturedRequests);
expect($lastRequest->getMethod())->toBe('POST');
expect($lastRequest->getUri()->getPath())->toBe('/api/v1/webhooks/mock-index/snapshot');
expect(json_decode($lastRequest->getBody()->getContents(), true))->toBe($data);
});
$this->assertEquals('POST', $lastRequest->getMethod());
$this->assertEquals('/api/v1/webhooks/mock-index/snapshot', $lastRequest->getUri()->getPath());
$this->assertEquals($data, json_decode($lastRequest->getBody()->getContents(), true));
}

it('should deploy the index', function () {
public function testShouldDeployTheIndex()
{
// Deploy index
$result = $this->index->deploy();
expect($result)->toHaveKey('success');

$this->index->deploy();
$lastRequest = end($this->capturedRequests);
expect($lastRequest->getMethod())->toBe('POST');
expect($lastRequest->getUri()->getPath())->toBe('/api/v1/webhooks/mock-index/deploy');
expect(json_decode($lastRequest->getBody()->getContents(), true))->toBe(null);
});
$this->assertEquals('POST', $lastRequest->getMethod());
$this->assertEquals('/api/v1/webhooks/mock-index/deploy', $lastRequest->getUri()->getPath());
$this->assertNull(json_decode($lastRequest->getBody()->getContents(), true));
}

it('should check for pending operations', function () {
public function testShouldCheckForPendingOperations()
{
// Check for pending operations
$result = $this->index->hasPendingOperations();
expect($result)->toBe(false);

$this->index->hasPendingOperations();
$lastRequest = end($this->capturedRequests);
expect($lastRequest->getMethod())->toBe('POST');
expect($lastRequest->getUri()->getPath())->toBe('/api/v1/webhooks/mock-index/has-data');
expect(json_decode($lastRequest->getBody()->getContents(), true))->toBe(null);
});
});
$this->assertEquals('POST', $lastRequest->getMethod());
$this->assertEquals('/api/v1/webhooks/mock-index/has-data', $lastRequest->getUri()->getPath());
$this->assertNull(json_decode($lastRequest->getBody()->getContents(), true));
}
}
45 changes: 0 additions & 45 deletions tests/Pest.php

This file was deleted.

Loading

0 comments on commit 1fe2661

Please sign in to comment.