Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
New minor version
Browse files Browse the repository at this point in the history
  • Loading branch information
VGirol committed Jul 20, 2019
1 parent a33aaf9 commit dc9af5f
Show file tree
Hide file tree
Showing 41 changed files with 2,180 additions and 122 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ All notable changes to `jsonapi-assert` will be documented in this file.
- Linted many files
- Refactored some unit tests
- Updated the README file

## 1.1.0 - 2019-07-20

- Added Factory classes for tests
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"license": "MIT",
"require": {
"php": "^7.2",
"phpunit/phpunit": "^8.0",
"dms/phpunit-arraysubset-asserts": "^0.1"
"phpunit/phpunit": "^8.0"
},
"require-dev": {
"infection/infection": "^0.13"
Expand Down
16 changes: 1 addition & 15 deletions src/Asserts/Content/AssertErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace VGirol\JsonApiAssert\Asserts\Content;

use DMS\PHPUnitExtensions\ArraySubset\Constraint\ArraySubset;
use PHPUnit\Framework\Assert as PHPUnit;
use PHPUnit\Framework\ExpectationFailedException;
use VGirol\JsonApiAssert\Messages;
Expand Down Expand Up @@ -35,20 +34,7 @@ public static function assertErrorsContains($expectedErrors, $errors, $strict)
);

foreach ($expectedErrors as $expectedError) {
$test = false;
$constraint = new ArraySubset($expectedError, true);
foreach ($errors as $error) {
$test = $test || $constraint->evaluate($error, '', true);
}

PHPUnit::assertTrue(
$test,
sprintf(
Messages::ERRORS_OBJECT_DOES_NOT_CONTAIN_EXPECTED_ERROR,
var_export($errors, true),
var_export($expectedError, true)
)
);
PHPUnit::assertContains($expectedError, $errors);
}
}
}
4 changes: 2 additions & 2 deletions src/Asserts/Content/AssertLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static function assertLinksObjectContains($name, $expected, $links): void
/**
* Asserts that a link object equals an expected value.
*
* @param array $expected
* @param string|null $expected
* @param array $link
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
Expand All @@ -59,7 +59,7 @@ public static function assertLinkObjectEquals($expected, $link, string $message
/**
* Returns a new instance of the \VGirol\JsonApiAssert\Constraint\LinkEqualsConstraint class.
*
* @param string $expected The expected link
* @param string|null $expected The expected link
*
* @return \VGirol\JsonApiAssert\Constraint\LinkEqualsConstraint
*/
Expand Down
62 changes: 35 additions & 27 deletions src/Asserts/Content/AssertPagination.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
declare (strict_types = 1);

declare(strict_types=1);

namespace VGirol\JsonApiAssert\Asserts\Content;

use PHPUnit\Framework\Assert as PHPUnit;
use VGirol\JsonApiAssert\Constraint\PaginationLinksEqualConstraint;
use VGirol\JsonApiAssert\Members;

/**
Expand All @@ -12,21 +14,33 @@
trait AssertPagination
{
/**
* Asserts that a links object has pagination links.
* Gets the list of allowed members for pagination links
*
* @param array $links
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @return array
*/
public static function assertHasPaginationLinks($links): void
private static function allowedMembers(): array
{
$members = [
return [
Members::FIRST,
Members::LAST,
Members::PREV,
Members::NEXT
];
static::assertContainsAtLeastOneMember($members, $links);
}

/**
* Asserts that a links object has pagination links.
*
* @param array $links
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
*/
public static function assertHasPaginationLinks($links): void
{
static::assertContainsAtLeastOneMember(
static::allowedMembers(),
$links
);
}

/**
Expand All @@ -38,13 +52,7 @@ public static function assertHasPaginationLinks($links): void
*/
public static function assertHasNoPaginationLinks($links): void
{
$members = [
Members::FIRST,
Members::LAST,
Members::PREV,
Members::NEXT
];
foreach ($members as $name) {
foreach (static::allowedMembers() as $name) {
static::assertNotHasMember($name, $links);
}
}
Expand All @@ -59,18 +67,18 @@ public static function assertHasNoPaginationLinks($links): void
*/
public static function assertPaginationLinksEquals($expected, $json): void
{
foreach ($expected as $name => $expectedLink) {
if ($expectedLink === false) {
PHPUnit::assertNotHasMember($name, $json);
continue;
}
static::assertHasMember($name, $json);

if ($expectedLink === true) {
continue;
}
PHPUnit::assertThat($json, self::paginationLinksEqualConstraint($expected));
}

static::assertLinksObjectContains($name, $expectedLink, $json);
}
/**
* Returns a new instance of the \VGirol\JsonApiAssert\Constraint\PaginationLinksEqualConstraint class.
*
* @param array $expected The expected links
*
* @return \VGirol\JsonApiAssert\Constraint\PaginationLinksEqualConstraint
*/
private static function paginationLinksEqualConstraint($expected): PaginationLinksEqualConstraint
{
return new PaginationLinksEqualConstraint($expected);
}
}
2 changes: 1 addition & 1 deletion src/Asserts/Content/AssertResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function assertResourceObjectEquals($expected, $json)
*/
public static function assertResourceCollectionEquals($expected, $json)
{
static::assertIsArrayOfObjects($json);
static::assertIsArrayOfObjects($expected);
PHPUnit::assertEquals(count($expected), count($json));

$index = 0;
Expand Down
6 changes: 5 additions & 1 deletion src/Asserts/Content/AssertResourceLinkage.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare (strict_types = 1);

declare(strict_types=1);

namespace VGirol\JsonApiAssert\Asserts\Content;

Expand Down Expand Up @@ -55,6 +56,9 @@ public static function assertResourceLinkageEquals($expected, $json, $strict)
return;
}

PHPUnit::assertNotNull($json);
/** @var array $json */

if (!static::isArrayOfObjects($expected)) {
static::assertIsNotArrayOfObjects($json);
static::assertResourceIdentifierEquals($expected, $json);
Expand Down
16 changes: 14 additions & 2 deletions src/Constraint/LinkEqualsConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function __construct($expected)
public function toString(): string
{
return \sprintf(
'equals "%s"',
$this->expected
'equals %s',
$this->exporter()->export($this->expected)
);
}

Expand Down Expand Up @@ -79,4 +79,16 @@ protected function matches($other): bool

return count($diff) === 0;
}

/**
* Evaluates the constraint for parameter $other. Returns true if the
* constraint is met, false otherwise.
*
* @param mixed $other value or object to evaluate
* @return boolean
*/
public function check($other): bool
{
return $this->matches($other);
}
}
117 changes: 117 additions & 0 deletions src/Constraint/PaginationLinksEqualConstraint.php
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
];
}
}
5 changes: 3 additions & 2 deletions src/Factory/CollectionFactory.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare (strict_types = 1);

declare(strict_types=1);

namespace VGirol\JsonApiAssert\Factory;

Expand All @@ -10,7 +11,7 @@ class CollectionFactory extends BaseFactory
*
* @var array
*/
protected $array;
public $array;

/**
* Undocumented function
Expand Down
8 changes: 4 additions & 4 deletions src/Factory/DocumentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

class DocumentFactory extends BaseFactory
{
use HasMeta;
use HasData;
use HasErrors;
use HasLinks;
use HasData;
use HasMeta;

/**
* Undocumented variable
Expand Down Expand Up @@ -67,10 +67,10 @@ public function toArray(): array
$json[Members::ERRORS] = $this->errors;
}
if (isset($this->data)) {
$json[Members::DATA] = $this->data;
$json[Members::DATA] = $this->data->toArray();
}
if (isset($this->included)) {
$json[Members::INCLUDED] = $this->included;
$json[Members::INCLUDED] = $this->included->toArray();
}
if (isset($this->jsonapi)) {
$json[Members::JSONAPI] = $this->jsonapi;
Expand Down
2 changes: 1 addition & 1 deletion src/Factory/HasIdentification.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function setId($resourceId)
return $this;
}

protected function getIdentification(): ?array
public function getIdentification(): ?array
{
if (!isset($this->id) && !isset($this->resourceType)) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Factory/HasResourceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait HasResourceType
/**
* Undocumented variable
*
* @var string
* @var string|null
*/
public $resourceType;

Expand Down
Loading

0 comments on commit dc9af5f

Please sign in to comment.