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

Commit

Permalink
Updated methods and tests to use JsonApi-Constant
Browse files Browse the repository at this point in the history
  • Loading branch information
VGirol committed Dec 27, 2019
2 parents 8db863b + c71022f commit bb09a70
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 88 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
## 1.0.3 - 2019-10-04

- Many enhancements, bug fixes and code lint

## 1.0.4 - 2019-12-27

### Fixed

- Updated some methods to use JsonApi-Constant package
4 changes: 3 additions & 1 deletion src/Contract/HasLinksContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace VGirol\JsonApiFaker\Contract;

use VGirol\JsonApiConstant\Members;

/**
* Interface for classes having links property.
*/
Expand Down Expand Up @@ -51,5 +53,5 @@ public function addLink(string $name, $link);
*
* @return static
*/
public function fakeLinks($links = ['self' => ['url']]);
public function fakeLinks($links = [Members::LINK_SELF => ['url']]);
}
4 changes: 2 additions & 2 deletions src/Factory/ErrorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function toArray(): array
public function fake()
{
return $this->fakeIdentifier()
->fakeLinks(['about' => ['url']])
->fakeLinks([Members::LINK_ABOUT => ['url']])
->fakeMeta()
->fakeStatus()
->fakeCode()
Expand Down Expand Up @@ -210,7 +210,7 @@ private function fakeSource()

return $this->setSource(
[
'parameter' => $faker->word
Members::ERROR_PARAMETER => $faker->word
]
);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Factory/BaseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public function toArray(): ?array
return [
'attr' => 'value with <, &, \' and ".',
'arr' => [
'first',
'second'
'val1',
'val2'
],
'float' => 3.0
];
Expand All @@ -107,7 +107,7 @@ public function fake()
};

$json = $obj->toJson();
$expected = '{"attr":"value with <, &, \' and \".","arr":["first","second"],"float":3.0}';
$expected = '{"attr":"value with <, &, \' and \".","arr":["val1","val2"],"float":3.0}';

PHPUnit::assertEquals($expected, $json);
}
Expand Down
23 changes: 12 additions & 11 deletions tests/Factory/CollectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\Assert as PHPUnit;
use VGirol\JsonApiAssert\Assert;
use VGirol\JsonApiConstant\Members;
use VGirol\JsonApiFaker\Exception\JsonApiFakerException;
use VGirol\JsonApiFaker\Factory\CollectionFactory;
use VGirol\JsonApiFaker\Factory\ResourceIdentifierFactory;
Expand Down Expand Up @@ -55,8 +56,8 @@ public function resourceIdentifierCollection()
array_push(
$expected,
[
'type' => $type,
'id' => $id
Members::TYPE => $type,
Members::ID => $id
]
);

Expand Down Expand Up @@ -90,8 +91,8 @@ public function changeEachItemOfTheCollection()
array_push(
$expected,
[
'type' => $type,
'id' => $id
Members::TYPE => $type,
Members::ID => $id
]
);

Expand All @@ -112,7 +113,7 @@ public function changeEachItemOfTheCollection()
PHPUnit::assertSame($expected, $result);

array_walk($expected, function (&$item) {
$item['meta'] = ['new' => $item['id']];
$item[Members::META] = ['new' => $item[Members::ID]];
});

$obj = $factory->each(
Expand Down Expand Up @@ -156,8 +157,8 @@ public function mapEachItemOfTheCollection()
array_push(
$expected,
[
'type' => "new{$type}",
'id' => $id
Members::TYPE => "new{$type}",
Members::ID => $id
]
);

Expand All @@ -179,8 +180,8 @@ public function mapEachItemOfTheCollection()
*/
function ($item) {
return [
'type' => 'new' . $item->getResourceType(),
'id' => $item->getId()
Members::TYPE => 'new' . $item->getResourceType(),
Members::ID => $item->getId()
];
}
);
Expand All @@ -199,8 +200,8 @@ public function mapEachItemOfAnEmptyCollection()
$factory = new CollectionFactory;
$factory->map(function ($item) {
return [
'type' => "new{$item->resourceType}",
'id' => $item->id
Members::TYPE => "new{$item->resourceType}",
Members::ID => $item->id
];
});
}
Expand Down
57 changes: 29 additions & 28 deletions tests/Factory/ErrorFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\Assert as PHPUnit;
use VGirol\JsonApiAssert\Assert;
use VGirol\JsonApiConstant\Members;
use VGirol\JsonApiFaker\Exception\JsonApiFakerException;
use VGirol\JsonApiFaker\Factory\ErrorFactory;
use VGirol\JsonApiFaker\Messages;
Expand All @@ -27,20 +28,20 @@ public function setter($key, $value)
public function setterProvider()
{
return [
'status' => [
'status',
'error status' => [
Members::ERROR_STATUS,
'test'
],
'code' => [
'code',
'error code' => [
Members::ERROR_CODE,
'test'
],
'title' => [
'title',
'error title' => [
Members::ERROR_TITLE,
'test'
],
'details' => [
'details',
'error details' => [
Members::ERROR_DETAILS,
'test'
]
];
Expand Down Expand Up @@ -86,14 +87,14 @@ public function errorFactory()
$meta = ['key' => 'value'];

$expected = [
'id' => $id,
'status' => $status,
'meta' => $meta
Members::ID => $id,
Members::ERROR_STATUS => $status,
Members::META => $meta
];

$factory = new ErrorFactory;
$factory->setId($id)
->set('status', $status)
->set(Members::ERROR_STATUS, $status)
->setMeta($meta);

$result = $factory->toArray();
Expand All @@ -109,31 +110,31 @@ public function errorFactoryFull()
$id = 'errorID';
$status = '400';
$meta = ['key' => 'value'];
$links = ['about' => 'url'];
$links = [Members::LINK_ABOUT => 'url'];
$code = 'errorCode';
$title = 'error title';
$details = 'error explanation';
$source = ['parameter' => 'query'];
$source = [Members::ERROR_PARAMETER => 'query'];

$expected = [
'id' => $id,
'status' => $status,
'meta' => $meta,
'links' => $links,
'code' => $code,
'title' => $title,
'details' => $details,
'source' => $source
Members::ID => $id,
Members::ERROR_STATUS => $status,
Members::META => $meta,
Members::LINKS => $links,
Members::ERROR_CODE => $code,
Members::ERROR_TITLE => $title,
Members::ERROR_DETAILS => $details,
Members::ERROR_SOURCE => $source
];

$factory = new ErrorFactory;
$factory->setId($id)
->set('status', $status)
->set(Members::ERROR_STATUS, $status)
->setMeta($meta)
->setLinks($links)
->set('code', $code)
->set('title', $title)
->set('details', $details)
->set(Members::ERROR_CODE, $code)
->set(Members::ERROR_TITLE, $title)
->set(Members::ERROR_DETAILS, $details)
->setSource($source);

$result = $factory->toArray();
Expand Down Expand Up @@ -164,7 +165,7 @@ public function fake()

PHPUnit::assertNotEmpty($factory->getLinks());
PHPUnit::assertEquals(1, count($factory->getLinks()));
PHPUnit::assertEquals('about', array_keys($factory->getLinks())[0]);
PHPUnit::assertEquals(Members::LINK_ABOUT, array_keys($factory->getLinks())[0]);

PHPUnit::assertNotEmpty($factory->getMeta());
PHPUnit::assertEquals(5, count($factory->getMeta()));
Expand All @@ -186,7 +187,7 @@ public function fake()
PHPUnit::assertNotEmpty($factory->source);
PHPUnit::assertIsArray($factory->source);
PHPUnit::assertEquals(1, count($factory->source));
PHPUnit::assertEquals('parameter', array_keys($factory->source)[0]);
PHPUnit::assertEquals(Members::ERROR_PARAMETER, array_keys($factory->source)[0]);

Assert::assertIsValidErrorObject($factory->toArray(), true);
}
Expand Down
17 changes: 9 additions & 8 deletions tests/Factory/HasIdentificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\Assert as PHPUnit;
use VGirol\JsonApiAssert\Assert;
use VGirol\JsonApiConstant\Members;
use VGirol\JsonApiFaker\Factory\BaseFactory;
use VGirol\JsonApiFaker\Factory\HasIdentification;
use VGirol\JsonApiFaker\Tests\TestCase;
Expand Down Expand Up @@ -33,24 +34,24 @@ public function getIdentificationProvider()
'idTest',
'typeTest',
[
'id' => 'idTest',
'type' => 'typeTest'
Members::ID => 'idTest',
Members::TYPE => 'typeTest'
]
],
'no id' => [
null,
'typeTest',
[
'id' => null,
'type' => 'typeTest'
Members::ID => null,
Members::TYPE => 'typeTest'
]
],
'no type' => [
'idTest',
null,
[
'id' => 'idTest',
'type' => null
Members::ID => 'idTest',
Members::TYPE => null
]
]
];
Expand Down Expand Up @@ -81,8 +82,8 @@ public function fakeIdentification()
public function toArray(): ?array
{
return [
'id' => strval($this->id),
'type' => $this->resourceType
Members::ID => strval($this->id),
Members::TYPE => $this->resourceType
];
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Factory/HasIdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\Assert as PHPUnit;
use VGirol\JsonApiAssert\Assert;
use VGirol\JsonApiConstant\Members;
use VGirol\JsonApiFaker\Factory\BaseFactory;
use VGirol\JsonApiFaker\Factory\HasIdentifier;
use VGirol\JsonApiFaker\Testing\CheckMethods;
Expand Down Expand Up @@ -38,7 +39,7 @@ public function fakeIdentifier()
public function toArray(): ?array
{
return [
'id' => strval($this->id)
Members::ID => strval($this->id)
];
}

Expand Down
17 changes: 9 additions & 8 deletions tests/Factory/HasLinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\Assert as PHPUnit;
use VGirol\JsonApiAssert\Assert;
use VGirol\JsonApiConstant\Members;
use VGirol\JsonApiFaker\Factory\BaseFactory;
use VGirol\JsonApiFaker\Factory\HasLinks;
use VGirol\JsonApiFaker\Testing\CheckMethods;
Expand All @@ -23,10 +24,10 @@ public function setLinks()
'setLinks',
'getLinks',
[
'self' => 'test'
Members::LINK_SELF => 'test'
],
[
'related' => 'another test'
Members::LINK_RELATED => 'another test'
]
);
}
Expand All @@ -53,10 +54,10 @@ public function fake()
'addLink',
'getLinks',
[
'self' => 'test'
Members::LINK_SELF => 'test'
],
[
'related' => 'another test'
Members::LINK_RELATED => 'another test'
]
);
}
Expand All @@ -83,8 +84,8 @@ public function fake()
'addLinks',
'getLinks',
[
'self' => 'test',
'related' => 'another test'
Members::LINK_SELF => 'test',
Members::LINK_RELATED => 'another test'
],
[
'other' => 'anything'
Expand Down Expand Up @@ -118,8 +119,8 @@ public function fake()
PHPUnit::assertSame($obj, $mock);
PHPUnit::assertNotEmpty($mock->getLinks());
PHPUnit::assertEquals(1, count($mock->getLinks()));
PHPUnit::assertEquals(['self'], array_keys($mock->getLinks()));
PHPUnit::assertEquals([Members::LINK_SELF], array_keys($mock->getLinks()));

Assert::assertIsValidLinksObject($mock->getLinks(), ['self'], true);
Assert::assertIsValidLinksObject($mock->getLinks(), [Members::LINK_SELF], true);
}
}
Loading

0 comments on commit bb09a70

Please sign in to comment.