This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
2,180 additions
and
122 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VGirol\JsonApiAssert\Constraint; | ||
|
||
use PHPUnit\Framework\Constraint\Constraint; | ||
use VGirol\JsonApiAssert\Constraint\LinkEqualsConstraint; | ||
use VGirol\JsonApiAssert\Members; | ||
|
||
/** | ||
* A constraint class to assert that a link object equals an expected value. | ||
*/ | ||
class PaginationLinksEqualConstraint extends Constraint | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $expected; | ||
|
||
/** | ||
* Class constructor. | ||
* | ||
* @param array $expected | ||
*/ | ||
public function __construct($expected) | ||
{ | ||
$this->expected = $expected; | ||
} | ||
|
||
/** | ||
* Returns a string representation of the constraint. | ||
*/ | ||
public function toString(): string | ||
{ | ||
return \sprintf( | ||
'equals %s', | ||
$this->exporter()->export($this->expected) | ||
); | ||
} | ||
|
||
/** | ||
* 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 | ||
{ | ||
// Add missing members with false value | ||
$cleanExpected = array_merge( | ||
array_fill_keys(static::allowedMembers(), false), | ||
$this->expected | ||
); | ||
asort($cleanExpected); | ||
|
||
// Extract only pagination members from incoming json | ||
$cleanJson = array_intersect_key($other, array_flip($this->allowedMembers())); | ||
asort($cleanJson); | ||
|
||
// Search for unexpected members | ||
$notExpectedMembers = array_keys( | ||
array_filter( | ||
$cleanExpected, | ||
function ($value) { | ||
return $value === false; | ||
} | ||
) | ||
); | ||
if (count(array_intersect_key($cleanJson, array_flip($notExpectedMembers))) !== 0) { | ||
return false; | ||
} | ||
|
||
// Extracts expected members | ||
$expectedMembers = array_filter( | ||
$cleanExpected, | ||
function ($value) { | ||
return $value !== false; | ||
} | ||
); | ||
if (array_keys($expectedMembers) != array_keys($cleanJson)) { | ||
return false; | ||
} | ||
|
||
// Extracts members whose value have to be tested | ||
$expectedValues = array_filter( | ||
$expectedMembers, | ||
function ($value) { | ||
return $value !== true; | ||
} | ||
); | ||
|
||
foreach ($expectedValues as $name => $expectedLink) { | ||
$constraint = new LinkEqualsConstraint($expectedLink); | ||
if ($constraint->check($cleanJson[$name]) === false) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Gets the list of allowed members for pagination links | ||
* | ||
* @return array | ||
*/ | ||
private static function allowedMembers(): array | ||
{ | ||
return [ | ||
Members::FIRST, | ||
Members::LAST, | ||
Members::PREV, | ||
Members::NEXT | ||
]; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.