Skip to content

Commit aff1169

Browse files
authored
Test refactoring (#62)
1 parent bb65a43 commit aff1169

13 files changed

+291
-162
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
}
2929
},
3030
"scripts": {
31-
"test": "php-cs-fixer fix -v --dry-run --ansi && phpunit --colors=always"
31+
"test": "php-cs-fixer fix -v --dry-run --ansi && phpunit --colors=always --coverage-text"
3232
}
3333
}

src/Document/Resource/Relationship/Relationship.php src/Document/Resource/Relationship.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace JsonApiPhp\JsonApi\Document\Resource\Relationship;
4+
namespace JsonApiPhp\JsonApi\Document\Resource;
55

66
use JsonApiPhp\JsonApi\Document\Link\LinkInterface;
77
use JsonApiPhp\JsonApi\Document\LinksTrait;
88
use JsonApiPhp\JsonApi\Document\Meta;
99
use JsonApiPhp\JsonApi\Document\MetaTrait;
1010
use JsonApiPhp\JsonApi\Document\Resource\Linkage\LinkageInterface;
11-
use JsonApiPhp\JsonApi\Document\Resource\ResourceObject;
1211

1312
final class Relationship implements \JsonSerializable
1413
{

src/Document/Resource/ResourceIdentifier.php

+11
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,19 @@
77

88
class ResourceIdentifier implements \JsonSerializable
99
{
10+
/**
11+
* @var string
12+
*/
1013
private $type;
14+
15+
/**
16+
* @var string
17+
*/
1118
private $id;
19+
20+
/**
21+
* @var Meta
22+
*/
1223
private $meta;
1324

1425
public function __construct(string $type, string $id, Meta $meta = null)

src/Document/Resource/ResourceObject.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
use JsonApiPhp\JsonApi\Document\LinksTrait;
77
use JsonApiPhp\JsonApi\Document\Meta;
8-
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Relationship;
98

109
class ResourceObject implements \JsonSerializable
1110
{
@@ -35,24 +34,27 @@ public function setMeta(Meta $meta)
3534
public function setAttribute(string $name, $value)
3635
{
3736
if ($this->isReservedName($name)) {
38-
throw new \InvalidArgumentException('Can not use a reserved name');
37+
throw new \InvalidArgumentException("Can not use a reserved name '$name'");
3938
}
4039
if (!$this->isValidMemberName($name)) {
41-
throw new \OutOfBoundsException('Not a valid attribute name');
40+
throw new \OutOfBoundsException("Not a valid attribute name '$name'");
4241
}
4342
if (isset($this->relationships[$name])) {
44-
throw new \LogicException("Field $name already exists in relationships");
43+
throw new \LogicException("Field '$name' already exists in relationships");
4544
}
4645
$this->attributes[$name] = $value;
4746
}
4847

4948
public function setRelationship(string $name, Relationship $relationship)
5049
{
5150
if ($this->isReservedName($name)) {
52-
throw new \InvalidArgumentException('Can not use a reserved name');
51+
throw new \InvalidArgumentException("Can not use a reserved name '$name'");
52+
}
53+
if (!$this->isValidMemberName($name)) {
54+
throw new \OutOfBoundsException("Not a valid attribute name '$name'");
5355
}
5456
if (isset($this->attributes[$name])) {
55-
throw new \LogicException("Field $name already exists in attributes");
57+
throw new \LogicException("Field '$name' already exists in attributes");
5658
}
5759
$this->relationships[$name] = $relationship;
5860
}

test/Document/CompoundDocumentTest.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use JsonApiPhp\JsonApi\Document\Meta;
88
use JsonApiPhp\JsonApi\Document\Resource\Linkage\MultiLinkage;
99
use JsonApiPhp\JsonApi\Document\Resource\Linkage\SingleLinkage;
10-
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Relationship;
10+
use JsonApiPhp\JsonApi\Document\Resource\Relationship;
1111
use JsonApiPhp\JsonApi\Document\Resource\ResourceIdentifier;
1212
use JsonApiPhp\JsonApi\Document\Resource\ResourceObject;
1313
use JsonApiPhp\JsonApi\Test\BaseTestCase;
@@ -159,14 +159,26 @@ public function testOfficialDocsExample()
159159
/**
160160
* @expectedException \LogicException
161161
* @expectedExceptionMessage Full linkage is required for apples:1
162+
* @dataProvider documentsWithoutFullLinkage
163+
* @param Document $doc
162164
*/
163-
public function testFullLinkageIsRequired()
165+
public function testFullLinkageIsRequired(Document $doc)
164166
{
165-
$doc = Document::nullDocument();
166167
$doc->setIncluded(new ResourceObject('apples', '1'));
167168
json_encode($doc);
168169
}
169170

171+
public function documentsWithoutFullLinkage(): array
172+
{
173+
return [
174+
[Document::nullDocument()],
175+
[Document::fromIdentifier(new ResourceIdentifier('oranges', '1'))],
176+
[Document::fromIdentifiers(new ResourceIdentifier('oranges', '1'), new ResourceIdentifier('oranges', '2'))],
177+
[Document::fromResource(new ResourceObject('oranges', '1'))],
178+
[Document::fromResources(new ResourceObject('oranges', '1'), new ResourceObject('oranges', '1'))],
179+
];
180+
}
181+
170182
/**
171183
* A compound document must be explicitly marked as sparse. In this case full linkage is not required.
172184
*/

test/Document/DocumentTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use JsonApiPhp\JsonApi\Document\Resource\ResourceObject;
1212
use JsonApiPhp\JsonApi\Test\BaseTestCase;
1313
use JsonApiPhp\JsonApi\Test\Document\Resource\Relationship\LinkageTest;
14-
use JsonApiPhp\JsonApi\Test\Document\Resource\ResourceTest;
14+
use JsonApiPhp\JsonApi\Test\Document\Resource\ResourceObjectTest;
1515

1616
/**
1717
* This is the JSON document's top level object
@@ -68,7 +68,7 @@ public function testDocumentMayContainJustErrors()
6868

6969
/**
7070
* A valid document may contain just a primary data object.
71-
* The primary data object is represented by ResourceInterface (@see ResourceTest for details).
71+
* The primary data object is represented by ResourceInterface (@see ResourceObjectTest for details).
7272
* Here is how a document can be created from different kinds of resources:
7373
* - null resource
7474
* - resource identifier

test/Document/Resource/Relationship/RelationshipTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
use JsonApiPhp\JsonApi\Document\Link\Link;
77
use JsonApiPhp\JsonApi\Document\Meta;
88
use JsonApiPhp\JsonApi\Document\Resource\Linkage\NullLinkage;
9+
use JsonApiPhp\JsonApi\Document\Resource\Relationship;
910
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Linkage;
10-
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Relationship;
1111
use JsonApiPhp\JsonApi\Test\BaseTestCase;
1212

1313
/**
@@ -52,7 +52,7 @@ public function testCanCreateFromSelfLink()
5252
}
5353
}
5454
',
55-
Relationship::fromSelfLink(new Link('http://localhost'))
55+
\JsonApiPhp\JsonApi\Document\Resource\Relationship::fromSelfLink(new Link('http://localhost'))
5656
);
5757
}
5858

@@ -66,7 +66,7 @@ public function testCanCreateFromRelatedLink()
6666
}
6767
}
6868
',
69-
Relationship::fromRelatedLink(new Link('http://localhost'))
69+
\JsonApiPhp\JsonApi\Document\Resource\Relationship::fromRelatedLink(new Link('http://localhost'))
7070
);
7171
}
7272

@@ -78,7 +78,7 @@ public function testCanCreateFromLinkage()
7878
"data": null
7979
}
8080
',
81-
Relationship::fromLinkage(new NullLinkage())
81+
\JsonApiPhp\JsonApi\Document\Resource\Relationship::fromLinkage(new NullLinkage())
8282
);
8383
}
8484

test/Document/Resource/ResourceFieldsTest.php

+3-57
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace JsonApiPhp\JsonApi\Test\Document\Resource;
55

66
use JsonApiPhp\JsonApi\Document\Meta;
7-
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Relationship;
7+
use JsonApiPhp\JsonApi\Document\Resource\Relationship;
88
use JsonApiPhp\JsonApi\Document\Resource\ResourceObject;
99
use PHPUnit\Framework\TestCase;
1010

@@ -23,7 +23,7 @@ class ResourceFieldsTest extends TestCase
2323
{
2424
/**
2525
* @expectedException \LogicException
26-
* @expectedExceptionMessage Field foo already exists in attributes
26+
* @expectedExceptionMessage Field 'foo' already exists in attributes
2727
*/
2828
public function testCanNotSetRelationshipIfAttributeExists()
2929
{
@@ -34,7 +34,7 @@ public function testCanNotSetRelationshipIfAttributeExists()
3434

3535
/**
3636
* @expectedException \LogicException
37-
* @expectedExceptionMessage Field foo already exists in relationships
37+
* @expectedExceptionMessage Field 'foo' already exists in relationships
3838
*/
3939
public function testCanNotSetAttributeIfRelationshipExists()
4040
{
@@ -67,65 +67,11 @@ public function testRelationshipCanNotHaveReservedNames(string $name)
6767
$res->setRelationship($name, Relationship::fromMeta(Meta::fromArray(['a' => 'b'])));
6868
}
6969

70-
/**
71-
* @param string $name
72-
* @expectedException \OutOfBoundsException
73-
* @expectedExceptionMessage Not a valid attribute name
74-
* @dataProvider invalidAttributeNames
75-
*/
76-
public function testAttributeNameIsNotValid(string $name)
77-
{
78-
$res = new ResourceObject('books', 'abc');
79-
$res->setAttribute($name, 1);
80-
}
81-
82-
/**
83-
* @param string $name
84-
* @dataProvider validAttributeNames
85-
*/
86-
public function testAttributeNameIsValid(string $name)
87-
{
88-
$res = new ResourceObject('books', 'abc');
89-
$res->setAttribute($name, 1);
90-
$this->assertTrue(true);
91-
}
92-
9370
public function reservedAttributeNames(): array
9471
{
9572
return [
9673
['id'],
9774
['type'],
9875
];
9976
}
100-
101-
public function invalidAttributeNames(): array
102-
{
103-
return [
104-
['_abcde'],
105-
['abcd_'],
106-
['abc$EDS'],
107-
['#abcde'],
108-
['abcde('],
109-
['b_'],
110-
['_a'],
111-
['$ab_c-d'],
112-
['-abc'],
113-
];
114-
}
115-
116-
public function validAttributeNames(): array
117-
{
118-
return [
119-
['abcd'],
120-
['abcA4C'],
121-
['abc_d3f45'],
122-
['abd_eca'],
123-
['a'],
124-
['b'],
125-
['ab'],
126-
['a-bc_de'],
127-
['abcéêçèÇ_n'],
128-
['abc 汉字 abc'],
129-
];
130-
}
13177
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace JsonApiPhp\JsonApi\Test\Document\Resource;
5+
6+
use JsonApiPhp\JsonApi\Document\Meta;
7+
use JsonApiPhp\JsonApi\Document\Resource\ResourceIdentifier;
8+
use JsonApiPhp\JsonApi\Test\BaseTestCase;
9+
10+
/**
11+
* Resource Identifiers
12+
*
13+
* @see http://jsonapi.org/format/#document-resource-identifier-objects
14+
*/
15+
class ResourceIdentifierTest extends BaseTestCase
16+
{
17+
public function testResourceIdentifierMustContainTypeAndId()
18+
{
19+
$this->assertEncodesTo(
20+
'
21+
{
22+
"type": "books",
23+
"id": "1"
24+
}
25+
',
26+
new ResourceIdentifier('books', '1')
27+
);
28+
}
29+
30+
public function testResourceIdentifierMayContainMeta()
31+
{
32+
$this->assertEncodesTo(
33+
'
34+
{
35+
"type": "books",
36+
"id": "1",
37+
"meta": {
38+
"foo":"bar"
39+
}
40+
}
41+
',
42+
new ResourceIdentifier('books', '1', Meta::fromArray(['foo' => 'bar']))
43+
);
44+
}
45+
}

0 commit comments

Comments
 (0)