Skip to content

Commit

Permalink
Add tests for DSN parsing in AdapterFactory (#454)
Browse files Browse the repository at this point in the history
To make #452 better
testable I added a few tests to for the AdapterFactory parseDsn part.
  • Loading branch information
alexander-schranz authored Nov 3, 2024
1 parent 2feaf7c commit 62b37d8
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/seal/src/Adapter/AdapterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function parseDsn(string $dsn): array

if (\str_contains($dsn, ':///')) {
// make DSN like loupe:///full/path/project/var/indexes parseable
$dsn = \str_replace(':///', '://' . $adapterName . '/', $dsn);
$dsn = \str_replace(':///', '://' . $adapterName . '/', $dsn . $query);
} else {
$dsn = $dsn . '@' . $adapterName . $query;
}
Expand Down
180 changes: 180 additions & 0 deletions packages/seal/tests/Adapter/AdapterFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Schranz Search package.
*
* (c) Alexander Schranz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Adapter;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Schranz\Search\SEAL\Adapter\AdapterFactory;
use Schranz\Search\SEAL\Adapter\AdapterFactoryInterface;
use Schranz\Search\SEAL\Adapter\AdapterInterface;

#[CoversClass(AdapterFactory::class)]
class AdapterFactoryTest extends TestCase
{
private AdapterFactory $adapterFactory;

protected function setUp(): void
{
$this->adapterFactory = new AdapterFactory([
'scheme' => $this->createAdapterFactory('scheme'),
]);
}

/**
* @param array{
* scheme: string,
* host: string,
* port?: int,
* user?: string,
* pass?: string,
* path?: string,
* query: array<string, string>,
* fragment?: string,
* } $expectedResult
*/
#[DataProvider('provideDsn')]
public function testParseDsn(string $dsn, array $expectedResult): void
{
$parsedDsn = $this->adapterFactory->parseDsn($dsn);

$this->assertSame($expectedResult, $parsedDsn);
}

public function testAdapterNotFound(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown Search adapter: "not-found" available adapters are "scheme".');

$this->adapterFactory->parseDsn('not-found://host:1234');
}

public function testInvalidDsn(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid DSN: "".');

$this->adapterFactory->parseDsn('');
}

/**
* @return \Generator<array{0: string, 1: array{
* scheme: string,
* host: string,
* port?: int,
* user?: string,
* pass?: string,
* path?: string,
* query: array<string, string>,
* fragment?: string,
* }}>
*/
public static function provideDsn(): \Generator
{
yield 'standard' => [
'scheme://host:1234',
[
'scheme' => 'scheme',
'host' => 'host',
'port' => 1234,
'query' => [],
],
];

yield 'standard_full' => [
'scheme://user:pass@host:1234/path?queryKey=queryValue#fragment',
[
'scheme' => 'scheme',
'host' => 'host',
'port' => 1234,
'user' => 'user',
'pass' => 'pass',
'path' => '/path',
'query' => [
'queryKey' => 'queryValue',
],
'fragment' => 'fragment',
],
];

yield 'only_username_password' => [
'scheme://user:pass',
[
'scheme' => 'scheme',
'host' => '',
'user' => 'user',
'pass' => 'pass',
'query' => [],
],
];

yield 'only_username_password_with_query' => [
'scheme://user:pass?queryKey=queryValue#fragment',
[
'scheme' => 'scheme',
'host' => '',
'user' => 'user',
'pass' => 'pass',
'query' => [
'queryKey' => 'queryValue',
],
'fragment' => 'fragment',
],
];

yield 'path' => [
'scheme:///var/data/directory',
[
'scheme' => 'scheme',
'host' => '',
'path' => '/var/data/directory',
'query' => [],
],
];

yield 'path_with_query' => [
'scheme:///var/data/directory?queryKey=queryValue#fragment',
[
'scheme' => 'scheme',
'host' => '',
'path' => '/var/data/directory',
'query' => [
'queryKey' => 'queryValue',
],
'fragment' => 'fragment',
],
];
}

private function createAdapterFactory(string $name): AdapterFactoryInterface
{
$adapterFactory = new class() implements AdapterFactoryInterface {
public static string $name;

public function createAdapter(array $dsn): AdapterInterface
{
throw new \Exception('Not implemented');
}

public static function getName(): string
{
return static::$name;
}
};

$adapterFactory::$name = $name;

return $adapterFactory;
}
}
2 changes: 2 additions & 0 deletions packages/seal/tests/Marshaller/FlattenMarshallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

namespace Schranz\Search\SEAL\Tests\Marshaller;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Schranz\Search\SEAL\Marshaller\FlattenMarshaller;
use Schranz\Search\SEAL\Schema\Field;

#[CoversClass(FlattenMarshaller::class)]
class FlattenMarshallerTest extends TestCase
{
/**
Expand Down
2 changes: 2 additions & 0 deletions packages/seal/tests/Marshaller/MarshallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

namespace Schranz\Search\SEAL\Tests\Marshaller;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Schranz\Search\SEAL\Marshaller\Marshaller;
use Schranz\Search\SEAL\Testing\TestingHelper;

#[CoversClass(Marshaller::class)]
class MarshallerTest extends TestCase
{
public function testMarshall(): void
Expand Down
2 changes: 2 additions & 0 deletions packages/seal/tests/Schema/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

namespace Schranz\Search\SEAL\Tests\Schema;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Schranz\Search\SEAL\Schema\Field;
use Schranz\Search\SEAL\Schema\Index;

#[CoversClass(Index::class)]
class IndexTest extends TestCase
{
public function testIndex(): void
Expand Down
2 changes: 2 additions & 0 deletions packages/seal/tests/Schema/Loader/PhpFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

namespace Schranz\Search\SEAL\Tests\Schema\Loader;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Schranz\Search\SEAL\Schema\Field;
use Schranz\Search\SEAL\Schema\Loader\PhpFileLoader;

#[CoversClass(PhpFileLoader::class)]
class PhpFileLoaderTest extends TestCase
{
public function testLoadBasic(): void
Expand Down

0 comments on commit 62b37d8

Please sign in to comment.