Skip to content

Test refactoring #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
}
},
"scripts": {
"test": "php-cs-fixer fix -v --dry-run --ansi && phpunit --colors=always"
"test": "php-cs-fixer fix -v --dry-run --ansi && phpunit --colors=always --coverage-text"
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php
declare(strict_types=1);

namespace JsonApiPhp\JsonApi\Document\Resource\Relationship;
namespace JsonApiPhp\JsonApi\Document\Resource;

use JsonApiPhp\JsonApi\Document\Link\LinkInterface;
use JsonApiPhp\JsonApi\Document\LinksTrait;
use JsonApiPhp\JsonApi\Document\Meta;
use JsonApiPhp\JsonApi\Document\MetaTrait;
use JsonApiPhp\JsonApi\Document\Resource\Linkage\LinkageInterface;
use JsonApiPhp\JsonApi\Document\Resource\ResourceObject;

final class Relationship implements \JsonSerializable
{
Expand Down
11 changes: 11 additions & 0 deletions src/Document/Resource/ResourceIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@

class ResourceIdentifier implements \JsonSerializable
{
/**
* @var string
*/
private $type;

/**
* @var string
*/
private $id;

/**
* @var Meta
*/
private $meta;

public function __construct(string $type, string $id, Meta $meta = null)
Expand Down
14 changes: 8 additions & 6 deletions src/Document/Resource/ResourceObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use JsonApiPhp\JsonApi\Document\LinksTrait;
use JsonApiPhp\JsonApi\Document\Meta;
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Relationship;

class ResourceObject implements \JsonSerializable
{
Expand Down Expand Up @@ -35,24 +34,27 @@ public function setMeta(Meta $meta)
public function setAttribute(string $name, $value)
{
if ($this->isReservedName($name)) {
throw new \InvalidArgumentException('Can not use a reserved name');
throw new \InvalidArgumentException("Can not use a reserved name '$name'");
}
if (!$this->isValidMemberName($name)) {
throw new \OutOfBoundsException('Not a valid attribute name');
throw new \OutOfBoundsException("Not a valid attribute name '$name'");
}
if (isset($this->relationships[$name])) {
throw new \LogicException("Field $name already exists in relationships");
throw new \LogicException("Field '$name' already exists in relationships");
}
$this->attributes[$name] = $value;
}

public function setRelationship(string $name, Relationship $relationship)
{
if ($this->isReservedName($name)) {
throw new \InvalidArgumentException('Can not use a reserved name');
throw new \InvalidArgumentException("Can not use a reserved name '$name'");
}
if (!$this->isValidMemberName($name)) {
throw new \OutOfBoundsException("Not a valid attribute name '$name'");
}
if (isset($this->attributes[$name])) {
throw new \LogicException("Field $name already exists in attributes");
throw new \LogicException("Field '$name' already exists in attributes");
}
$this->relationships[$name] = $relationship;
}
Expand Down
18 changes: 15 additions & 3 deletions test/Document/CompoundDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use JsonApiPhp\JsonApi\Document\Meta;
use JsonApiPhp\JsonApi\Document\Resource\Linkage\MultiLinkage;
use JsonApiPhp\JsonApi\Document\Resource\Linkage\SingleLinkage;
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Relationship;
use JsonApiPhp\JsonApi\Document\Resource\Relationship;
use JsonApiPhp\JsonApi\Document\Resource\ResourceIdentifier;
use JsonApiPhp\JsonApi\Document\Resource\ResourceObject;
use JsonApiPhp\JsonApi\Test\BaseTestCase;
Expand Down Expand Up @@ -159,14 +159,26 @@ public function testOfficialDocsExample()
/**
* @expectedException \LogicException
* @expectedExceptionMessage Full linkage is required for apples:1
* @dataProvider documentsWithoutFullLinkage
* @param Document $doc
*/
public function testFullLinkageIsRequired()
public function testFullLinkageIsRequired(Document $doc)
{
$doc = Document::nullDocument();
$doc->setIncluded(new ResourceObject('apples', '1'));
json_encode($doc);
}

public function documentsWithoutFullLinkage(): array
{
return [
[Document::nullDocument()],
[Document::fromIdentifier(new ResourceIdentifier('oranges', '1'))],
[Document::fromIdentifiers(new ResourceIdentifier('oranges', '1'), new ResourceIdentifier('oranges', '2'))],
[Document::fromResource(new ResourceObject('oranges', '1'))],
[Document::fromResources(new ResourceObject('oranges', '1'), new ResourceObject('oranges', '1'))],
];
}

/**
* A compound document must be explicitly marked as sparse. In this case full linkage is not required.
*/
Expand Down
4 changes: 2 additions & 2 deletions test/Document/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use JsonApiPhp\JsonApi\Document\Resource\ResourceObject;
use JsonApiPhp\JsonApi\Test\BaseTestCase;
use JsonApiPhp\JsonApi\Test\Document\Resource\Relationship\LinkageTest;
use JsonApiPhp\JsonApi\Test\Document\Resource\ResourceTest;
use JsonApiPhp\JsonApi\Test\Document\Resource\ResourceObjectTest;

/**
* This is the JSON document's top level object
Expand Down Expand Up @@ -68,7 +68,7 @@ public function testDocumentMayContainJustErrors()

/**
* A valid document may contain just a primary data object.
* The primary data object is represented by ResourceInterface (@see ResourceTest for details).
* The primary data object is represented by ResourceInterface (@see ResourceObjectTest for details).
* Here is how a document can be created from different kinds of resources:
* - null resource
* - resource identifier
Expand Down
8 changes: 4 additions & 4 deletions test/Document/Resource/Relationship/RelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use JsonApiPhp\JsonApi\Document\Link\Link;
use JsonApiPhp\JsonApi\Document\Meta;
use JsonApiPhp\JsonApi\Document\Resource\Linkage\NullLinkage;
use JsonApiPhp\JsonApi\Document\Resource\Relationship;
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Linkage;
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Relationship;
use JsonApiPhp\JsonApi\Test\BaseTestCase;

/**
Expand Down Expand Up @@ -52,7 +52,7 @@ public function testCanCreateFromSelfLink()
}
}
',
Relationship::fromSelfLink(new Link('http://localhost'))
\JsonApiPhp\JsonApi\Document\Resource\Relationship::fromSelfLink(new Link('http://localhost'))
);
}

Expand All @@ -66,7 +66,7 @@ public function testCanCreateFromRelatedLink()
}
}
',
Relationship::fromRelatedLink(new Link('http://localhost'))
\JsonApiPhp\JsonApi\Document\Resource\Relationship::fromRelatedLink(new Link('http://localhost'))
);
}

Expand All @@ -78,7 +78,7 @@ public function testCanCreateFromLinkage()
"data": null
}
',
Relationship::fromLinkage(new NullLinkage())
\JsonApiPhp\JsonApi\Document\Resource\Relationship::fromLinkage(new NullLinkage())
);
}

Expand Down
60 changes: 3 additions & 57 deletions test/Document/Resource/ResourceFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace JsonApiPhp\JsonApi\Test\Document\Resource;

use JsonApiPhp\JsonApi\Document\Meta;
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Relationship;
use JsonApiPhp\JsonApi\Document\Resource\Relationship;
use JsonApiPhp\JsonApi\Document\Resource\ResourceObject;
use PHPUnit\Framework\TestCase;

Expand All @@ -23,7 +23,7 @@ class ResourceFieldsTest extends TestCase
{
/**
* @expectedException \LogicException
* @expectedExceptionMessage Field foo already exists in attributes
* @expectedExceptionMessage Field 'foo' already exists in attributes
*/
public function testCanNotSetRelationshipIfAttributeExists()
{
Expand All @@ -34,7 +34,7 @@ public function testCanNotSetRelationshipIfAttributeExists()

/**
* @expectedException \LogicException
* @expectedExceptionMessage Field foo already exists in relationships
* @expectedExceptionMessage Field 'foo' already exists in relationships
*/
public function testCanNotSetAttributeIfRelationshipExists()
{
Expand Down Expand Up @@ -67,65 +67,11 @@ public function testRelationshipCanNotHaveReservedNames(string $name)
$res->setRelationship($name, Relationship::fromMeta(Meta::fromArray(['a' => 'b'])));
}

/**
* @param string $name
* @expectedException \OutOfBoundsException
* @expectedExceptionMessage Not a valid attribute name
* @dataProvider invalidAttributeNames
*/
public function testAttributeNameIsNotValid(string $name)
{
$res = new ResourceObject('books', 'abc');
$res->setAttribute($name, 1);
}

/**
* @param string $name
* @dataProvider validAttributeNames
*/
public function testAttributeNameIsValid(string $name)
{
$res = new ResourceObject('books', 'abc');
$res->setAttribute($name, 1);
$this->assertTrue(true);
}

public function reservedAttributeNames(): array
{
return [
['id'],
['type'],
];
}

public function invalidAttributeNames(): array
{
return [
['_abcde'],
['abcd_'],
['abc$EDS'],
['#abcde'],
['abcde('],
['b_'],
['_a'],
['$ab_c-d'],
['-abc'],
];
}

public function validAttributeNames(): array
{
return [
['abcd'],
['abcA4C'],
['abc_d3f45'],
['abd_eca'],
['a'],
['b'],
['ab'],
['a-bc_de'],
['abcéêçèÇ_n'],
['abc 汉字 abc'],
];
}
}
45 changes: 45 additions & 0 deletions test/Document/Resource/ResourceIdentifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

namespace JsonApiPhp\JsonApi\Test\Document\Resource;

use JsonApiPhp\JsonApi\Document\Meta;
use JsonApiPhp\JsonApi\Document\Resource\ResourceIdentifier;
use JsonApiPhp\JsonApi\Test\BaseTestCase;

/**
* Resource Identifiers
*
* @see http://jsonapi.org/format/#document-resource-identifier-objects
*/
class ResourceIdentifierTest extends BaseTestCase
{
public function testResourceIdentifierMustContainTypeAndId()
{
$this->assertEncodesTo(
'
{
"type": "books",
"id": "1"
}
',
new ResourceIdentifier('books', '1')
);
}

public function testResourceIdentifierMayContainMeta()
{
$this->assertEncodesTo(
'
{
"type": "books",
"id": "1",
"meta": {
"foo":"bar"
}
}
',
new ResourceIdentifier('books', '1', Meta::fromArray(['foo' => 'bar']))
);
}
}
Loading