Skip to content

Commit

Permalink
improve tests data for factory, add 2 check of wrong config
Browse files Browse the repository at this point in the history
  • Loading branch information
JoMessina committed Nov 20, 2023
1 parent bb64f43 commit 53fa3b1
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 31 deletions.
8 changes: 2 additions & 6 deletions src/Capacity/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Kiboko\Plugin\Sylius\Capacity;

use Kiboko\Plugin\Sylius;
use Kiboko\Plugin\Sylius\Validator\ApiType;
use PhpParser\Builder;
use PhpParser\Node;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
Expand Down Expand Up @@ -119,9 +120,6 @@ public function __construct(private readonly ExpressionLanguage $interpreter) {}

public function applies(array $config): bool
{
if (!isset($config['api_type'])) {
return false;
}
switch ($config['api_type']) {
case 'admin':
$endpoints = self::$endpointsAdmin;
Expand All @@ -136,9 +134,7 @@ public function applies(array $config): bool
$doubleEndpoints = self::$doubleEndpointsLegacy;
break;
default:
$endpoints = [];
$doubleEndpoints = [];
break;
throw new \InvalidArgumentException(sprintf('The value of api_type should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($config['api_type'], \JSON_THROW_ON_ERROR)));
}

return isset($config['type'])
Expand Down
6 changes: 2 additions & 4 deletions src/Capacity/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Kiboko\Plugin\Sylius\Capacity;

use Kiboko\Plugin\Sylius;
use Kiboko\Plugin\Sylius\Validator\ApiType;
use PhpParser\Builder;
use PhpParser\Node;

Expand Down Expand Up @@ -78,14 +79,11 @@ final class Create implements CapacityInterface

public function applies(array $config): bool
{
if (!isset($config['api_type'])) {
return false;
}
$endpoints = match ($config['api_type']) {
'admin' => self::$endpointsAdmin,
'shop' => self::$endpointsShop,
'legacy' => self::$endpointsLegacy,
default => throw new \UnhandledMatchError($config['api_type'])
default => throw new \InvalidArgumentException(sprintf('The value of api_type should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($config['api_type'], \JSON_THROW_ON_ERROR)))
};

return isset($config['type'])
Expand Down
8 changes: 2 additions & 6 deletions src/Capacity/ListPerPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Kiboko\Plugin\Sylius\Capacity;

use Kiboko\Plugin\Sylius;
use Kiboko\Plugin\Sylius\Validator\ApiType;
use PhpParser\Builder;
use PhpParser\Node;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
Expand Down Expand Up @@ -118,9 +119,6 @@ public function __construct(private readonly ExpressionLanguage $interpreter) {}

public function applies(array $config): bool
{
if (!isset($config['api_type'])) {
return false;
}
switch ($config['api_type']) {
case 'admin':
$endpoints = self::$endpointsAdmin;
Expand All @@ -135,9 +133,7 @@ public function applies(array $config): bool
$doubleEndpoints = self::$doubleEndpointsLegacy;
break;
default:
$endpoints = [];
$doubleEndpoints = [];
break;
throw new \InvalidArgumentException(sprintf('The value of api_type should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($config['api_type'], \JSON_THROW_ON_ERROR)));
}

return isset($config['type'])
Expand Down
6 changes: 2 additions & 4 deletions src/Capacity/Upsert.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Kiboko\Plugin\Sylius\Capacity;

use Kiboko\Plugin\Sylius;
use Kiboko\Plugin\Sylius\Validator\ApiType;
use PhpParser\Builder;
use PhpParser\Node;

Expand Down Expand Up @@ -70,14 +71,11 @@ final class Upsert implements CapacityInterface

public function applies(array $config): bool
{
if (!isset($config['api_type'])) {
return false;
}
$endpoints = match ($config['api_type']) {
'admin' => self::$endpointsAdmin,
'shop' => self::$endpointsShop,
'legacy' => self::$endpointsLegacy,
default => throw new \UnhandledMatchError($config['api_type'])
default => throw new \InvalidArgumentException(sprintf('The value of api_type should be one of [%s], got %s.', implode(', ', ApiType::casesValue()), json_encode($config['api_type'], \JSON_THROW_ON_ERROR)))
};

return isset($config['type'])
Expand Down
77 changes: 71 additions & 6 deletions tests/functional/Factory/ExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Kiboko\Contract\Configurator\InvalidConfigurationException;
use Kiboko\Plugin\Sylius\Factory\Extractor;
use Kiboko\Plugin\Sylius\Factory\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

Expand All @@ -30,7 +31,18 @@ public static function validDataProvider(): \Generator
];
}

public static function wrongConfigs(): \Generator
public static function wrongApiType(): \Generator
{
yield [
'config' => [
'type' => 'products',
'method' => 'get',
'api_type' => 'wrong',
],
];
}

public static function missingApiType(): \Generator
{
yield [
'config' => [
Expand All @@ -43,15 +55,26 @@ public static function wrongConfigs(): \Generator
];
yield [
'config' => [
'type' => 'wrong',
'method' => 'all',
'api_type' => 'legacy',
'type' => 'products',
],
];
yield [
'config' => [
'method' => 'get',
],
];
yield [
'config' => [
'type' => 'products',
'method' => 'wrong',
'method' => 'get',
],
];
}

public static function missingCapacityConfigs(): \Generator
{
yield [
'config' => [
'api_type' => 'legacy',
],
];
Expand All @@ -61,9 +84,24 @@ public static function wrongConfigs(): \Generator
'api_type' => 'legacy',
],
];
yield [
'config' => [
'api_type' => 'legacy',
'method' => 'get',
],
];
yield [
'config' => [
'type' => 'wrong',
'method' => 'all',
'api_type' => 'legacy',
],
];
yield [
'config' => [
'type' => 'products',
'method' => 'wrong',
'api_type' => 'legacy',
],
];
}
Expand All @@ -76,7 +114,34 @@ public function testValidateConfiguration(array $config): void
$client->compile($config);
}

#[\PHPUnit\Framework\Attributes\DataProvider('wrongConfigs')]

#[\PHPUnit\Framework\Attributes\DataProvider('wrongApiType')]
public function testWrongApiType(array $config)
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('The value of api_type should be one of [admin, shop, legacy], got "wrong".');

$client = new Loader();
$this->assertFalse($client->validate($config));
$client->compile($config);
}


#[\PHPUnit\Framework\Attributes\DataProvider('missingApiType')]
public function testMissingApiType(array $config)
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('The value of api_type should be one of [admin, shop, legacy], got null.');

$client = new Loader();
$this->assertFalse($client->validate($config));
$client->compile($config);
}


#[\PHPUnit\Framework\Attributes\DataProvider('missingCapacityConfigs')]
public function testMissingCapacity(array $config): void
{
$this->expectException(InvalidConfigurationException::class);
Expand Down
77 changes: 72 additions & 5 deletions tests/functional/Factory/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,18 @@ public static function validDataProvider(): \Generator
];
}

public static function wrongConfigs(): \Generator
public static function wrongApiType(): \Generator
{
yield [
'config' => [
'type' => 'products',
'method' => 'upsert',
'api_type' => 'wrong',
],
];
}

public static function missingApiType(): \Generator
{
yield [
'config' => [
Expand All @@ -42,21 +53,52 @@ public static function wrongConfigs(): \Generator
];
yield [
'config' => [
'type' => 'wrong',
'method' => 'all',
'type' => 'products',
],
];
yield [
'config' => [
'method' => 'upsert',
],
];
yield [
'config' => [
'type' => 'products',
'method' => 'upsert',
],
];
}

public static function missingCapacityConfigs(): \Generator
{
yield [
'config' => [
'api_type' => 'legacy',
],
];
yield [
'config' => [
'api_type' => 'legacy',
'type' => 'products',
'method' => 'wrong',
],
];
yield [
'config' => [
'method' => 'upsert',
'api_type' => 'legacy',
],
];
yield [
'config' => [
'type' => 'wrong',
'method' => 'upsert',
'api_type' => 'legacy',
],
];
yield [
'config' => [
'type' => 'products',
'method' => 'wrong',
'api_type' => 'legacy',
],
];
Expand All @@ -70,7 +112,32 @@ public function testValidateConfiguration(array $config): void
$client->compile($config);
}

#[\PHPUnit\Framework\Attributes\DataProvider('wrongConfigs')]
#[\PHPUnit\Framework\Attributes\DataProvider('wrongApiType')]
public function testWrongApiType(array $config)
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('The value of api_type should be one of [admin, shop, legacy], got "wrong".');

$client = new Loader();
$this->assertFalse($client->validate($config));
$client->compile($config);
}


#[\PHPUnit\Framework\Attributes\DataProvider('missingApiType')]
public function testMissingApiType(array $config)
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('The value of api_type should be one of [admin, shop, legacy], got null.');

$client = new Loader();
$this->assertFalse($client->validate($config));
$client->compile($config);
}

#[\PHPUnit\Framework\Attributes\DataProvider('missingCapacityConfigs')]
public function testMissingCapacity(array $config): void
{
$this->expectException(InvalidConfigurationException::class);
Expand Down

0 comments on commit 53fa3b1

Please sign in to comment.