-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add basic tests for YamlProvider * PHP Mapping Provider * Fix CS
- Loading branch information
Showing
8 changed files
with
208 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
], | ||
], | ||
], | ||
]; |