From a33aaf9a44d8e1488005996d6451b877acade51b Mon Sep 17 00:00:00 2001 From: Crapo Date: Sat, 13 Jul 2019 10:38:21 +0200 Subject: [PATCH] Many changes --- README.md | 18 ++++ composer.json | 2 +- src/Asserts/Content/AssertInclude.php | 18 ++++ src/Asserts/Content/AssertLinks.php | 20 ++++- src/Asserts/Content/AssertResource.php | 22 ++++- .../Structure/AssertResourceObject.php | 29 ++++++- src/Asserts/Structure/AssertStructure.php | 8 +- src/Constraint/LinkEqualsConstraint.php | 82 +++++++++++++++++++ src/Factory/BaseFactory.php | 7 +- src/Factory/DocumentFactory.php | 7 +- src/Factory/HasData.php | 5 +- src/Factory/HasErrors.php | 5 +- src/Factory/HasIdentification.php | 5 +- src/Factory/HasLinks.php | 5 +- src/Factory/HasMeta.php | 5 +- src/Factory/HasResourceType.php | 5 +- src/Factory/JsonapiFactory.php | 5 +- src/Factory/ResourceObjectFactory.php | 7 +- src/HasJsonApiAssert.php | 2 + src/Messages.php | 5 +- tests/Factory/BaseFactoryTest.php | 62 ++++++++++++++ tests/Factory/CollectionFactoryTest.php | 47 +++++++++++ 22 files changed, 335 insertions(+), 36 deletions(-) create mode 100644 src/Asserts/Content/AssertInclude.php create mode 100644 src/Constraint/LinkEqualsConstraint.php create mode 100644 tests/Factory/BaseFactoryTest.php diff --git a/README.md b/README.md index 067142b..f5aef54 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/composer.json b/composer.json index e269f0f..7474db1 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ "dms/phpunit-arraysubset-asserts": "^0.1" }, "require-dev": { - "infection/infection": "dev-master" + "infection/infection": "^0.13" }, "authors": [ { diff --git a/src/Asserts/Content/AssertInclude.php b/src/Asserts/Content/AssertInclude.php new file mode 100644 index 0000000..a14d3b5 --- /dev/null +++ b/src/Asserts/Content/AssertInclude.php @@ -0,0 +1,18 @@ +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; + } +} diff --git a/src/Factory/BaseFactory.php b/src/Factory/BaseFactory.php index 773a34b..7fa968a 100644 --- a/src/Factory/BaseFactory.php +++ b/src/Factory/BaseFactory.php @@ -1,5 +1,6 @@ {$object})) { $this->{$object} = []; @@ -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} = []; diff --git a/src/Factory/DocumentFactory.php b/src/Factory/DocumentFactory.php index 29faa89..700f319 100644 --- a/src/Factory/DocumentFactory.php +++ b/src/Factory/DocumentFactory.php @@ -1,5 +1,6 @@ */ - protected $relationships; + public $relationships; /** * Undocumented function diff --git a/src/HasJsonApiAssert.php b/src/HasJsonApiAssert.php index be632eb..f13a12d 100644 --- a/src/HasJsonApiAssert.php +++ b/src/HasJsonApiAssert.php @@ -4,6 +4,7 @@ namespace VGirol\JsonApiAssert; use VGirol\JsonApiAssert\Asserts\Content\AssertErrors; +use VGirol\JsonApiAssert\Asserts\Content\AssertInclude; use VGirol\JsonApiAssert\Asserts\Content\AssertJsonapi; use VGirol\JsonApiAssert\Asserts\Content\AssertLinks; use VGirol\JsonApiAssert\Asserts\Content\AssertPagination; @@ -48,6 +49,7 @@ trait HasJsonApiAssert use AssertResource; use AssertLinkage; use AssertErrors; + use AssertInclude; /** * Throws an InvalidArgumentException because of an invalid argument passed to a method. diff --git a/src/Messages.php b/src/Messages.php index 50dcdde..8d8ad26 100644 --- a/src/Messages.php +++ b/src/Messages.php @@ -1,5 +1,6 @@ getMockForAbstractClass(BaseFactory::class); + + PHPUnit::assertObjectNotHasAttribute($object, $factory); + + $factory->addToObject($object, $key, $value); + + PHPUnit::assertObjectHasAttribute($object, $factory); + PHPUnit::assertEquals( + [$key => $value], + $factory->{$object} + ); + } + + /** + * @test + */ + public function addToArray() + { + $object = 'test'; + $value1 = 'value1'; + $value2 = 'value2'; + + $factory = $this->getMockForAbstractClass(BaseFactory::class); + + PHPUnit::assertObjectNotHasAttribute($object, $factory); + + $factory->addToArray($object, $value1); + + PHPUnit::assertObjectHasAttribute($object, $factory); + PHPUnit::assertEquals( + [$value1], + $factory->{$object} + ); + + $factory->addToArray($object, $value2); + + PHPUnit::assertEquals( + [$value1, $value2], + $factory->{$object} + ); + } +} diff --git a/tests/Factory/CollectionFactoryTest.php b/tests/Factory/CollectionFactoryTest.php index cdb27d2..a6e05b7 100644 --- a/tests/Factory/CollectionFactoryTest.php +++ b/tests/Factory/CollectionFactoryTest.php @@ -1,4 +1,5 @@ $type, + 'id' => $id + ] + ); + + $factory = HelperFactory::create('resource-identifier'); + $factory->setId($id); + $factory->setResourceType($type); + array_push( + $collection, + $factory + ); + } + + $factory = HelperFactory::create('collection'); + $factory->setCollection($collection); + + $result = $factory->toArray(); + + PHPUnit::assertSame($expected, $result); + + array_walk($expected, function (&$item) { + $item['meta'] = ['new' => $item['id']]; + }); + + $factory->each(function ($item) { + $item->addToMeta('new', $item->id); + }); + $result = $factory->toArray(); + + PHPUnit::assertSame($expected, $result); + } }