Skip to content

Commit

Permalink
refacto to use the InvalidConfigurationException on the right node an…
Browse files Browse the repository at this point in the history
…d have a coherent error's message, validate Configuration tests
  • Loading branch information
JoMessina committed Nov 20, 2023
1 parent 3f412ac commit 771836b
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 46 deletions.
25 changes: 21 additions & 4 deletions src/Configuration/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Kiboko\Plugin\Sylius\Configuration;

use Kiboko\Plugin\Sylius\Validator\ApiType;
use Kiboko\Plugin\Sylius\Validator\ExtractorConfigurationValidator;
use Kiboko\Plugin\Sylius\Validator\LoaderConfigurationValidator;
use Symfony\Component\Config;

use function Kiboko\Component\SatelliteToolbox\Configuration\asExpression;
Expand All @@ -13,7 +15,6 @@
final class Extractor implements Config\Definition\ConfigurationInterface
{


public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Builder\TreeBuilder
{
$filters = new Search();
Expand All @@ -23,19 +24,35 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui
/* @phpstan-ignore-next-line */
$builder->getRootNode()
->validate()
->ifArray()
->then(fn(array $item) => ExtractorConfigurationValidator::validate($item))
->always(fn(array $item) => ExtractorConfigurationValidator::validate($item)) //store the item value
->end()
->children()
->scalarNode('api_type')
->isRequired()
->cannotBeEmpty()
->validate()
->always(fn(string $item) => ExtractorConfigurationValidator::validateApiType($item)) //check index of the item value
->end()
->end()
->scalarNode('type')
->isRequired()
->cannotBeEmpty()
->validate()
->always(fn(string $item) => ExtractorConfigurationValidator::validateType($item)) //check index of the item value
->end()
->end()
->scalarNode('method')
->isRequired()
->cannotBeEmpty()
->validate()
->always(fn(string $item) => ExtractorConfigurationValidator::validateMethod($item)) //check index of the item value
->end()
->end()
->scalarNode('method')->end()
->scalarNode('code')
->cannotBeEmpty()
->validate()
->always(fn(string $item) => ExtractorConfigurationValidator::validateCode($item)) //check index of the item value
->end()
->validate()
->ifTrue(isExpression())
->then(asExpression())
Expand Down
14 changes: 10 additions & 4 deletions src/Configuration/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Kiboko\Plugin\Sylius\Configuration;

use Kiboko\Plugin\Sylius\Validator\ApiType;
use Kiboko\Plugin\Sylius\Validator\LoaderConfigurationValidator;
use Symfony\Component\Config;

Expand All @@ -16,22 +17,27 @@ public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Bui

/* @phpstan-ignore-next-line */
$builder->getRootNode()
->validate()
->ifArray()
->then(fn(array $item) => LoaderConfigurationValidator::validate($item))
->end()
->children()
->scalarNode('api_type')
->isRequired()
->cannotBeEmpty()
->validate()
->always(fn(string $item) => LoaderConfigurationValidator::validateApiType($item))
->end()
->end()
->scalarNode('type')
->isRequired()
->cannotBeEmpty()
->validate()
->always(fn(string $item) => LoaderConfigurationValidator::validateType($item))
->end()
->end()
->scalarNode('method')
->isRequired()
->cannotBeEmpty()
->validate()
->always(fn(string $item) => LoaderConfigurationValidator::validateMethod($item))
->end()
->end()
->end()
;
Expand Down
10 changes: 10 additions & 0 deletions src/Validator/ApiType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@ enum ApiType: string
case ADMIN = 'admin';
case SHOP = 'shop';
case LEGACY = 'legacy';

public static function casesValue(): array
{
$apiTypeCases = [];
foreach (ApiType::cases() as $cases)
{
$apiTypeCases[] = $cases->value;
}
return $apiTypeCases;
}
}
94 changes: 67 additions & 27 deletions src/Validator/ExtractorConfigurationValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Kiboko\Plugin\Sylius\Validator;

use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use function PHPUnit\Framework\isEmpty;

class ExtractorConfigurationValidator implements ConfigurationValidatorInterface
{
private static array $endpointsLegacy = [
Expand Down Expand Up @@ -456,40 +459,77 @@ class ExtractorConfigurationValidator implements ConfigurationValidatorInterface
'adjustment',
'order',
];

private static array $item = [];

public static function getEndpointsApiType(): array
{
return match (self::$currentApiType) {
ApiType::ADMIN->value => self::$endpointsAdmin,
ApiType::SHOP->value => self::$endpointsShop,
ApiType::LEGACY->value => self::$endpointsLegacy
};
}

public static function getDoubleEndpointsApiType(): array
{
return match (self::$currentApiType) {
ApiType::ADMIN->value => self::$doubleEndpointsAdmin,
ApiType::SHOP->value => self::$doubleEndpointsShop,
ApiType::LEGACY->value => self::$doubleEndpointsLegacy
};
}
public static string $currentApiType;
public static string $currentType;

public static function validate(array $item): array
{
switch($item['api_type']) {
case ApiType::ADMIN->value:
$endpoints = self::$endpointsAdmin;
$doubleEndpoints = self::$doubleEndpointsAdmin;
break;
case ApiType::SHOP->value:
$endpoints = self::$endpointsShop;
$doubleEndpoints = self::$doubleEndpointsShop;
break;
case ApiType::LEGACY->value:
$endpoints = self::$endpointsLegacy;
$doubleEndpoints = self::$doubleEndpointsLegacy;
break;
default:
$endpoints = [];
$doubleEndpoints = [];
break;
if(\in_array($item['type'], self::getDoubleEndpointsApiType()) && !\array_key_exists('code', $item)) {
throw new InvalidConfigurationException('The code parameters is required and cannot be empty because you choose a type: ' . $item['type']);
}
if (!\in_array($item['type'], array_merge(array_keys($endpoints), $doubleEndpoints))) {
throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', array_merge(array_keys($endpoints), $doubleEndpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR)));
return $item;
}

public static function validateApiType(string $apiType)
{
self::$currentApiType = $apiType;
if(!\in_array($apiType, ApiType::casesValue())){
throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($apiType, \JSON_THROW_ON_ERROR)));
}
return $apiType;
}

public static function validateType(string $type)
{
self::$currentType = $type;
$endpoints = self::getEndpointsApiType();
$doubleEndpoints = self::getDoubleEndpointsApiType();
if (!\in_array($type, array_merge(array_keys($endpoints), $doubleEndpoints))) {
throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s.', implode(', ', array_merge(array_keys($endpoints), $doubleEndpoints)), json_encode($type, \JSON_THROW_ON_ERROR)));
}
return $type;
}

public static function validateMethod(string $method)
{
$endpoints = self::getEndpointsApiType();
$doubleEndpoints = self::getDoubleEndpointsApiType();
if (
\array_key_exists($item['type'], $endpoints)
&& !\in_array($item['method'], $endpoints[$item['type']])
&& !\in_array($item['type'], $doubleEndpoints)
\array_key_exists(self::$currentType, $endpoints)
&& !\in_array($method, $endpoints[self::$currentType])
&& !\in_array(self::$currentType, $doubleEndpoints)
) {
throw new \InvalidArgumentException(sprintf('The value should be one of [%s], got %s.', implode(', ', $endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR)));
}
if (\in_array($item['type'], $doubleEndpoints) && !\array_key_exists('code', $item)) {
throw new \InvalidArgumentException(sprintf('The %s type should have a "code" field set.', $item['type']));
throw new \InvalidArgumentException(sprintf('The value should be one of [%s], got %s.', implode(', ', $endpoints[self::$currentType]), json_encode($method, \JSON_THROW_ON_ERROR)));
}
return $method;
}

return $item;
public static function validateCode(string $code)
{
$doubleEndpoints = self::getDoubleEndpointsApiType();
if (\in_array(self::$currentType, $doubleEndpoints)) {
throw new \InvalidArgumentException(sprintf('The %s type should have a "code" field set.', self::$currentType), json_encode($code, \JSON_THROW_ON_ERROR));
}
return $code;
}
}
50 changes: 46 additions & 4 deletions src/Validator/LoaderConfigurationValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Kiboko\Plugin\Sylius\Validator;

use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

class LoaderConfigurationValidator implements ConfigurationValidatorInterface
{
private static array $endpointsLegacy = [
Expand Down Expand Up @@ -249,19 +251,59 @@ class LoaderConfigurationValidator implements ConfigurationValidatorInterface
'verify',
],
];
public static function validate(array $item): array


public static string $currentApiType;
public static string $currentType;

public static function getEndpointsApiType(): array
{
$endpoints = match ($item['api_type']) {
return match (self::$currentApiType) {
ApiType::ADMIN->value => self::$endpointsAdmin,
ApiType::SHOP->value => self::$endpointsShop,
ApiType::LEGACY->value => self::$endpointsLegacy
};
}
public static function validate(array $item): array
{
$endpoints = self::getEndpointsApiType();
if (!\in_array($item['type'], array_keys($endpoints))) {
throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', array_keys($endpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR)));
throw new \InvalidArgumentException(sprintf('the value "type" should be one of [%s], got %s', implode(', ', array_keys($endpoints)), json_encode($item['type'], \JSON_THROW_ON_ERROR)));
}
if (!\in_array($item['method'], $endpoints[$item['type']])) {
throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', $endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR)));
throw new \InvalidArgumentException(sprintf('the value "method" should be one of [%s], got %s', implode(', ', $endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR)));
}
return $item;
}

public static function validateApiType(string $apiType)
{
self::$currentApiType = $apiType;
if(!\in_array($apiType, ApiType::casesValue())){
throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($apiType, \JSON_THROW_ON_ERROR)));
}
return $apiType;
}

public static function validateType(string $type)
{
self::$currentType = $type;
$endpoints = self::getEndpointsApiType();
if (!\in_array($type, array_keys($endpoints))) {
throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s.', implode(', ', array_keys($endpoints)), json_encode($type, \JSON_THROW_ON_ERROR)));
}
return $type;
}

public static function validateMethod(string $method)
{
$endpoints = self::getEndpointsApiType();
if (
\array_key_exists(self::$currentType, $endpoints)
&& !\in_array($method, $endpoints[self::$currentType])
) {
throw new \InvalidArgumentException(sprintf('The value should be one of [%s], got %s.', implode(', ', $endpoints[self::$currentType]), json_encode($method, \JSON_THROW_ON_ERROR)));
}
return $method;
}
}
2 changes: 2 additions & 0 deletions tests/functional/Builder/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function testExpectingTokenOrPassword(): void
$this->expectException(MissingAuthenticationMethodException::class);
$this->expectExceptionMessage('Please check your client builder, you should either call withToken() or withPassword() methods.');

$client->withApiType('legacy');

$client->getNode();
}
}
2 changes: 2 additions & 0 deletions tests/functional/Builder/Extractor/ExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function testAllProducts(): void

$builder = new Extractor($capacity);
$builder->withClient($client->getNode());
$builder->withApiType('legacy');

$this->assertBuildsExtractorExtractsExactly(
[
Expand Down Expand Up @@ -189,6 +190,7 @@ public function testAllProductsWithSearch(): void

$builder = new Extractor($capacity);
$builder->withClient($client->getNode());
$builder->withApiType('legacy');

$this->assertBuildsExtractorExtractsExactly(
[
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/Builder/Loader/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function testUpsertProduct(): void

$builder = new Loader($capacity);
$builder->withClient($client->getNode());
$builder->withApiType('legacy');

$this->assertBuildsLoaderLoadsExactly(
[
Expand Down Expand Up @@ -95,6 +96,7 @@ public function testCreateProduct(): void

$builder = new Loader($capacity);
$builder->withClient($client->getNode());
$builder->withApiType('legacy');

$this->assertBuildsLoaderLoadsExactly(
[
Expand Down
Loading

0 comments on commit 771836b

Please sign in to comment.