diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 71316264..ace37cf9 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -799,6 +799,22 @@ final class Expectation return $this; } + /** + * @param class-string $class + * + * @return Expectation + */ + public function toContainOnlyInstancesOf(string $class): Expectation + { + if (!is_iterable($this->value)) { + InvalidExpectationValue::expected('iterable'); + } + + Assert::assertContainsOnlyInstancesOf($class, $this->value); + + return $this; + } + /** * Asserts that executing value throws an exception. * diff --git a/tests/Features/Expect/toContainOnlyInstancesOf.php b/tests/Features/Expect/toContainOnlyInstancesOf.php new file mode 100644 index 00000000..25ce664c --- /dev/null +++ b/tests/Features/Expect/toContainOnlyInstancesOf.php @@ -0,0 +1,20 @@ +times = [new DateTimeImmutable(), new DateTimeImmutable()]; +}); + +test('pass', function () { + expect($this->times)->toContainOnlyInstancesOf(DateTimeImmutable::class); + expect($this->times)->not->toContainOnlyInstancesOf(DateTime::class); +}); + +test('failures', function () { + expect($this->times)->toContainOnlyInstancesOf(DateTime::class); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect($this->times)->not->toContainOnlyInstancesOf(DateTimeImmutable::class); +})->throws(ExpectationFailedException::class);