Skip to content
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to `laravel-specification-pattern` will be documented in this file.

## [v2.7.0] - 2024-11-20

### Added

- Support for custom exception code.

## [v2.6.0] - 2024-09-20

### Added
Expand Down Expand Up @@ -76,6 +82,7 @@ All notable changes to `laravel-specification-pattern` will be documented in thi

- initial release

[v2.7.0]: https://github.com/maartenpaauw/laravel-specification-pattern/compare/v2.6.0...v2.7.0
[v2.6.0]: https://github.com/maartenpaauw/laravel-specification-pattern/compare/v2.5.0...v2.6.0
[v2.5.0]: https://github.com/maartenpaauw/laravel-specification-pattern/compare/v2.4.0...v2.5.0
[v2.4.0]: https://github.com/maartenpaauw/laravel-specification-pattern/compare/v2.3.0...v2.4.0
Expand Down
4 changes: 2 additions & 2 deletions src/CompositeSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ final public function andNot(Specification $specification): self
/**
* @return CompositeSpecification<TCandidate>
*/
final public function verbose(string $message = ''): self
final public function verbose(string $message = '', int $code = 0): self
{
return new VerboseSpecification($this, $message);
return new VerboseSpecification($this, $message, $code);
}
}
21 changes: 20 additions & 1 deletion src/VerboseSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class VerboseSpecification extends CompositeSpecification
public function __construct(
private readonly Specification $origin,
private readonly string $message = '',
private readonly int $code = 0,
) {}

/**
Expand All @@ -29,6 +30,19 @@ public function withMessage(string $message): self
return new self(
$this->origin,
$message,
$this->code,
);
}

/**
* @return VerboseSpecification<TCandidate>
*/
public function withCode(int $code): self
{
return new self(
$this->origin,
$this->message,
$code,
);
}

Expand All @@ -37,6 +51,11 @@ public function message(): string
return $this->message;
}

public function code(): int
{
return $this->code;
}

/**
* @param TCandidate $candidate
*
Expand All @@ -49,6 +68,6 @@ public function isSatisfiedBy(mixed $candidate): bool
return true;
}

throw new DissatisfiedSpecification($this->message);
throw new DissatisfiedSpecification($this->message, $this->code);
}
}
3 changes: 2 additions & 1 deletion tests/CompositeSpecificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ public function test_it_should_make_the_specification_verbose(): void
{
// Act
$specification = $this->specification
->verbose('The specification is not satisfied');
->verbose('The specification is not satisfied', 10);

// Assert
$this->assertInstanceOf(VerboseSpecification::class, $specification);
$this->assertSame('The specification is not satisfied', $specification->message());
$this->assertSame(10, $specification->code());
}
}
25 changes: 25 additions & 0 deletions tests/VerboseSpecificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function test_it_should_throw_an_exception_with_an_empty_message_when_the
// Assert
$this->expectException(DissatisfiedSpecification::class);
$this->expectExceptionMessage('');
$this->expectExceptionCode(0);

// Act
$this->specification->isSatisfiedBy('Hello Laravel!');
Expand All @@ -51,6 +52,18 @@ public function test_it_should_be_possible_to_customize_the_exception_message():
->isSatisfiedBy('Hello Laravel!');
}

public function test_it_should_be_possible_to_customize_the_exception_code(): void
{
// Assert
$this->expectException(DissatisfiedSpecification::class);
$this->expectExceptionCode(10);

// Act
$this->specification
->withCode(10)
->isSatisfiedBy('Hello Laravel!');
}

public function test_it_should_return_true_when_the_specification_is_satisfied_with_the_given_candidate(): void
{
// Act
Expand All @@ -71,4 +84,16 @@ public function test_it_should_be_possible_to_receive_the_message(): void
// Assert
$this->assertSame('This is the reason why it is dissatisfied.', $message);
}

public function test_it_should_be_possible_to_receive_the_code(): void
{
// Arrange
$specification = $this->specification->withCode(10);

// Act
$code = $specification->code();

// Assert
$this->assertSame(10, $code);
}
}