Skip to content

Add constructor property assertions #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"illuminate/console": "^10.0|^11.0",
"illuminate/support": "^10.0|^11.0",
"spatie/laravel-package-tools": "^1.16.2",
"symfony/polyfill-php83": "^1.30"
"symfony/polyfill-php83": "^1.30",
"webmozart/assert": "^1.11"
},
"require-dev": {
"illuminate/testing": "^10.0|^11.0",
Expand Down
5 changes: 4 additions & 1 deletion src/AndSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Maartenpaauw\Specifications;

use Override;
use Webmozart\Assert\Assert;

/**
* @template TCandidate
Expand All @@ -18,7 +19,9 @@ final class AndSpecification extends CompositeSpecification
*/
public function __construct(
private readonly array $specifications,
) {}
) {
Assert::allIsInstanceOf($specifications, Specification::class);
}

#[Override]
public function isSatisfiedBy(mixed $candidate): bool
Expand Down
5 changes: 4 additions & 1 deletion src/OrSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Maartenpaauw\Specifications;

use Override;
use Webmozart\Assert\Assert;

/**
* @template TCandidate
Expand All @@ -18,7 +19,9 @@ final class OrSpecification extends CompositeSpecification
*/
public function __construct(
private readonly array $specifications,
) {}
) {
Assert::allIsInstanceOf($this->specifications, Specification::class);
}

#[Override]
public function isSatisfiedBy(mixed $candidate): bool
Expand Down
5 changes: 4 additions & 1 deletion src/XorSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Maartenpaauw\Specifications;

use Override;
use Webmozart\Assert\Assert;

/**
* @template TCandidate
Expand All @@ -18,7 +19,9 @@ final class XorSpecification extends CompositeSpecification
*/
public function __construct(
private readonly array $specifications,
) {}
) {
Assert::allIsInstanceOf($specifications, Specification::class);
}

#[Override]
public function isSatisfiedBy(mixed $candidate): bool
Expand Down
9 changes: 9 additions & 0 deletions tests/AndSpecificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Maartenpaauw\Specifications\Tests;

use InvalidArgumentException;
use Maartenpaauw\Specifications\AndSpecification;
use Workbench\App\LengthSpecification;
use Workbench\App\UppercaseSpecification;
Expand All @@ -30,6 +31,14 @@ protected function setUp(): void
]);
}

public function test_it_should_throw_an_invalid_argument_exception_when_the_specification_array_does_contain_invalid_data(): void
{
self::expectException(InvalidArgumentException::class);

// @phpstan-ignore-next-line
new AndSpecification(['invalid']);
}

public function test_it_should_return_true_when_the_candidate_matches_all_specifications(): void
{
// Act
Expand Down
9 changes: 9 additions & 0 deletions tests/OrSpecificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Maartenpaauw\Specifications\Tests;

use InvalidArgumentException;
use Maartenpaauw\Specifications\OrSpecification;
use Workbench\App\LengthSpecification;
use Workbench\App\UppercaseSpecification;
Expand All @@ -30,6 +31,14 @@ protected function setUp(): void
]);
}

public function test_it_should_throw_an_invalid_argument_exception_when_the_specification_array_does_contain_invalid_data(): void
{
self::expectException(InvalidArgumentException::class);

// @phpstan-ignore-next-line
new OrSpecification(['invalid']);
}

public function test_it_should_return_true_when_the_candidate_matches_all_specifications(): void
{
// Act
Expand Down
9 changes: 9 additions & 0 deletions tests/XorSpecificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Maartenpaauw\Specifications\Tests;

use InvalidArgumentException;
use Maartenpaauw\Specifications\XorSpecification;
use PHPUnit\Framework\TestCase;
use Workbench\App\LengthSpecification;
Expand Down Expand Up @@ -31,6 +32,14 @@ protected function setUp(): void
]);
}

public function test_it_should_throw_an_invalid_argument_exception_when_the_specification_array_does_contain_invalid_data(): void
{
self::expectException(InvalidArgumentException::class);

// @phpstan-ignore-next-line
new XorSpecification(['invalid']);
}

public function test_it_should_only_return_true_when_one_specification_is_satisfied(): void
{
$this->assertTrue($this->specification->isSatisfiedBy('HELLO'));
Expand Down