Skip to content

Commit

Permalink
Add PhpProvider (#82)
Browse files Browse the repository at this point in the history
* Add basic tests for YamlProvider

* PHP Mapping Provider

* Fix CS
  • Loading branch information
jmsche authored Jul 23, 2021
1 parent 223a849 commit 559de61
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Extract code to manage index name from `JoliCode\Elastically\Client` to
`JoliCode\Elastically\IndexNameMapper`
- Introduce `JoliCode\Elastically\Mapping\MappingProviderInterface` and concrete
implementation: `JoliCode\Elastically\Mapping\YamlProvider`
implementations: `JoliCode\Elastically\Mapping\YamlProvider` and
`JoliCode\Elastically\Mapping\PhpProvider`

### Deprecated

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ An instance of `MappingProviderInterface`.
If this option is not defined, the factory will fallback to `YamlProvider` and will use
`Factory::CONFIG_MAPPINGS_DIRECTORY` option.

There are two providers available in Elastically: `YamlProvider` and `PhpProvider`.

### `Factory::CONFIG_SERIALIZER` (optional)

A `SerializerInterface` compatible object that will by used on indexation.
Expand Down
49 changes: 49 additions & 0 deletions src/Mapping/PhpProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

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

namespace JoliCode\Elastically\Mapping;

use Elastica\Exception\InvalidException;

final class PhpProvider implements MappingProviderInterface
{
private string $configurationDirectory;

public function __construct(string $configurationDirectory)
{
$this->configurationDirectory = $configurationDirectory;
}

public function provideMapping(string $indexName, array $context = []): ?array
{
$fileName = $context['filename'] ?? ($indexName . '_mapping.php');
$mappingFilePath = $this->configurationDirectory . \DIRECTORY_SEPARATOR . $fileName;
if (!is_file($mappingFilePath)) {
throw new InvalidException(sprintf('Mapping file "%s" not found.', $mappingFilePath));
}

$mapping = require $mappingFilePath;
if (1 === $mapping) {
// File seems to be empty
return null;
}

$analyzerFilePath = $this->configurationDirectory . \DIRECTORY_SEPARATOR . 'analyzers.php';
if ($mapping && is_file($analyzerFilePath)) {
$analyzer = require $analyzerFilePath;
$mapping['settings']['analysis'] = array_merge_recursive($mapping['settings']['analysis'] ?? [], $analyzer);
}

return $mapping;
}
}
58 changes: 58 additions & 0 deletions tests/Mapping/PhpProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

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

namespace JoliCode\Elastically\Tests\Mapping;

use Elastica\Exception\InvalidException;
use JoliCode\Elastically\Mapping\PhpProvider;
use PHPUnit\Framework\TestCase;

final class PhpProviderTest extends TestCase
{
public function testNonExistentFileThrowsException(): void
{
$this->expectException(InvalidException::class);
$provider = new PhpProvider(__DIR__ . '/../configs');
$provider->provideMapping('unknown');
}

public function testMappingIsNullWhenConfigIsEmpty(): void
{
$provider = new PhpProvider(__DIR__ . '/../configs');
self::assertNull($provider->provideMapping('empty'));
}

public function testMappingContainsConfiguredAnalyzers(): void
{
$provider = new PhpProvider(__DIR__ . '/../configs_analysis');
$beerMapping = $provider->provideMapping('foo');
// Make sure the structure has both mapping & configured analyzers.
self::assertSame([
'mappings' => [
'properties' => [
'name' => ['type' => 'keyword'],
],
],
'settings' => [
'analysis' => [
'analyzer' => [
'beer_name' => [
'tokenizer' => 'classic',
'filter' => ['elision'],
],
],
],
],
], $beerMapping);
}
}
58 changes: 58 additions & 0 deletions tests/Mapping/YamlProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

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

namespace JoliCode\Elastically\Tests\Mapping;

use Elastica\Exception\InvalidException;
use JoliCode\Elastically\Mapping\YamlProvider;
use PHPUnit\Framework\TestCase;

final class YamlProviderTest extends TestCase
{
public function testNonExistentFileThrowsException(): void
{
$this->expectException(InvalidException::class);
$provider = new YamlProvider(__DIR__ . '/../configs');
$provider->provideMapping('unknown');
}

public function testMappingIsNullWhenConfigIsEmpty(): void
{
$provider = new YamlProvider(__DIR__ . '/../configs');
self::assertNull($provider->provideMapping('empty'));
}

public function testMappingContainsConfiguredAnalyzers(): void
{
$provider = new YamlProvider(__DIR__ . '/../configs_analysis');
$beerMapping = $provider->provideMapping('foo');
// Make sure the structure has both mapping & configured analyzers.
self::assertSame([
'mappings' => [
'properties' => [
'name' => ['type' => 'text'],
],
],
'settings' => [
'analysis' => [
'analyzer' => [
'beer_name' => [
'tokenizer' => 'standard',
'filter' => ['asciifolding'],
],
],
],
],
], $beerMapping);
}
}
Empty file added tests/configs/empty_mapping.php
Empty file.
19 changes: 19 additions & 0 deletions tests/configs_analysis/analyzers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

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

return [
'analyzer' => [
'beer_name' => [
'tokenizer' => 'classic',
'filter' => ['elision'],
],
],
];
20 changes: 20 additions & 0 deletions tests/configs_analysis/foo_mapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

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

return [
'mappings' => [
'properties' => [
'name' => [
'type' => 'keyword',
],
],
],
];

0 comments on commit 559de61

Please sign in to comment.