Skip to content

Commit

Permalink
API Upgrade SapphireTest to work with phpunit 9
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jul 21, 2021
1 parent 507c70e commit 32377d9
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 48 deletions.
26 changes: 15 additions & 11 deletions src/Dev/Constraint/SSListContains.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

namespace SilverStripe\Dev\Constraint;

use PHPUnit_Framework_Constraint;
use PHPUnit_Framework_ExpectationFailedException;
// use PHPUnit_Framework_Constraint;
use PHPUnit\Framework\Constraint\Constraint;
// use PHPUnit_Framework_ExpectationFailedException;
use PHPUnit\Framework\ExpectationFailedException;
use SilverStripe\Dev\SSListExporter;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\SS_List;
use SilverStripe\View\ViewableData;

if (!class_exists(PHPUnit_Framework_Constraint::class)) {
if (!class_exists(Constraint::class)) {
return;
}

/**
* Constraint for checking if a SS_List contains items matching the given
* key-value pairs.
*/
class SSListContains extends PHPUnit_Framework_Constraint implements TestOnly
class SSListContains extends Constraint implements TestOnly
{
/**
* @var array
Expand All @@ -31,9 +33,8 @@ class SSListContains extends PHPUnit_Framework_Constraint implements TestOnly
*/
protected $hasLeftoverItems = false;

public function __construct($matches)
public function __construct(array $matches)
{
parent::__construct();
$this->exporter = new SSListExporter();

$this->matches = $matches;
Expand All @@ -55,9 +56,9 @@ public function __construct($matches)
*
* @return null|bool
*
* @throws PHPUnit_Framework_ExpectationFailedException
* @throws ExpectationFailedException
*/
public function evaluate($other, $description = '', $returnResult = false)
public function evaluate($other, $description = '', $returnResult = false): ?bool
{
$success = true;

Expand Down Expand Up @@ -86,7 +87,7 @@ public function evaluate($other, $description = '', $returnResult = false)
* @param ViewableData $item
* @return bool
*/
protected function checkIfItemEvaluatesRemainingMatches(ViewableData $item)
protected function checkIfItemEvaluatesRemainingMatches(ViewableData $item): bool
{
$success = false;
foreach ($this->matches as $key => $match) {
Expand All @@ -107,7 +108,7 @@ protected function checkIfItemEvaluatesRemainingMatches(ViewableData $item)
*
* @return string
*/
public function toString()
public function toString(): string
{
$matchToString = function ($key, $value) {
return ' "' . $key . '" is "' . $value . '"';
Expand All @@ -132,7 +133,10 @@ public function toString()
return $this->getStubForToString() . $allMatchesAsString;
}

protected function getStubForToString()
/**
* @return string
*/
protected function getStubForToString(): string
{
return ' contains an item matching ';
}
Expand Down
17 changes: 9 additions & 8 deletions src/Dev/Constraint/SSListContainsOnlyMatchingItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

namespace SilverStripe\Dev\Constraint;

use PHPUnit_Framework_Constraint;
use PHPUnit_Framework_ExpectationFailedException;
// use PHPUnit_Framework_Constraint;
use PHPUnit\Framework\Constraint\Constraint;
// use PHPUnit_Framework_ExpectationFailedException;
use PHPUnit\Framework\ExpectationFailedException;
use SilverStripe\Dev\SSListExporter;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\SS_List;

if (!class_exists(PHPUnit_Framework_Constraint::class)) {
if (!class_exists(Constraint::class)) {
return;
}

/**
* Constraint for checking if every item in a SS_List matches a given match,
* e.g. every Member has isActive set to true
*/
class SSListContainsOnlyMatchingItems extends PHPUnit_Framework_Constraint implements TestOnly
class SSListContainsOnlyMatchingItems extends Constraint implements TestOnly
{
/**
* @var array
Expand All @@ -30,7 +32,6 @@ class SSListContainsOnlyMatchingItems extends PHPUnit_Framework_Constraint imple

public function __construct($match)
{
parent::__construct();
$this->exporter = new SSListExporter();

$this->constraint = new ViewableDataContains($match);
Expand All @@ -53,9 +54,9 @@ public function __construct($match)
*
* @return null|bool
*
* @throws PHPUnit_Framework_ExpectationFailedException
* @throws ExpectationFailedException
*/
public function evaluate($other, $description = '', $returnResult = false)
public function evaluate($other, $description = '', $returnResult = false): ?bool
{
$success = true;

Expand All @@ -82,7 +83,7 @@ public function evaluate($other, $description = '', $returnResult = false)
*
* @return string
*/
public function toString()
public function toString(): string
{
return 'contains only Objects where "' . key($this->match) . '" is "' . current($this->match) . '"';
}
Expand Down
23 changes: 12 additions & 11 deletions src/Dev/Constraint/ViewableDataContains.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace SilverStripe\Dev\Constraint;

use PHPUnit_Framework_Constraint;
use PHPUnit_Framework_ExpectationFailedException;
use PHPUnit_Util_InvalidArgumentHelper;
// use PHPUnit_Framework_Constraint;
use PHPUnit\Framework\Constraint\Constraint;
// use PHPUnit_Framework_ExpectationFailedException;
use PHPUnit\Framework\ExpectationFailedException;
// use PHPUnit_Util_InvalidArgumentHelper;
use SilverStripe\Dev\TestOnly;
use SilverStripe\View\ViewableData;
use SilverStripe\Dev\SapphireTest;

if (!class_exists(PHPUnit_Framework_Constraint::class)) {
return;
Expand All @@ -16,7 +19,7 @@
* Constraint for checking if a ViewableData (e.g. ArrayData or any DataObject) contains fields matching the given
* key-value pairs.
*/
class ViewableDataContains extends PHPUnit_Framework_Constraint implements TestOnly
class ViewableDataContains extends Constraint implements TestOnly
{
/**
* @var array
Expand All @@ -27,12 +30,10 @@ class ViewableDataContains extends PHPUnit_Framework_Constraint implements TestO
* ViewableDataContains constructor.
* @param array $match
*/
public function __construct($match)
public function __construct(array $match)
{
parent::__construct();

if (!is_array($match)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(
throw SapphireTest::createPHPUnitFrameworkException(
1,
'array'
);
Expand All @@ -57,9 +58,9 @@ public function __construct($match)
*
* @return null|bool
*
* @throws PHPUnit_Framework_ExpectationFailedException
* @throws ExpectationFailedException
*/
public function evaluate($other, $description = '', $returnResult = false)
public function evaluate($other, $description = '', $returnResult = false): ?bool
{
$success = true;

Expand Down Expand Up @@ -89,7 +90,7 @@ public function evaluate($other, $description = '', $returnResult = false)
*
* @return string
*/
public function toString()
public function toString(): string
{
return 'contains only Objects where "' . key($this->match) . '" is "' . current($this->match) . '"';
}
Expand Down
Loading

0 comments on commit 32377d9

Please sign in to comment.