Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for DSN parsing in AdapterFactory #454

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <alexander@sulu.io>
*
* 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
Loading