Skip to content

Commit d5a0453

Browse files
committed
fix a few errors
1 parent 9d0ef44 commit d5a0453

File tree

8 files changed

+54
-73
lines changed

8 files changed

+54
-73
lines changed

src/Validator/RegExp.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ public function __construct(
2020
/**
2121
* @throws \UnexpectedValueException
2222
*/
23-
public function __invoke(mixed $value): string {
23+
public function __invoke(mixed $value): ?string {
2424
if (!\is_string($value) || !\preg_match($this->pattern, $value)) {
25-
$message = $this->message ?? \sprintf('The value does not match pattern "%s".', $this->pattern);
26-
throw new \UnexpectedValueException($message);
25+
return $this->message ?? \sprintf('The value does not match pattern "%s".', $this->pattern);
2726
}
28-
return $value;
27+
return NULL;
2928
}
3029

3130
}

src/Validator/RequiredMachineName.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class RequiredMachineName {
1212
/**
1313
* @throws \UnexpectedValueException
1414
*/
15-
public function __invoke(mixed $value): string {
15+
public function __invoke(mixed $value): ?string {
1616
return (new Chained(new Required(), new MachineName()))($value);
1717
}
1818

src/Validator/RequiredServiceName.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class RequiredServiceName {
1212
/**
1313
* @throws \UnexpectedValueException
1414
*/
15-
public function __invoke(mixed $value): string {
15+
public function __invoke(mixed $value): ?string {
1616
return (new Chained(new Required(), new ServiceName()))($value);
1717
}
1818

tests/unit/Validator/ChainedTest.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,36 @@ final class ChainedTest extends TestCase {
1818
* Test callback.
1919
*/
2020
#[DataProvider('dataProvider')]
21-
public function test(mixed $machine_name, ?\UnexpectedValueException $exception): void {
22-
if ($exception) {
23-
$this->expectExceptionObject($exception);
24-
}
21+
public function test(mixed $machine_name, $expected): void {
2522
$validator = new Chained(
2623
new RegExp('/111/', 'v1'),
2724
new RegExp('/222/', 'v2'),
2825
new RegExp('/333/', 'v3'),
2926
);
30-
self::assertSame($machine_name, $validator($machine_name));
27+
self::assertSame($expected, $validator($machine_name));
3128
}
3229

3330
/**
3431
* Test callback.
3532
*/
3633
#[DataProvider('dataProvider')]
37-
public function testWith(mixed $machine_name, ?\UnexpectedValueException $exception): void {
38-
if ($exception) {
39-
$this->expectExceptionObject($exception);
40-
}
34+
public function testWith(mixed $machine_name, $expected): void {
4135
$validator = new Chained(new RegExp('/111/', 'v1'));
4236
$validator = $validator->with(
4337
new RegExp('/222/', 'v2'),
4438
new RegExp('/333/', 'v3'),
4539
);
46-
self::assertSame($machine_name, $validator($machine_name));
40+
self::assertSame($expected, $validator($machine_name));
4741
}
4842

4943
/**
5044
* Test data provider.
5145
*/
5246
public static function dataProvider(): array {
5347
return [
54-
['', new \UnexpectedValueException('v1')],
55-
['111', new \UnexpectedValueException('v2')],
56-
['111-222', new \UnexpectedValueException('v3')],
48+
['', 'v1'],
49+
['111', 'v2'],
50+
['111-222', 'v3'],
5751
['111-222-333', NULL],
5852
];
5953
}

tests/unit/Validator/ClassNameTest.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,25 @@ final class ClassNameTest extends TestCase {
1717
* Test callback.
1818
*/
1919
#[DataProvider('dataProvider')]
20-
public function test(mixed $machine_name, ?\UnexpectedValueException $exception): void {
21-
if ($exception) {
22-
$this->expectExceptionObject($exception);
23-
}
24-
self::assertSame($machine_name, (new ClassName())($machine_name));
20+
public function test(mixed $machine_name, $expected): void {
21+
self::assertSame($expected, (new ClassName())($machine_name));
2522
}
2623

2724
public static function dataProvider(): array {
28-
$exception = new \UnexpectedValueException('The value is not correct class name.');
25+
$error = 'The value is not correct class name.';
2926
return [
3027
['Single', NULL],
3128
['UpperCamelCase', NULL],
32-
['lowCamelCase', $exception],
33-
['snake_case_here', $exception],
34-
['With Space', $exception],
35-
[' NotTrimmed ', $exception],
36-
['With_Underscore', $exception],
37-
['WrongSymbols@)@#&)', $exception],
38-
[TRUE, $exception],
39-
[NULL, $exception],
40-
[[], $exception],
41-
[new \stdClass(), $exception],
29+
['lowCamelCase', $error],
30+
['snake_case_here', $error],
31+
['With Space', $error],
32+
[' NotTrimmed ', $error],
33+
['With_Underscore', $error],
34+
['WrongSymbols@)@#&)', $error],
35+
[TRUE, $error],
36+
[NULL, $error],
37+
[[], $error],
38+
[new \stdClass(), $error],
4239
];
4340
}
4441

tests/unit/Validator/RegExpTest.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,19 @@ final class RegExpTest extends TestCase {
1717
* Test callback.
1818
*/
1919
#[DataProvider('dataProvider')]
20-
public function test(mixed $machine_name, string $pattern, ?string $message, ?\UnexpectedValueException $exception): void {
20+
public function test(mixed $machine_name, string $pattern, ?string $message, $expected): void {
2121
$validator = new RegExp($pattern, $message);
22-
if ($exception) {
23-
$this->expectExceptionObject($exception);
24-
}
25-
self::assertSame($machine_name, $validator($machine_name));
22+
self::assertSame($expected, $validator($machine_name));
2623
}
2724

2825
public static function dataProvider(): array {
2926
return [
30-
['wrong', '/abc/', NULL, new \UnexpectedValueException('The value does not match pattern "/abc/".')],
31-
['wrong', '/abc/', 'Custom message', new \UnexpectedValueException('Custom message')],
27+
['wrong', '/abc/', NULL, 'The value does not match pattern "/abc/".'],
28+
['wrong', '/abc/', 'Custom message', 'Custom message'],
3229
['abc', '/abc/', NULL, NULL],
33-
[NULL, '/abc/', NULL, new \UnexpectedValueException('The value does not match pattern "/abc/".')],
34-
[FALSE, '/abc/', NULL, new \UnexpectedValueException('The value does not match pattern "/abc/".')],
35-
[[], '/abc/', NULL, new \UnexpectedValueException('The value does not match pattern "/abc/".')],
30+
[NULL, '/abc/', NULL, 'The value does not match pattern "/abc/".'],
31+
[FALSE, '/abc/', NULL, 'The value does not match pattern "/abc/".'],
32+
[[], '/abc/', NULL, 'The value does not match pattern "/abc/".'],
3633
];
3734
}
3835

tests/unit/Validator/RequiredClassNameTest.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,23 @@ final class RequiredClassNameTest extends TestCase {
1717
* Test callback.
1818
*/
1919
#[DataProvider('dataProvider')]
20-
public function test(mixed $machine_name, ?\UnexpectedValueException $exception): void {
21-
if ($exception) {
22-
$this->expectExceptionObject($exception);
23-
}
24-
self::assertSame($machine_name, (new RequiredClassName())($machine_name));
20+
public function test(mixed $machine_name, $expected): void {
21+
self::assertSame($expected, (new RequiredClassName())($machine_name));
2522
}
2623

2724
public static function dataProvider(): array {
28-
$cn_exception = new \UnexpectedValueException('The value is not correct class name.');
29-
$rq_exception = new \UnexpectedValueException('The value is required.');
25+
$cn_error = 'The value is not correct class name.';
26+
$rq_error = 'The value is required.';
3027
return [
3128
['Single', NULL],
3229
['UpperCamelCase', NULL],
33-
['snake_case_here', $cn_exception],
34-
['With Space', $cn_exception],
35-
['0', $cn_exception],
36-
['', $rq_exception],
37-
[NULL, $rq_exception],
38-
[FALSE, $cn_exception],
39-
[TRUE, $cn_exception],
30+
['snake_case_here', $cn_error],
31+
['With Space', $cn_error],
32+
['0', $cn_error],
33+
['', $rq_error],
34+
[NULL, $rq_error],
35+
[FALSE, $cn_error],
36+
[TRUE, $cn_error],
4037
];
4138
}
4239

tests/unit/Validator/RequiredServiceNameTest.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,23 @@ final class RequiredServiceNameTest extends TestCase {
1717
* Test callback.
1818
*/
1919
#[DataProvider('dataProvider')]
20-
public function test(mixed $machine_name, ?\UnexpectedValueException $exception): void {
21-
if ($exception) {
22-
$this->expectExceptionObject($exception);
23-
}
24-
self::assertSame($machine_name, (new RequiredServiceName())($machine_name));
20+
public function test(mixed $machine_name, $expected): void {
21+
self::assertSame($expected, (new RequiredServiceName())($machine_name));
2522
}
2623

2724
public static function dataProvider(): array {
28-
$sn_exception = new \UnexpectedValueException('The value is not correct service name.');
29-
$rq_exception = new \UnexpectedValueException('The value is required.');
25+
$sn_error = 'The value is not correct service name.';
26+
$rq_error = 'The value is required.';
3027
return [
3128
['snake_case_here', NULL],
3229
['dot.inside', NULL],
33-
['CamelCaseHere', $sn_exception],
34-
[' not_trimmed ', $sn_exception],
35-
['0', $sn_exception],
36-
['', $rq_exception],
37-
[NULL, $rq_exception],
38-
[FALSE, $sn_exception],
39-
[TRUE, $sn_exception],
30+
['CamelCaseHere', $sn_error],
31+
[' not_trimmed ', $sn_error],
32+
['0', $sn_error],
33+
['', $rq_error],
34+
[NULL, $rq_error],
35+
[FALSE, $sn_error],
36+
[TRUE, $sn_error],
4037
];
4138
}
4239

0 commit comments

Comments
 (0)