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

Commit

Permalink
Many changes
Browse files Browse the repository at this point in the history
  • Loading branch information
VGirol committed Jul 13, 2019
1 parent c6cec55 commit a33aaf9
Show file tree
Hide file tree
Showing 22 changed files with 335 additions and 36 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,24 @@ It will do the following checks :
- if presents, asserts that the "meta" member is valid ([assertIsValidMetaObject](#assertIsValidMetaObject)).
- asserts that the resource has valid fields ([assertHasValidFields](#assertHasValidFields)).

### assertIsValidResourceObjectCollection

Asserts that a json fragment is a valid collection of resource objects.

Definition :

`assertIsValidResourceObjectCollection($json, $strict)`

Parameters :

- `array` `$json`
- `boolean` `$strict` if true, unsafe characters are not allowed when checking members name.

It will do the following checks :

- asserts that the provided resource collection is either an empty array or an array of objects.
- asserts that the collection of resources is valid ([assertIsValidResourceObject](#assertIsValidResourceObject)).

### assertIsValidTopLevelLinksMember

Asserts that a json fragment is a valid top-level links member.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dms/phpunit-arraysubset-asserts": "^0.1"
},
"require-dev": {
"infection/infection": "dev-master"
"infection/infection": "^0.13"
},
"authors": [
{
Expand Down
18 changes: 18 additions & 0 deletions src/Asserts/Content/AssertInclude.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare (strict_types = 1);

namespace VGirol\JsonApiAssert\Asserts\Content;

trait AssertInclude
{
/**
* Asserts that an array of included resource objects contains a given resource object or collection.
*
* @param array $expected
* @param array $json
*/
public static function assertIncludeObjectContains($expected, $json)
{
static::assertResourceCollectionContains($expected, $json);
}
}
20 changes: 17 additions & 3 deletions src/Asserts/Content/AssertLinks.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\LinkEqualsConstraint;

/**
* Assertions relating to the pagination
Expand Down Expand Up @@ -49,8 +51,20 @@ public static function assertLinksObjectContains($name, $expected, $links): void
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
*/
public static function assertLinkObjectEquals($expected, $link): void
public static function assertLinkObjectEquals($expected, $link, string $message = ''): void
{
PHPUnit::assertThat($link, self::linkEqualsConstraint($expected), $message);
}

/**
* Returns a new instance of the \VGirol\JsonApiAssert\Constraint\LinkEqualsConstraint class.
*
* @param string $expected The expected link
*
* @return \VGirol\JsonApiAssert\Constraint\LinkEqualsConstraint
*/
private static function linkEqualsConstraint($expected): LinkEqualsConstraint
{
PHPUnit::assertSame($expected, $link);
return new LinkEqualsConstraint($expected);
}
}
22 changes: 20 additions & 2 deletions src/Asserts/Content/AssertResource.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 All @@ -19,7 +20,7 @@ public static function assertResourceObjectEquals($expected, $json)
}

/**
* Asserts that an array of resource objects correspond to a given collection.
* Asserts that an array of resource objects is equal to a given collection (same values and same order).
*
* @param array $expected
* @param array $json
Expand All @@ -35,4 +36,21 @@ public static function assertResourceCollectionEquals($expected, $json)
$index++;
}
}

/**
* Asserts that an array of resource objects contains a given collection.
*
* @param array $expected
* @param array $json
*/
public static function assertResourceCollectionContains($expected, $json)
{
if (!static::isArrayOfObjects($expected)) {
$expected = [$expected];
}

foreach ($expected as $resource) {
PHPUnit::assertContains($resource, $json);
}
}
}
29 changes: 28 additions & 1 deletion src/Asserts/Structure/AssertResourceObject.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\Structure;

Expand All @@ -12,6 +13,32 @@
*/
trait AssertResourceObject
{
/**
* Asserts that a json fragment is a valid collection of resource objects.
*
* @param array|null $json
* @param boolean $strict If true, unsafe characters are not allowed when checking members name.
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
*/
public static function assertIsValidResourceObjectCollection($json, bool $strict): void
{
PHPUnit::assertIsArray(
$json,
Messages::RESOURCE_COLLECTION_NOT_ARRAY
);

if (empty($json)) {
return;
}

static::assertIsArrayOfObjects($json);

foreach ($json as $resource) {
static::assertIsValidResourceObject($resource, $strict);
}
}

/**
* Asserts that a json fragment is a valid resource.
*
Expand Down
8 changes: 3 additions & 5 deletions src/Asserts/Structure/AssertStructure.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\Structure;

Expand Down Expand Up @@ -210,10 +211,7 @@ private static function assertIsValidPrimarySingle($resource, bool $strict): voi
*/
public static function assertIsValidIncludedCollection($included, $data, bool $strict): void
{
static::assertIsArrayOfObjects($included);
foreach ($included as $resource) {
static::assertIsValidResourceObject($resource, $strict);
}
static::assertIsValidResourceObjectCollection($included, $strict);

$resIdentifiers = array_merge(
static::getAllResourceIdentifierObjects($data),
Expand Down
82 changes: 82 additions & 0 deletions src/Constraint/LinkEqualsConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace VGirol\JsonApiAssert\Constraint;

use PHPUnit\Framework\Constraint\Constraint;

/**
* A constraint class to assert that a link object equals an expected value.
*/
class LinkEqualsConstraint extends Constraint
{
/**
* @var string|null
*/
private $expected;

/**
* Class constructor.
*
* @param string|null $expected
*/
public function __construct($expected)
{
$this->expected = $expected;
}

/**
* Returns a string representation of the constraint.
*/
public function toString(): string
{
return \sprintf(
'equals "%s"',
$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
{
if (is_null($this->expected)) {
return \is_null($other);
}

if (is_null($other)) {
return false;
}

$linkElms = explode('?', $other);
$expectedElms = explode('?', $this->expected);

if (count($expectedElms) != count($linkElms)) {
return false;
}

if ($expectedElms[0] != $linkElms[0]) {
return false;
}

if (count($linkElms) == 1) {
return true;
}

$expectedQuery = explode('&', $expectedElms[1]);
$linkQuery = explode('&', $linkElms[1]);

if (count($expectedQuery) != count($linkQuery)) {
return false;
}

$diff = array_diff($expectedQuery, $linkQuery);

return count($diff) === 0;
}
}
7 changes: 4 additions & 3 deletions src/Factory/BaseFactory.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php
declare (strict_types = 1);

declare(strict_types=1);

namespace VGirol\JsonApiAssert\Factory;

abstract class BaseFactory
{
abstract public function toArray(): ?array;

protected function addToObject(string $object, string $name, $value): void
public function addToObject(string $object, string $name, $value): void
{
if (!isset($this->{$object})) {
$this->{$object} = [];
Expand All @@ -16,7 +17,7 @@ protected function addToObject(string $object, string $name, $value): void
$this->{$object}[$name] = $value;
}

protected function addToArray(string $object, $value): void
public function addToArray(string $object, $value): void
{
if (!isset($this->{$object})) {
$this->{$object} = [];
Expand Down
7 changes: 4 additions & 3 deletions src/Factory/DocumentFactory.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 @@ -17,14 +18,14 @@ class DocumentFactory extends BaseFactory
*
* @var CollectionFactory
*/
protected $included;
public $included;

/**
* Undocumented variable
*
* @var JsonapiFactory
*/
protected $jsonapi;
public $jsonapi;

/**
* Undocumented function
Expand Down
5 changes: 3 additions & 2 deletions src/Factory/HasData.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 @@ trait HasData
*
* @var ResourceIdentifierFactory|ResourceObjectFactory|CollectionFactory|null
*/
protected $data = null;
public $data = null;

/**
* Undocumented function
Expand Down
5 changes: 3 additions & 2 deletions src/Factory/HasErrors.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 @@ trait HasErrors
*
* @var array
*/
protected $errors;
public $errors;

/**
* Undocumented function
Expand Down
5 changes: 3 additions & 2 deletions src/Factory/HasIdentification.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 @@ -9,7 +10,7 @@ trait HasIdentification
{
use HasResourceType;

protected $id;
public $id;

/**
* Undocumented function
Expand Down
5 changes: 3 additions & 2 deletions src/Factory/HasLinks.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
declare (strict_types = 1);

declare(strict_types=1);

namespace VGirol\JsonApiAssert\Factory;

trait HasLinks
{
protected $links;
public $links;

/**
* Undocumented function
Expand Down
Loading

0 comments on commit a33aaf9

Please sign in to comment.