Skip to content

Commit 244490c

Browse files
committed
Add support for nullable expectations
1 parent 12a396b commit 244490c

14 files changed

+780
-132
lines changed

src/Expectation.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
final readonly class Expectation implements Expectable
2222
{
23-
private Exporter $exporter;
23+
public Exporter $exporter;
2424

2525
/**
2626
* @param TValue $value
@@ -39,6 +39,14 @@ public function not(): NegatedExpectation
3939
return new NegatedExpectation($this);
4040
}
4141

42+
/**
43+
* @return NullableExpectation<TValue>
44+
*/
45+
public function nullOr(): NullableExpectation
46+
{
47+
return new NullableExpectation($this);
48+
}
49+
4250
/**
4351
* @return self<TValue>
4452
*/

src/NegatedExpectation.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@
2424
*/
2525
final readonly class NegatedExpectation implements Expectable
2626
{
27-
private Exporter $exporter;
27+
/**
28+
* @var TValue
29+
*/
30+
public mixed $value;
2831

2932
/**
3033
* @param Expectation<TValue> $expectation
3134
*/
3235
public function __construct(
3336
public Expectation $expectation,
3437
) {
35-
$this->exporter = new Exporter();
38+
$this->value = $expectation->value;
3639
}
3740

3841
/**
@@ -48,7 +51,7 @@ public function isArray(?string $message = null): self
4851

4952
throw new ExpectationFailedException(
5053
$message ?? 'Value "{value}" is not expected to pass the negated expectation for method "isArray".',
51-
['value' => $this->exporter->exportValue($this->expectation->value)],
54+
['value' => $this->expectation->exporter->exportValue($this->value)],
5255
);
5356
}
5457

@@ -65,7 +68,7 @@ public function isBool(?string $message = null): self
6568

6669
throw new ExpectationFailedException(
6770
$message ?? 'Value "{value}" is not expected to pass the negated expectation for method "isBool".',
68-
['value' => $this->exporter->exportValue($this->expectation->value)],
71+
['value' => $this->expectation->exporter->exportValue($this->value)],
6972
);
7073
}
7174

@@ -82,7 +85,7 @@ public function isFalse(?string $message = null): self
8285

8386
throw new ExpectationFailedException(
8487
$message ?? 'Value "{value}" is not expected to pass the negated expectation for method "isFalse".',
85-
['value' => $this->exporter->exportValue($this->expectation->value)],
88+
['value' => $this->expectation->exporter->exportValue($this->value)],
8689
);
8790
}
8891

@@ -99,7 +102,7 @@ public function isFloat(?string $message = null): self
99102

100103
throw new ExpectationFailedException(
101104
$message ?? 'Value "{value}" is not expected to pass the negated expectation for method "isFloat".',
102-
['value' => $this->exporter->exportValue($this->expectation->value)],
105+
['value' => $this->expectation->exporter->exportValue($this->value)],
103106
);
104107
}
105108

@@ -116,7 +119,7 @@ public function isInt(?string $message = null): self
116119

117120
throw new ExpectationFailedException(
118121
$message ?? 'Value "{value}" is not expected to pass the negated expectation for method "isInt".',
119-
['value' => $this->exporter->exportValue($this->expectation->value)],
122+
['value' => $this->expectation->exporter->exportValue($this->value)],
120123
);
121124
}
122125

@@ -133,7 +136,7 @@ public function isIterable(?string $message = null): self
133136

134137
throw new ExpectationFailedException(
135138
$message ?? 'Value "{value}" is not expected to pass the negated expectation for method "isIterable".',
136-
['value' => $this->exporter->exportValue($this->expectation->value)],
139+
['value' => $this->expectation->exporter->exportValue($this->value)],
137140
);
138141
}
139142

@@ -150,7 +153,7 @@ public function isNull(?string $message = null): self
150153

151154
throw new ExpectationFailedException(
152155
$message ?? 'Value "{value}" is not expected to pass the negated expectation for method "isNull".',
153-
['value' => $this->exporter->exportValue($this->expectation->value)],
156+
['value' => $this->expectation->exporter->exportValue($this->value)],
154157
);
155158
}
156159

@@ -167,7 +170,7 @@ public function isNumeric(?string $message = null): self
167170

168171
throw new ExpectationFailedException(
169172
$message ?? 'Value "{value}" is not expected to pass the negated expectation for method "isNumeric".',
170-
['value' => $this->exporter->exportValue($this->expectation->value)],
173+
['value' => $this->expectation->exporter->exportValue($this->value)],
171174
);
172175
}
173176

@@ -184,7 +187,7 @@ public function isObject(?string $message = null): self
184187

185188
throw new ExpectationFailedException(
186189
$message ?? 'Value "{value}" is not expected to pass the negated expectation for method "isObject".',
187-
['value' => $this->exporter->exportValue($this->expectation->value)],
190+
['value' => $this->expectation->exporter->exportValue($this->value)],
188191
);
189192
}
190193

@@ -201,7 +204,7 @@ public function isScalar(?string $message = null): self
201204

202205
throw new ExpectationFailedException(
203206
$message ?? 'Value "{value}" is not expected to pass the negated expectation for method "isScalar".',
204-
['value' => $this->exporter->exportValue($this->expectation->value)],
207+
['value' => $this->expectation->exporter->exportValue($this->value)],
205208
);
206209
}
207210

@@ -218,7 +221,7 @@ public function isString(?string $message = null): self
218221

219222
throw new ExpectationFailedException(
220223
$message ?? 'Value "{value}" is not expected to pass the negated expectation for method "isString".',
221-
['value' => $this->exporter->exportValue($this->expectation->value)],
224+
['value' => $this->expectation->exporter->exportValue($this->value)],
222225
);
223226
}
224227

@@ -235,7 +238,7 @@ public function isTrue(?string $message = null): self
235238

236239
throw new ExpectationFailedException(
237240
$message ?? 'Value "{value}" is not expected to pass the negated expectation for method "isTrue".',
238-
['value' => $this->exporter->exportValue($this->expectation->value)],
241+
['value' => $this->expectation->exporter->exportValue($this->value)],
239242
);
240243
}
241244
}

0 commit comments

Comments
 (0)