Created
January 15, 2021 23:30
-
-
Save sebdesign/27a347efb7fe43c8a507edca71cc9833 to your computer and use it in GitHub Desktop.
assertCollectionContains
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests; | |
use Illuminate\Database\Eloquent\Model; | |
use PHPUnit\Framework\Constraint\Constraint; | |
/** | |
* Constraint that asserts that the Collection it is applied to contains | |
* a given value. | |
*/ | |
final class CollectionContains extends Constraint | |
{ | |
/** | |
* @var mixed | |
*/ | |
private $value; | |
/** | |
* @param mixed $value | |
* @throws \PHPUnit\Framework\Exception | |
*/ | |
public function __construct($value) | |
{ | |
$this->value = $value; | |
} | |
/** | |
* Returns a string representation of the constraint. | |
* | |
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException | |
*/ | |
public function toString(): string | |
{ | |
if ($this->value instanceof Model) { | |
return \trim(\vsprintf('contains [%s] %s', [ | |
\get_class($this->value), | |
$this->value->getKey(), | |
])); | |
} | |
if (\is_string($this->value) && \strpos($this->value, "\n") !== false) { | |
return 'contains "' . $this->value . '"'; | |
} | |
return 'contains ' . $this->exporter()->export($this->value); | |
} | |
/** | |
* Evaluates the constraint for parameter $other. Returns true if the | |
* constraint is met, false otherwise. | |
* | |
* @param mixed $other value or object to evaluate | |
*/ | |
protected function matches($other): bool | |
{ | |
return $other->contains($this->value); | |
} | |
/** | |
* Returns the description of the failure | |
* | |
* The beginning of failure messages is "Failed asserting that" in most | |
* cases. This method should return the second part of that sentence. | |
* | |
* @param mixed $other evaluated value or object | |
* | |
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException | |
*/ | |
protected function failureDescription($other): string | |
{ | |
return \sprintf( | |
'%s %s', | |
'a collection', | |
$this->toString() | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests; | |
use Illuminate\Foundation\Testing\TestCase as BaseTestCase; | |
use PHPUnit\Framework\Constraint\LogicalNot; | |
class TestCase extends BaseTestCase | |
{ | |
/** | |
* Assert the collection contains the expected collection. | |
* | |
* @param mixed $needle | |
* @param mixed $haystack | |
*/ | |
protected function assertCollectionContains($needle, $haystack, string $message = ''): void | |
{ | |
$this->assertThat($haystack, new CollectionContains($needle), $message); | |
} | |
/** | |
* Assert the collection doesn't contain the expected collection. | |
* | |
* @param mixed $needle | |
* @param mixed $haystack | |
*/ | |
protected function assertCollectionNotContains($needle, $haystack, string $message = ''): void | |
{ | |
$this->assertThat($haystack, new LogicalNot(new CollectionContains($needle)), $message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment