Skip to content

Commit 53fa3b1

Browse files
committed
improve tests data for factory, add 2 check of wrong config
1 parent bb64f43 commit 53fa3b1

File tree

6 files changed

+151
-31
lines changed

6 files changed

+151
-31
lines changed

src/Capacity/All.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Kiboko\Plugin\Sylius\Capacity;
66

77
use Kiboko\Plugin\Sylius;
8+
use Kiboko\Plugin\Sylius\Validator\ApiType;
89
use PhpParser\Builder;
910
use PhpParser\Node;
1011
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
@@ -119,9 +120,6 @@ public function __construct(private readonly ExpressionLanguage $interpreter) {}
119120

120121
public function applies(array $config): bool
121122
{
122-
if (!isset($config['api_type'])) {
123-
return false;
124-
}
125123
switch ($config['api_type']) {
126124
case 'admin':
127125
$endpoints = self::$endpointsAdmin;
@@ -136,9 +134,7 @@ public function applies(array $config): bool
136134
$doubleEndpoints = self::$doubleEndpointsLegacy;
137135
break;
138136
default:
139-
$endpoints = [];
140-
$doubleEndpoints = [];
141-
break;
137+
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)));
142138
}
143139

144140
return isset($config['type'])

src/Capacity/Create.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Kiboko\Plugin\Sylius\Capacity;
66

77
use Kiboko\Plugin\Sylius;
8+
use Kiboko\Plugin\Sylius\Validator\ApiType;
89
use PhpParser\Builder;
910
use PhpParser\Node;
1011

@@ -78,14 +79,11 @@ final class Create implements CapacityInterface
7879

7980
public function applies(array $config): bool
8081
{
81-
if (!isset($config['api_type'])) {
82-
return false;
83-
}
8482
$endpoints = match ($config['api_type']) {
8583
'admin' => self::$endpointsAdmin,
8684
'shop' => self::$endpointsShop,
8785
'legacy' => self::$endpointsLegacy,
88-
default => throw new \UnhandledMatchError($config['api_type'])
86+
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)))
8987
};
9088

9189
return isset($config['type'])

src/Capacity/ListPerPage.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Kiboko\Plugin\Sylius\Capacity;
66

77
use Kiboko\Plugin\Sylius;
8+
use Kiboko\Plugin\Sylius\Validator\ApiType;
89
use PhpParser\Builder;
910
use PhpParser\Node;
1011
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
@@ -118,9 +119,6 @@ public function __construct(private readonly ExpressionLanguage $interpreter) {}
118119

119120
public function applies(array $config): bool
120121
{
121-
if (!isset($config['api_type'])) {
122-
return false;
123-
}
124122
switch ($config['api_type']) {
125123
case 'admin':
126124
$endpoints = self::$endpointsAdmin;
@@ -135,9 +133,7 @@ public function applies(array $config): bool
135133
$doubleEndpoints = self::$doubleEndpointsLegacy;
136134
break;
137135
default:
138-
$endpoints = [];
139-
$doubleEndpoints = [];
140-
break;
136+
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)));
141137
}
142138

143139
return isset($config['type'])

src/Capacity/Upsert.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Kiboko\Plugin\Sylius\Capacity;
66

77
use Kiboko\Plugin\Sylius;
8+
use Kiboko\Plugin\Sylius\Validator\ApiType;
89
use PhpParser\Builder;
910
use PhpParser\Node;
1011

@@ -70,14 +71,11 @@ final class Upsert implements CapacityInterface
7071

7172
public function applies(array $config): bool
7273
{
73-
if (!isset($config['api_type'])) {
74-
return false;
75-
}
7674
$endpoints = match ($config['api_type']) {
7775
'admin' => self::$endpointsAdmin,
7876
'shop' => self::$endpointsShop,
7977
'legacy' => self::$endpointsLegacy,
80-
default => throw new \UnhandledMatchError($config['api_type'])
78+
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)))
8179
};
8280

8381
return isset($config['type'])

tests/functional/Factory/ExtractorTest.php

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Kiboko\Contract\Configurator\InvalidConfigurationException;
88
use Kiboko\Plugin\Sylius\Factory\Extractor;
9+
use Kiboko\Plugin\Sylius\Factory\Loader;
910
use PHPUnit\Framework\TestCase;
1011
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1112

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

33-
public static function wrongConfigs(): \Generator
34+
public static function wrongApiType(): \Generator
35+
{
36+
yield [
37+
'config' => [
38+
'type' => 'products',
39+
'method' => 'get',
40+
'api_type' => 'wrong',
41+
],
42+
];
43+
}
44+
45+
public static function missingApiType(): \Generator
3446
{
3547
yield [
3648
'config' => [
@@ -43,15 +55,26 @@ public static function wrongConfigs(): \Generator
4355
];
4456
yield [
4557
'config' => [
46-
'type' => 'wrong',
47-
'method' => 'all',
48-
'api_type' => 'legacy',
58+
'type' => 'products',
59+
],
60+
];
61+
yield [
62+
'config' => [
63+
'method' => 'get',
4964
],
5065
];
5166
yield [
5267
'config' => [
5368
'type' => 'products',
54-
'method' => 'wrong',
69+
'method' => 'get',
70+
],
71+
];
72+
}
73+
74+
public static function missingCapacityConfigs(): \Generator
75+
{
76+
yield [
77+
'config' => [
5578
'api_type' => 'legacy',
5679
],
5780
];
@@ -61,9 +84,24 @@ public static function wrongConfigs(): \Generator
6184
'api_type' => 'legacy',
6285
],
6386
];
87+
yield [
88+
'config' => [
89+
'api_type' => 'legacy',
90+
'method' => 'get',
91+
],
92+
];
93+
yield [
94+
'config' => [
95+
'type' => 'wrong',
96+
'method' => 'all',
97+
'api_type' => 'legacy',
98+
],
99+
];
64100
yield [
65101
'config' => [
66102
'type' => 'products',
103+
'method' => 'wrong',
104+
'api_type' => 'legacy',
67105
],
68106
];
69107
}
@@ -76,7 +114,34 @@ public function testValidateConfiguration(array $config): void
76114
$client->compile($config);
77115
}
78116

79-
#[\PHPUnit\Framework\Attributes\DataProvider('wrongConfigs')]
117+
118+
#[\PHPUnit\Framework\Attributes\DataProvider('wrongApiType')]
119+
public function testWrongApiType(array $config)
120+
{
121+
$this->expectException(\InvalidArgumentException::class);
122+
$this->expectExceptionCode(0);
123+
$this->expectExceptionMessage('The value of api_type should be one of [admin, shop, legacy], got "wrong".');
124+
125+
$client = new Loader();
126+
$this->assertFalse($client->validate($config));
127+
$client->compile($config);
128+
}
129+
130+
131+
#[\PHPUnit\Framework\Attributes\DataProvider('missingApiType')]
132+
public function testMissingApiType(array $config)
133+
{
134+
$this->expectException(\InvalidArgumentException::class);
135+
$this->expectExceptionCode(0);
136+
$this->expectExceptionMessage('The value of api_type should be one of [admin, shop, legacy], got null.');
137+
138+
$client = new Loader();
139+
$this->assertFalse($client->validate($config));
140+
$client->compile($config);
141+
}
142+
143+
144+
#[\PHPUnit\Framework\Attributes\DataProvider('missingCapacityConfigs')]
80145
public function testMissingCapacity(array $config): void
81146
{
82147
$this->expectException(InvalidConfigurationException::class);

tests/functional/Factory/LoaderTest.php

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,18 @@ public static function validDataProvider(): \Generator
2929
];
3030
}
3131

32-
public static function wrongConfigs(): \Generator
32+
public static function wrongApiType(): \Generator
33+
{
34+
yield [
35+
'config' => [
36+
'type' => 'products',
37+
'method' => 'upsert',
38+
'api_type' => 'wrong',
39+
],
40+
];
41+
}
42+
43+
public static function missingApiType(): \Generator
3344
{
3445
yield [
3546
'config' => [
@@ -42,21 +53,52 @@ public static function wrongConfigs(): \Generator
4253
];
4354
yield [
4455
'config' => [
45-
'type' => 'wrong',
46-
'method' => 'all',
56+
'type' => 'products',
57+
],
58+
];
59+
yield [
60+
'config' => [
61+
'method' => 'upsert',
62+
],
63+
];
64+
yield [
65+
'config' => [
66+
'type' => 'products',
67+
'method' => 'upsert',
68+
],
69+
];
70+
}
71+
72+
public static function missingCapacityConfigs(): \Generator
73+
{
74+
yield [
75+
'config' => [
4776
'api_type' => 'legacy',
4877
],
4978
];
5079
yield [
5180
'config' => [
81+
'api_type' => 'legacy',
5282
'type' => 'products',
53-
'method' => 'wrong',
83+
],
84+
];
85+
yield [
86+
'config' => [
87+
'method' => 'upsert',
88+
'api_type' => 'legacy',
89+
],
90+
];
91+
yield [
92+
'config' => [
93+
'type' => 'wrong',
94+
'method' => 'upsert',
5495
'api_type' => 'legacy',
5596
],
5697
];
5798
yield [
5899
'config' => [
59100
'type' => 'products',
101+
'method' => 'wrong',
60102
'api_type' => 'legacy',
61103
],
62104
];
@@ -70,7 +112,32 @@ public function testValidateConfiguration(array $config): void
70112
$client->compile($config);
71113
}
72114

73-
#[\PHPUnit\Framework\Attributes\DataProvider('wrongConfigs')]
115+
#[\PHPUnit\Framework\Attributes\DataProvider('wrongApiType')]
116+
public function testWrongApiType(array $config)
117+
{
118+
$this->expectException(\InvalidArgumentException::class);
119+
$this->expectExceptionCode(0);
120+
$this->expectExceptionMessage('The value of api_type should be one of [admin, shop, legacy], got "wrong".');
121+
122+
$client = new Loader();
123+
$this->assertFalse($client->validate($config));
124+
$client->compile($config);
125+
}
126+
127+
128+
#[\PHPUnit\Framework\Attributes\DataProvider('missingApiType')]
129+
public function testMissingApiType(array $config)
130+
{
131+
$this->expectException(\InvalidArgumentException::class);
132+
$this->expectExceptionCode(0);
133+
$this->expectExceptionMessage('The value of api_type should be one of [admin, shop, legacy], got null.');
134+
135+
$client = new Loader();
136+
$this->assertFalse($client->validate($config));
137+
$client->compile($config);
138+
}
139+
140+
#[\PHPUnit\Framework\Attributes\DataProvider('missingCapacityConfigs')]
74141
public function testMissingCapacity(array $config): void
75142
{
76143
$this->expectException(InvalidConfigurationException::class);

0 commit comments

Comments
 (0)