2017-09-27 09:25:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Dev\Constraint;
|
|
|
|
|
|
|
|
use PHPUnit_Framework_Constraint;
|
|
|
|
use PHPUnit_Framework_ExpectationFailedException;
|
2021-10-27 04:39:47 +02:00
|
|
|
use PHPUnit\Framework\Constraint\Constraint;
|
|
|
|
use PHPUnit\Framework\ExpectationFailedException;
|
2017-09-27 09:25:37 +02:00
|
|
|
use SilverStripe\Dev\TestOnly;
|
|
|
|
use SilverStripe\ORM\SS_List;
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
/* -------------------------------------------------
|
|
|
|
*
|
|
|
|
* This version of SSListContains is for both phpunit5 and phpunit 9 because it extends SSListContains
|
|
|
|
* phpunit 6, 7 and 8 are not supported
|
|
|
|
*
|
|
|
|
* @see SilverStripe\Dev\SapphireTest
|
|
|
|
*
|
|
|
|
* -------------------------------------------------
|
|
|
|
*/
|
|
|
|
if (!class_exists(Constraint::class) && !class_exists(PHPUnit_Framework_Constraint::class)) {
|
2017-09-27 09:25:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constraint for checking if a SS_List contains only items matching the given
|
|
|
|
* key-value pairs. Each match must correspond to 1 distinct record.
|
|
|
|
*/
|
|
|
|
class SSListContainsOnly extends SSListContains implements TestOnly
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Check if the test fails due to a not matching item
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $itemNotMatching = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates the constraint for parameter $other
|
|
|
|
*
|
|
|
|
* If $returnResult is set to false (the default), an exception is thrown
|
|
|
|
* in case of a failure. null is returned otherwise.
|
|
|
|
*
|
|
|
|
* If $returnResult is true, the result of the evaluation is returned as
|
|
|
|
* a boolean value instead: true in case of success, false in case of a
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
* @param SS_List $other Value or object to evaluate.
|
|
|
|
* @param string $description Additional information about the test
|
|
|
|
* @param bool $returnResult Whether to return a result or throw an exception
|
|
|
|
*
|
|
|
|
* @return null|bool
|
|
|
|
*
|
2021-10-27 04:39:47 +02:00
|
|
|
* @throws PHPUnit_Framework_ExpectationFailedException|ExpectationFailedException
|
2017-09-27 09:25:37 +02:00
|
|
|
*/
|
2021-10-27 04:39:47 +02:00
|
|
|
public function evaluate($other, $description = '', $returnResult = false): ?bool
|
2017-09-27 09:25:37 +02:00
|
|
|
{
|
|
|
|
$success = true;
|
|
|
|
|
|
|
|
foreach ($other as $item) {
|
|
|
|
if (!$this->checkIfItemEvaluatesRemainingMatches($item)) {
|
|
|
|
$this->itemNotMatching = true;
|
|
|
|
$success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//we have remaining matches?
|
2022-04-14 03:12:59 +02:00
|
|
|
if (!$this->itemNotMatching && count($this->matches ?? []) !== 0) {
|
2017-09-27 09:25:37 +02:00
|
|
|
$success = false;
|
|
|
|
$this->hasLeftoverItems = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($returnResult) {
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$success) {
|
|
|
|
$this->fail($other, $description);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function getStubForToString(): string
|
2017-09-27 09:25:37 +02:00
|
|
|
{
|
|
|
|
return $this->itemNotMatching
|
|
|
|
? parent::getStubForToString()
|
|
|
|
: " contained only the given items, the following items were left over:\n";
|
|
|
|
}
|
|
|
|
}
|