Skip to content

Commit 5a81a75

Browse files
committed
feat: add orNot and andNot method
1 parent 5d5f698 commit 5a81a75

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/CompositeSpecification.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ public function or(Specification $specification): CompositeSpecification
3131
]);
3232
}
3333

34+
/**
35+
* @param Specification<TCandidate> $specification
36+
*
37+
* @return CompositeSpecification<TCandidate>
38+
*/
39+
public function orNot(Specification $specification): CompositeSpecification
40+
{
41+
return $this->or(new NotSpecification($specification));
42+
}
43+
3444
/**
3545
* @param Specification<TCandidate> $specification
3646
*
@@ -43,4 +53,14 @@ public function and(Specification $specification): CompositeSpecification
4353
$specification,
4454
]);
4555
}
56+
57+
/**
58+
* @param Specification<TCandidate> $specification
59+
*
60+
* @return CompositeSpecification<TCandidate>
61+
*/
62+
public function andNot(Specification $specification): CompositeSpecification
63+
{
64+
return $this->and(new NotSpecification($specification));
65+
}
4666
}

tests/CompositeSpecificationTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function it_should_return_true_when_chaining_the_specification_twice(): v
4444
}
4545

4646
/** @test */
47-
public function it_should_return_true_when_chaining_a_satisied_and_a_dissatisfied_or_specification(): void
47+
public function it_should_return_true_when_chaining_a_satisfied_and_a_dissatisfied_or_specification(): void
4848
{
4949
// Act
5050
$satisfied = $this->specification
@@ -70,7 +70,19 @@ public function it_should_return_true_when_combining_an_inverting_not_method_wit
7070
}
7171

7272
/** @test */
73-
public function it_should_return_true_when_chaining_multiple_satisied_and_specifications(): void
73+
public function it_should_be_possible_to_chain_the_or_not_specification(): void
74+
{
75+
// Act
76+
$satisfied = (new NegativeSpecification())
77+
->orNot(new NegativeSpecification())
78+
->isSatisfiedBy(null);
79+
80+
// Assert
81+
$this->assertTrue($satisfied);
82+
}
83+
84+
/** @test */
85+
public function it_should_return_true_when_chaining_multiple_satisfied_and_specifications(): void
7486
{
7587
// Act
7688
$satisfied = $this->specification
@@ -82,4 +94,16 @@ public function it_should_return_true_when_chaining_multiple_satisied_and_specif
8294
// Assert
8395
$this->assertTrue($satisfied);
8496
}
97+
98+
/** @test */
99+
public function it_should_be_possible_to_chain_the_and_not_specification(): void
100+
{
101+
// Act
102+
$satisfied = $this->specification
103+
->andNot(new NegativeSpecification())
104+
->isSatisfiedBy(null);
105+
106+
// Assert
107+
$this->assertTrue($satisfied);
108+
}
85109
}

0 commit comments

Comments
 (0)