Skip to content

Commit

Permalink
chore: phpunit with real typesense server
Browse files Browse the repository at this point in the history
BREAKING CHANGE: ClientSingletonFactory->getClient is now protected
  • Loading branch information
ragusa87 committed Feb 23, 2025
1 parent 2d779cf commit 032f3b5
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
DATABASE_URL="sqlite:///%kernel.project_dir%/typesense-test.db"
APP_SECRET=460c14d1f74aeb10cffae741fcaaddac773b3260
APP_ENV=test
APP_ENV=test
TYPESENSE_API_KEY=xyz
TYPESENSE_URL="http://127.0.0.1:8108"
13 changes: 12 additions & 1 deletion .github/workflows/coding-standards.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ on:
push:
branches:
- "main"

jobs:
conventional-commits:
name: "conventional-commits"
Expand All @@ -36,6 +35,18 @@ jobs:
php-version: "8.2"
- composer-versions: "lowest"
php-version: "8.4"

services:
typesense:
image: typesense/typesense:28.0
ports:
- 8108:8108/tcp
volumes:
- /tmp/typesense-server-data:/data
env:
TYPESENSE_DATA_DIR: /data
TYPESENSE_API_KEY: xyz
TYPESENSE_ENABLE_CORS: true
steps:
- name: "Checkout"
uses: "actions/checkout@v4"
Expand Down
25 changes: 16 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@ services:
tty: true
environment:
- PHP_IDE_CONFIG=serverName=typesensebundle
- TYPESENSE_URL=http://typesense:8108
- TYPESENSE_API_KEY=xyz
extra_hosts:
- host.docker.internal:host-gateway
networks:
- default
# typesense:
# image: typesense/typesense:27.1
# restart: on-failure
# ports:
# - 8983
# - "9908:8108"
# volumes:
# - searchdata:/data
# command: '--data-dir /data --api-key=xyz --enable-cors'
typesense:
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8108/health"]
links:
- php
image: typesense/typesense:28.0
restart: on-failure
volumes:
- typesense-server-data:/data
environment:
TYPESENSE_DATA_DIR: /data
TYPESENSE_API_KEY: xyz
TYPESENSE_ENABLE_CORS: true
volumes:
searchdata:
typesense-server-data:
2 changes: 1 addition & 1 deletion src/Client/ClientSingletonFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private function getConfiguration(): array
return array_merge($this->defaultConfig, $config);
}

public function getClient(): HttpClient
protected function getClient(): HttpClient
{
$client = $this->httpClient ?? (new Psr18ClientDiscovery())->find();

Expand Down
47 changes: 47 additions & 0 deletions tests/RealInstanceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Biblioverse\TypesenseBundle\Tests;

use Biblioverse\TypesenseBundle\Client\ClientInterface;
use Biblioverse\TypesenseBundle\Client\ClientSingletonFactory;
use Http\Discovery\Psr18ClientDiscovery;
use PHPUnit\Framework\TestCase;
use Typesense\Exceptions\ConfigError;

class RealInstanceTest extends TestCase
{
/**
* @throws ConfigError
*/
protected function getClient(): ?ClientInterface
{
$httpClient = (new Psr18ClientDiscovery())->find();

/** @var string|null $url */
$url = $_ENV['TYPESENSE_URL'] ?? null;
/** @var string|null $apiKey */
$apiKey = $_ENV['TYPESENSE_API_KEY'] ?? null;

if ($url === null || $apiKey === null) {
return null;
}

$clientSingletonFactory = new ClientSingletonFactory($url, $apiKey, $httpClient);

return $clientSingletonFactory->__invoke();
}

/**
* @throws \Exception
*/
public function testCanRetrieveMetrics(): void
{
$client = $this->getClient();
if (!$client instanceof ClientInterface) {
$this->markTestSkipped('env TYPESENSE_URL or TYPESENSE_API_KEY not provided');
}

$returnData = $client->getMetrics()->retrieve();
$this->assertArrayHasKey('system_memory_used_bytes', $returnData);
}
}

0 comments on commit 032f3b5

Please sign in to comment.