Skip to content

Commit fcbf130

Browse files
authored
Merge pull request #49 from Art4/47-deprecate-converting-int-to-string-in-type-or-id
deprecate converting int to string in type or
2 parents 779362e + 867501a commit fcbf130

13 files changed

+82
-43
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## [0.10.1] - 2019-09-24
9+
10+
### Deprecated
11+
12+
- Providing the fields `type` or `id` in a resource not as a string will be throw an ValidationException in v1.0, provide them always as strings instead.
13+
814
## [0.10] - 2018-11-07
915

1016
### Added

src/V1/ResourceIdentifier.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,20 @@ protected function parse($object)
5151
throw new ValidationException('A resource object MUST contain an id');
5252
}
5353

54-
if (is_object($object->type) or is_array($object->type)) {
55-
throw new ValidationException('Resource type cannot be an array or object');
54+
if (! is_string($object->type)) {
55+
if (is_object($object->type) or is_array($object->type)) {
56+
throw new ValidationException('Resource type cannot be an array or object');
57+
}
58+
59+
@trigger_error('Parsing `type` not as string is deprecated since version 0.10.1 and will be throw an Exception in 1.0. Provide `type` always as string instead', E_USER_DEPRECATED);
5660
}
5761

58-
if (is_object($object->id) or is_array($object->id)) {
59-
throw new ValidationException('Resource Id cannot be an array or object');
62+
if (! is_string($object->id)) {
63+
if (is_object($object->id) or is_array($object->id)) {
64+
throw new ValidationException('Resource Id cannot be an array or object');
65+
}
66+
67+
@trigger_error('Parsing `id` not as string is deprecated since version 0.10.1 and will be throw an Exception in 1.0. Provide `id` always as string instead', E_USER_DEPRECATED);
6068
}
6169

6270
$this->set('type', strval($object->type));

src/V1/ResourceItem.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ protected function parse($object)
4848
throw new ValidationException('A resource object MUST contain a type');
4949
}
5050

51-
if (is_object($object->type) or is_array($object->type)) {
52-
throw new ValidationException('Resource type cannot be an array or object');
51+
if (! is_string($object->type)) {
52+
if (is_object($object->type) or is_array($object->type)) {
53+
throw new ValidationException('Resource type cannot be an array or object');
54+
}
55+
56+
@trigger_error('Parsing `type` not as string is deprecated since version 0.10.1 and will be throw an Exception in 1.0. Provide `type` always as string instead', E_USER_DEPRECATED);
5357
}
5458

5559
$this->set('type', strval($object->type));
@@ -62,8 +66,12 @@ protected function parse($object)
6266
throw new ValidationException('A resource object MUST contain an id');
6367
}
6468

65-
if (is_object($object->id) or is_array($object->id)) {
66-
throw new ValidationException('Resource id cannot be an array or object');
69+
if (! is_string($object->id)) {
70+
if (is_object($object->id) or is_array($object->id)) {
71+
throw new ValidationException('Resource id cannot be an array or object');
72+
}
73+
74+
@trigger_error('Parsing `id` not as string is deprecated since version 0.10.1 and will be throw an Exception in 1.0. Provide `id` always as string instead', E_USER_DEPRECATED);
6775
}
6876

6977
$this->set('id', strval($object->id));

tests/Fixtures/Factory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public function make($name, array $args = [])
3737
{
3838
return $this->testcase
3939
->getMockBuilder('Art4\JsonApiClient\\' . $name . 'Interface') // Mock only the interfaces
40+
->disableOriginalConstructor()
41+
->disableOriginalClone()
42+
->disableArgumentCloning()
43+
->disallowMockingUnknownTypes()
4044
->getMock();
4145
}
4246
}

tests/Fixtures/V1Factory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public function __construct($testcase)
4949
*/
5050
public function make($name, array $args = [])
5151
{
52-
return $this->testcase->getMockBuilder(AccessableElement::class)->getMock();
52+
return $this->testcase->getMockBuilder(AccessableElement::class)
53+
->disableOriginalConstructor()
54+
->disableOriginalClone()
55+
->disableArgumentCloning()
56+
->disallowMockingUnknownTypes()
57+
->getMock();
5358
}
5459
}

tests/Integration/ParsingTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,4 +650,19 @@ public function testParseCreateShortResourceWithoutId()
650650
// Test full array
651651
$this->assertEquals(json_decode($string, true), $document->asArray(true));
652652
}
653+
654+
/**
655+
* @test
656+
*/
657+
public function testParseIdAsInteger()
658+
{
659+
$string = $this->getJsonString('16_type_and_id_as_integer.json');
660+
$document = Helper::parseResponseBody($string);
661+
662+
$this->assertInstanceOf('Art4\JsonApiClient\Document', $document);
663+
$this->assertSame(['data'], $document->getKeys());
664+
665+
// Test full array
666+
$this->assertEquals(json_decode($string, true), $document->asArray(true));
667+
}
653668
}

tests/Unit/DocumentLinkTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public function setUp()
3434
$this->manager = $this->buildManagerMock();
3535

3636
// Mock parent
37-
$this->parent = $this->getMockBuilder('Art4\JsonApiClient\AccessInterface')
38-
->getMock();
37+
$this->parent = $this->createMock('Art4\JsonApiClient\AccessInterface');
3938

4039
$this->parent->expects($this->any())
4140
->method('has')

tests/Unit/LinkTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public function setUp()
3434
$this->manager = $this->buildManagerMock();
3535

3636
// Mock parent link
37-
$this->parent_link = $this->getMockBuilder('Art4\JsonApiClient\LinkInterface')
38-
->getMock();
37+
$this->parent_link = $this->createMock('Art4\JsonApiClient\LinkInterface');
3938
}
4039

4140
/**

tests/Unit/RelationshipCollectionTest.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public function testCreateWithObject()
4343
$object->author = new \stdClass();
4444
$object->author->meta = new \stdClass();
4545

46-
$item = $this->getMockBuilder('Art4\JsonApiClient\ResourceItemInterface')
47-
->getMock();
46+
$item = $this->createMock('Art4\JsonApiClient\ResourceItemInterface');
4847

4948
$item->method('has')
5049
->with($this->equalTo('attributes.author'))
@@ -80,8 +79,7 @@ public function testCreateWithObject()
8079
*/
8180
public function testCreateWithEmptyObject()
8281
{
83-
$item = $this->getMockBuilder('Art4\JsonApiClient\ResourceItemInterface')
84-
->getMock();
82+
$item = $this->createMock('Art4\JsonApiClient\ResourceItemInterface');
8583

8684
$item->method('has')
8785
->with($this->equalTo('attributes'))
@@ -102,8 +100,7 @@ public function testCreateWithEmptyObject()
102100
*/
103101
public function testCreateWithTypePropertyThrowsException()
104102
{
105-
$item = $this->getMockBuilder('Art4\JsonApiClient\ResourceItemInterface')
106-
->getMock();
103+
$item = $this->createMock('Art4\JsonApiClient\ResourceItemInterface');
107104

108105
$item->expects($this->any())
109106
->method('has')
@@ -130,8 +127,7 @@ public function testCreateWithTypePropertyThrowsException()
130127
*/
131128
public function testCreateWithIdPropertyThrowsException()
132129
{
133-
$item = $this->getMockBuilder('Art4\JsonApiClient\ResourceItemInterface')
134-
->getMock();
130+
$item = $this->createMock('Art4\JsonApiClient\ResourceItemInterface');
135131

136132
$item->expects($this->any())
137133
->method('has')
@@ -158,8 +154,7 @@ public function testCreateWithIdPropertyThrowsException()
158154
*/
159155
public function testCreateWithAuthorInRelationshipsAndAttributesThrowsException()
160156
{
161-
$item = $this->getMockBuilder('Art4\JsonApiClient\ResourceItemInterface')
162-
->getMock();
157+
$item = $this->createMock('Art4\JsonApiClient\ResourceItemInterface');
163158

164159
$item->expects($this->any())
165160
->method('has')
@@ -186,8 +181,7 @@ public function testCreateWithAuthorInRelationshipsAndAttributesThrowsException(
186181
*/
187182
public function testCreateWithoutObjectThrowsException($input)
188183
{
189-
$item = $this->getMockBuilder('Art4\JsonApiClient\ResourceItemInterface')
190-
->getMock();
184+
$item = $this->createMock('Art4\JsonApiClient\ResourceItemInterface');
191185

192186
$collection = new RelationshipCollection($this->manager, $item);
193187

tests/Unit/RelationshipLinkTest.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ public function setUp()
3434
$this->manager = $this->buildManagerMock();
3535

3636
// Mock identifier collection
37-
$collection = $this->getMockBuilder('Art4\JsonApiClient\ResourceIdentifierCollectionInterface')
38-
->getMock();
37+
$collection = $this->createMock('Art4\JsonApiClient\ResourceIdentifierCollectionInterface');
3938

4039
// Mock Relationship with data
41-
$this->relationship = $this->getMockBuilder('Art4\JsonApiClient\RelationshipInterface')
42-
->getMock();
40+
$this->relationship = $this->createMock('Art4\JsonApiClient\RelationshipInterface');
4341

4442
$this->relationship->expects($this->any())
4543
->method('has')
@@ -160,8 +158,7 @@ public function testPaginationNotParsedIfRelationshipDataNotExists()
160158
$object->next = new \stdClass();
161159

162160
// Mock Relationship
163-
$relationship = $this->getMockBuilder('Art4\JsonApiClient\RelationshipInterface')
164-
->getMock();
161+
$relationship = $this->createMock('Art4\JsonApiClient\RelationshipInterface');
165162

166163
$relationship->expects($this->any())
167164
->method('has')
@@ -199,17 +196,15 @@ public function testPaginationNotParsedIfRelationshipIdentifierCollectionNotExis
199196
$object->next = new \stdClass();
200197

201198
// Mock Relationship
202-
$relationship = $this->getMockBuilder('Art4\JsonApiClient\RelationshipInterface')
203-
->getMock();
199+
$relationship = $this->createMock('Art4\JsonApiClient\RelationshipInterface');
204200

205201
$relationship->expects($this->any())
206202
->method('has')
207203
->with($this->equalTo('data'))
208204
->will($this->returnValue(true));
209205

210206
// Mock identifier item
211-
$data = $this->getMockBuilder('Art4\JsonApiClient\ResourceIdentifierInterface')
212-
->getMock();
207+
$data = $this->createMock('Art4\JsonApiClient\ResourceIdentifierInterface');
213208

214209
$relationship->expects($this->any())
215210
->method('get')

tests/Unit/ResourceItemLinkTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public function setUp()
3434
$this->manager = $this->buildManagerMock();
3535

3636
// Mock parent
37-
$this->parent = $this->getMockBuilder('Art4\JsonApiClient\AccessInterface')
38-
->getMock();
37+
$this->parent = $this->createMock('Art4\JsonApiClient\AccessInterface');
3938
}
4039

4140
/**

tests/Unit/Utils/ManagerTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public function testCreateReturnsSelf()
3939
*/
4040
public function testCreateWithConstructorReturnsSelf()
4141
{
42-
$factory = $this->getMockBuilder('Art4\JsonApiClient\Utils\FactoryInterface')
43-
->getMock();
42+
$factory = $this->createMock('Art4\JsonApiClient\Utils\FactoryInterface');
4443

4544
$manager = new Manager($factory);
4645

@@ -55,8 +54,7 @@ public function testCreateWithConstructorReturnsSelf()
5554
*/
5655
public function testSetFactoryReturnsSelf()
5756
{
58-
$factory = $this->getMockBuilder('Art4\JsonApiClient\Utils\FactoryInterface')
59-
->getMock();
57+
$factory = $this->createMock('Art4\JsonApiClient\Utils\FactoryInterface');
6058

6159
$manager = new Manager;
6260

@@ -68,8 +66,7 @@ public function testSetFactoryReturnsSelf()
6866
*/
6967
public function testGetFactoryReturnsFactoryInterface()
7068
{
71-
$factory = $this->getMockBuilder('Art4\JsonApiClient\Utils\FactoryInterface')
72-
->getMock();
69+
$factory = $this->createMock('Art4\JsonApiClient\Utils\FactoryInterface');
7370

7471
$manager = (new Manager)->setFactory($factory);
7572

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"data": {
3+
"type": "posts",
4+
"id": 1,
5+
"attributes": {
6+
"title": "Post 1 title",
7+
"content": "Post 1 content"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)