From 452d4b26b966b1c8c24d00f5798f1a8940171132 Mon Sep 17 00:00:00 2001 From: abenerd Date: Sun, 8 May 2022 11:09:35 +0200 Subject: [PATCH] add new expectation --- src/Mixins/Expectation.php | 16 +++++++++++++++ .../Expect/toContainOnlyInstancesOf.php | 20 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 tests/Features/Expect/toContainOnlyInstancesOf.php 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);