Skip to content

Commit

Permalink
Make clear when Stub should not be executed
Browse files Browse the repository at this point in the history
Having a named constructor with a name that clarifies that we don't
expect to execute the stub will make it easier to read the tests.

Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Feb 22, 2024
1 parent d25557c commit 3a7ac02
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
5 changes: 5 additions & 0 deletions tests/library/Rules/Stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public function __construct(bool ...$validations)
$this->validations = $validations;
}

public static function daze(): self
{
return new self();
}

public static function pass(int $expectedCount): self
{
return new self(...array_fill(0, $expectedCount, true));
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function shouldCreateValidationExceptionWhenExceptionIsNotFound(): void
{
$factory = new Factory();
$input = 'input';
$rule = Stub::pass(0);
$rule = Stub::daze();

self::assertInstanceOf(ValidationException::class, $factory->exception($rule, $input));
}
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/Rules/EachTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function itShouldNotOverrideMessages(): void
public static function providerForValidInput(): iterable
{
return [
[new Each(Stub::pass(0)), []],
[new Each(Stub::daze()), []],
[new Each(Stub::pass(5)), [1, 2, 3, 4, 5]],
[new Each(Stub::pass(5)), self::createTraversableInput(1, 5)],
[new Each(Stub::pass(5)), self::createStdClassInput(1, 5)],
Expand All @@ -85,10 +85,10 @@ public static function providerForValidInput(): iterable
public static function providerForInvalidInput(): iterable
{
return [
[new Each(Stub::fail(0)), 123],
[new Each(Stub::fail(0)), ''],
[new Each(Stub::fail(0)), null],
[new Each(Stub::fail(0)), false],
[new Each(Stub::daze()), 123],
[new Each(Stub::daze()), ''],
[new Each(Stub::daze()), null],
[new Each(Stub::daze()), false],
[new Each(Stub::fail(5)), ['', 2, 3, 4, 5]],
[new Each(Stub::fail(5)), ['a', 2, 3, 4, 5]],
[new Each(Stub::fail(5)), self::createTraversableInput(1, 5)],
Expand Down
16 changes: 8 additions & 8 deletions tests/unit/Rules/KeySetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public function shouldNotAcceptAllOfWithMoreThanOneKeyRule(): void
$this->expectExceptionMessage('KeySet rule accepts only Key rules');

new KeySet(new AllOf(
new Key('foo', Stub::pass(0), false),
new Key('bar', Stub::pass(0), false),
new Key('foo', Stub::daze(), false),
new Key('bar', Stub::daze(), false),
));
}

Expand All @@ -44,7 +44,7 @@ public function shouldNotAcceptAllOfWithNonKeyRule(): void
$this->expectException(ComponentException::class);
$this->expectExceptionMessage('KeySet rule accepts only Key rules');

new KeySet(new AllOf(Stub::pass(0)));
new KeySet(new AllOf(Stub::daze()));
}

#[Test]
Expand All @@ -53,7 +53,7 @@ public function shouldNotAcceptNonKeyRule(): void
$this->expectException(ComponentException::class);
$this->expectExceptionMessage('KeySet rule accepts only Key rules');

new KeySet(Stub::pass(0));
new KeySet(Stub::daze());
}

#[Test]
Expand All @@ -65,7 +65,7 @@ public function shouldValidateKeysWhenThereAreMissingRequiredKeys(): void

$sut = new KeySet(
new Key('foo', Stub::pass(1), true),
new Key('bar', Stub::pass(0), true),
new Key('bar', Stub::daze(), true),
);

self::assertFalse($sut->validate($input));
Expand All @@ -80,7 +80,7 @@ public function shouldValidateKeysWhenThereAreMissingNonRequiredKeys(): void

$sut = new KeySet(
new Key('foo', Stub::pass(1), true),
new Key('bar', Stub::pass(0), false),
new Key('bar', Stub::daze(), false),
);

self::assertTrue($sut->validate($input));
Expand All @@ -107,8 +107,8 @@ public function shouldValidateKeysWhenThereAreMoreKeys(): void
public function shouldValidateKeysWhenEmpty(): void
{
$sut = new KeySet(
new Key('foo', Stub::pass(0), true),
new Key('bar', Stub::pass(0), true),
new Key('foo', Stub::daze(), true),
new Key('bar', Stub::daze(), true),
);

self::assertFalse($sut->validate([]));
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Rules/NullableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function shouldValidateRuleWhenInputIsNotNullable(mixed $input): void
#[DoesNotPerformAssertions]
public function shouldNotAssertRuleWhenInputIsNull(): void
{
$sut = new Nullable(Stub::pass(0));
$sut = new Nullable(Stub::daze());
$sut->assert(null);
}

Expand All @@ -63,7 +63,7 @@ public function shouldAssertRuleWhenInputIsNotNullable(mixed $input): void
#[DoesNotPerformAssertions]
public function shouldNotCheckRuleWhenInputIsNull(): void
{
$rule = new Nullable(Stub::pass(0));
$rule = new Nullable(Stub::daze());
$rule->check(null);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Rules/PropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function providerForValidInput(): iterable
new WithProperties(),
],
'non mandatory attribute is not present with extra validator' => [
new Property('nonexistent', Stub::pass(0), false),
new Property('nonexistent', Stub::daze(), false),
new WithProperties(),
],
'attribute is present but uninitialized with extra validator' => [
Expand All @@ -58,7 +58,7 @@ public static function providerForInvalidInput(): iterable
{
return [
'attribute is absent without extra validator' => [new Property('barr'), new WithProperties()],
'attribute is absent with extra validator' => [new Property('barr', Stub::fail(0)), new WithProperties()],
'attribute is absent with extra validator' => [new Property('barr', Stub::daze()), new WithProperties()],
'private attribute is not valid based on extra validator' => [
new Property('private', Stub::fail(1)),
new WithProperties(),
Expand Down

0 comments on commit 3a7ac02

Please sign in to comment.