Skip to content

Commit 699f060

Browse files
committed
Increase Coverage
- Changed Property so it doesn't have an unreachable line anymore. - Rename ValidatorTest to ValidatorBuilderTest to better reflect the source. - Added missing tests for KeyExists's ArrayAccess support. - Added tests for using custom exceptions.
1 parent 9def16a commit 699f060

File tree

6 files changed

+50
-17
lines changed

6 files changed

+50
-17
lines changed

src/Validators/Property.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,18 @@ public function evaluate(mixed $input): Result
4747
private function getPropertyValue(object $object, string $propertyName): mixed
4848
{
4949
$reflection = new ReflectionObject($object);
50+
$value = null;
5051
while ($reflection instanceof ReflectionClass) {
5152
if ($reflection->hasProperty($propertyName)) {
5253
$property = $reflection->getProperty($propertyName);
5354

54-
return $property->isInitialized($object) ? $property->getValue($object) : null;
55+
$value = $property->isInitialized($object) ? $property->getValue($object) : null;
56+
break;
5557
}
5658

5759
$reflection = $reflection->getParentClass();
5860
}
5961

60-
return null;
62+
return $value;
6163
}
6264
}

tests/src/TestCase.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace Respect\Validation\Test;
1414

15+
use ArrayAccess;
16+
use ArrayObject;
1517
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
1618
use Respect\Validation\Test\Stubs\WithProperties;
1719
use Respect\Validation\Test\Stubs\WithStaticProperties;
@@ -194,21 +196,23 @@ public static function providerForNonResourceType(): DataProvider
194196
return self::providerForAnyValues()->without('resourceType');
195197
}
196198

197-
/** @return array<string, array{string|int, array<mixed>}> */
199+
/** @return array<string, array{string|int, array<mixed>|ArrayAccess<int, mixed>}> */
198200
public static function providerForArrayWithMissingKeys(): array
199201
{
200202
return [
203+
'missing key on an ArrayAccess object' => [1, new ArrayObject([])],
201204
'integer key, non-empty input' => [0, [1 => true, 2 => true]],
202205
'string key, non-empty input' => ['foo', ['bar' => true, 'baz' => true]],
203206
'integer key, empty input' => [0, []],
204207
'string key, empty input' => ['foo', []],
205208
];
206209
}
207210

208-
/** @return array<string, array{string|int, array<mixed>}> */
211+
/** @return array<string, array{string|int, array<mixed>|ArrayAccess<int, mixed>}> */
209212
public static function providerForArrayWithExistingKeys(): array
210213
{
211214
return [
215+
'key on an ArrayAccess object' => [1, new ArrayObject([1 => true])],
212216
'integer key with a single value array' => [1, [1 => true]],
213217
'integer key with a multiple value array' => [2, [1 => true, 2 => true]],
214218
'string key with a single value array' => ['foo', ['foo' => true, 'bar' => true]],
Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Respect\Validation;
1717

18+
use InvalidArgumentException;
1819
use PHPUnit\Framework\Attributes\CoversClass;
1920
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
2021
use PHPUnit\Framework\Attributes\Test;
@@ -23,10 +24,11 @@
2324
use Respect\Validation\Test\TestCase;
2425
use Respect\Validation\Test\Validators\Stub;
2526

27+
use function sprintf;
2628
use function uniqid;
2729

2830
#[CoversClass(ValidatorBuilder::class)]
29-
final class ValidatorTest extends TestCase
31+
final class ValidatorBuilderTest extends TestCase
3032
{
3133
#[Test]
3234
public function invalidRuleClassShouldThrowComponentException(): void
@@ -165,4 +167,26 @@ public function itShouldEvaluateAndReturnResultWhenSingleFailingRuleIsAdded(): v
165167

166168
self::assertFalse($result->hasPassed);
167169
}
170+
171+
#[Test]
172+
public function itShouldThrowCustomExceptionWhenPassedToAssertMethod(): void
173+
{
174+
$this->expectException(InvalidArgumentException::class);
175+
$this->expectExceptionMessage('Custom exception message');
176+
177+
ValidatorBuilder::init(Stub::fail(1))
178+
->assert('whatever', new InvalidArgumentException('Custom exception message'));
179+
}
180+
181+
#[Test]
182+
public function itShouldThrowCustomExceptionWhenCallableUsedInAssertMethod(): void
183+
{
184+
$this->expectException(InvalidArgumentException::class);
185+
$this->expectExceptionMessage('Got: "whatever"');
186+
187+
ValidatorBuilder::init(Stub::fail(1))
188+
->assert('whatever', static fn($e) => new InvalidArgumentException(
189+
sprintf('Got: %s', $e->getMessage()),
190+
));
191+
}
168192
}

tests/unit/Validators/KeyExistsTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Respect\Validation\Validators;
1212

13+
use ArrayAccess;
1314
use PHPUnit\Framework\Attributes\CoversClass;
1415
use PHPUnit\Framework\Attributes\DataProvider;
1516
use PHPUnit\Framework\Attributes\Test;
@@ -27,20 +28,20 @@ public function itShouldAlwaysInvalidateNonArrayValues(mixed $input): void
2728
self::assertInvalidInput($validator, $input);
2829
}
2930

30-
/** @param array<mixed> $input */
31+
/** @param array<mixed>|ArrayAccess<int|string, mixed> $input */
3132
#[Test]
3233
#[DataProvider('providerForArrayWithMissingKeys')]
33-
public function itShouldInvalidateMissingKeys(int|string $key, array $input): void
34+
public function itShouldInvalidateMissingKeys(int|string $key, array|ArrayAccess $input): void
3435
{
3536
$validator = new KeyExists($key);
3637

3738
self::assertInvalidInput($validator, $input);
3839
}
3940

40-
/** @param array<mixed> $input */
41+
/** @param array<mixed>|ArrayAccess<int|string, mixed> $input */
4142
#[Test]
4243
#[DataProvider('providerForArrayWithExistingKeys')]
43-
public function itShouldValidateExistingKeys(int|string $key, array $input): void
44+
public function itShouldValidateExistingKeys(int|string $key, array|ArrayAccess $input): void
4445
{
4546
$validator = new KeyExists($key);
4647

tests/unit/Validators/KeyOptionalTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Respect\Validation\Validators;
1212

13+
use ArrayAccess;
1314
use PHPUnit\Framework\Attributes\CoversClass;
1415
use PHPUnit\Framework\Attributes\DataProvider;
1516
use PHPUnit\Framework\Attributes\Group;
@@ -30,20 +31,20 @@ public function itShouldAlwaysValidateNonArrayValues(mixed $input): void
3031
self::assertValidInput($validator, $input);
3132
}
3233

33-
/** @param array<mixed> $input */
34+
/** @param array<mixed>|ArrayAccess<int|string, mixed> $input */
3435
#[Test]
3536
#[DataProvider('providerForArrayWithMissingKeys')]
36-
public function itShouldAlwaysValidateMissingKeys(int|string $key, array $input): void
37+
public function itShouldAlwaysValidateMissingKeys(int|string $key, array|ArrayAccess $input): void
3738
{
3839
$validator = new KeyOptional($key, Stub::daze());
3940

4041
self::assertValidInput($validator, $input);
4142
}
4243

43-
/** @param array<mixed> $input */
44+
/** @param array<mixed>|ArrayAccess<int|string, mixed> $input */
4445
#[Test]
4546
#[DataProvider('providerForArrayWithExistingKeys')]
46-
public function itShouldValidateExistingKeysWithWrappedRule(int|string $key, array $input): void
47+
public function itShouldValidateExistingKeysWithWrappedRule(int|string $key, array|ArrayAccess $input): void
4748
{
4849
$wrapped = Stub::pass(1);
4950

tests/unit/Validators/KeyTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Respect\Validation\Validators;
1515

16+
use ArrayAccess;
1617
use PHPUnit\Framework\Attributes\CoversClass;
1718
use PHPUnit\Framework\Attributes\DataProvider;
1819
use PHPUnit\Framework\Attributes\Group;
@@ -33,20 +34,20 @@ public function itShouldAlwaysInvalidateNonArrayValues(mixed $input): void
3334
self::assertInvalidInput($validator, $input);
3435
}
3536

36-
/** @param array<mixed> $input */
37+
/** @param array<mixed>|ArrayAccess<int|string, mixed> $input */
3738
#[Test]
3839
#[DataProvider('providerForArrayWithMissingKeys')]
39-
public function itShouldInvalidateMissingKeys(int|string $key, array $input): void
40+
public function itShouldInvalidateMissingKeys(int|string $key, array|ArrayAccess $input): void
4041
{
4142
$validator = new Key($key, Stub::daze());
4243

4344
self::assertInvalidInput($validator, $input);
4445
}
4546

46-
/** @param array<mixed> $input */
47+
/** @param array<mixed>|ArrayAccess<int|string, mixed> $input */
4748
#[Test]
4849
#[DataProvider('providerForArrayWithExistingKeys')]
49-
public function itShouldValidateExistingKeysWithWrappedRule(int|string $key, array $input): void
50+
public function itShouldValidateExistingKeysWithWrappedRule(int|string $key, array|ArrayAccess $input): void
5051
{
5152
$wrapped = Stub::pass(1);
5253

0 commit comments

Comments
 (0)