Merge pull request #520 from abenerd/feature/new-expectation

add new expectation
This commit is contained in:
Nuno Maduro
2022-05-24 08:00:25 +01:00
committed by GitHub
2 changed files with 36 additions and 0 deletions

View File

@ -799,6 +799,22 @@ final class Expectation
return $this;
}
/**
* @param class-string $class
*
* @return Expectation<TValue>
*/
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.
*

View File

@ -0,0 +1,20 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
beforeEach(function () {
$this->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);