Skip to content

Commit

Permalink
Merge pull request #49 from Art4/47-deprecate-converting-int-to-strin…
Browse files Browse the repository at this point in the history
…g-in-type-or-id

deprecate converting int to string in type or
  • Loading branch information
Art4 authored Sep 23, 2019
2 parents 779362e + 867501a commit fcbf130
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 43 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [0.10.1] - 2019-09-24

### Deprecated

- 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.

## [0.10] - 2018-11-07

### Added
Expand Down
16 changes: 12 additions & 4 deletions src/V1/ResourceIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,20 @@ protected function parse($object)
throw new ValidationException('A resource object MUST contain an id');
}

if (is_object($object->type) or is_array($object->type)) {
throw new ValidationException('Resource type cannot be an array or object');
if (! is_string($object->type)) {
if (is_object($object->type) or is_array($object->type)) {
throw new ValidationException('Resource type cannot be an array or object');
}

@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);
}

if (is_object($object->id) or is_array($object->id)) {
throw new ValidationException('Resource Id cannot be an array or object');
if (! is_string($object->id)) {
if (is_object($object->id) or is_array($object->id)) {
throw new ValidationException('Resource Id cannot be an array or object');
}

@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);
}

$this->set('type', strval($object->type));
Expand Down
16 changes: 12 additions & 4 deletions src/V1/ResourceItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ protected function parse($object)
throw new ValidationException('A resource object MUST contain a type');
}

if (is_object($object->type) or is_array($object->type)) {
throw new ValidationException('Resource type cannot be an array or object');
if (! is_string($object->type)) {
if (is_object($object->type) or is_array($object->type)) {
throw new ValidationException('Resource type cannot be an array or object');
}

@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);
}

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

if (is_object($object->id) or is_array($object->id)) {
throw new ValidationException('Resource id cannot be an array or object');
if (! is_string($object->id)) {
if (is_object($object->id) or is_array($object->id)) {
throw new ValidationException('Resource id cannot be an array or object');
}

@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);
}

$this->set('id', strval($object->id));
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixtures/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public function make($name, array $args = [])
{
return $this->testcase
->getMockBuilder('Art4\JsonApiClient\\' . $name . 'Interface') // Mock only the interfaces
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->getMock();
}
}
7 changes: 6 additions & 1 deletion tests/Fixtures/V1Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function __construct($testcase)
*/
public function make($name, array $args = [])
{
return $this->testcase->getMockBuilder(AccessableElement::class)->getMock();
return $this->testcase->getMockBuilder(AccessableElement::class)
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->getMock();
}
}
15 changes: 15 additions & 0 deletions tests/Integration/ParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,4 +650,19 @@ public function testParseCreateShortResourceWithoutId()
// Test full array
$this->assertEquals(json_decode($string, true), $document->asArray(true));
}

/**
* @test
*/
public function testParseIdAsInteger()
{
$string = $this->getJsonString('16_type_and_id_as_integer.json');
$document = Helper::parseResponseBody($string);

$this->assertInstanceOf('Art4\JsonApiClient\Document', $document);
$this->assertSame(['data'], $document->getKeys());

// Test full array
$this->assertEquals(json_decode($string, true), $document->asArray(true));
}
}
3 changes: 1 addition & 2 deletions tests/Unit/DocumentLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public function setUp()
$this->manager = $this->buildManagerMock();

// Mock parent
$this->parent = $this->getMockBuilder('Art4\JsonApiClient\AccessInterface')
->getMock();
$this->parent = $this->createMock('Art4\JsonApiClient\AccessInterface');

$this->parent->expects($this->any())
->method('has')
Expand Down
3 changes: 1 addition & 2 deletions tests/Unit/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public function setUp()
$this->manager = $this->buildManagerMock();

// Mock parent link
$this->parent_link = $this->getMockBuilder('Art4\JsonApiClient\LinkInterface')
->getMock();
$this->parent_link = $this->createMock('Art4\JsonApiClient\LinkInterface');
}

/**
Expand Down
18 changes: 6 additions & 12 deletions tests/Unit/RelationshipCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public function testCreateWithObject()
$object->author = new \stdClass();
$object->author->meta = new \stdClass();

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

$item->method('has')
->with($this->equalTo('attributes.author'))
Expand Down Expand Up @@ -80,8 +79,7 @@ public function testCreateWithObject()
*/
public function testCreateWithEmptyObject()
{
$item = $this->getMockBuilder('Art4\JsonApiClient\ResourceItemInterface')
->getMock();
$item = $this->createMock('Art4\JsonApiClient\ResourceItemInterface');

$item->method('has')
->with($this->equalTo('attributes'))
Expand All @@ -102,8 +100,7 @@ public function testCreateWithEmptyObject()
*/
public function testCreateWithTypePropertyThrowsException()
{
$item = $this->getMockBuilder('Art4\JsonApiClient\ResourceItemInterface')
->getMock();
$item = $this->createMock('Art4\JsonApiClient\ResourceItemInterface');

$item->expects($this->any())
->method('has')
Expand All @@ -130,8 +127,7 @@ public function testCreateWithTypePropertyThrowsException()
*/
public function testCreateWithIdPropertyThrowsException()
{
$item = $this->getMockBuilder('Art4\JsonApiClient\ResourceItemInterface')
->getMock();
$item = $this->createMock('Art4\JsonApiClient\ResourceItemInterface');

$item->expects($this->any())
->method('has')
Expand All @@ -158,8 +154,7 @@ public function testCreateWithIdPropertyThrowsException()
*/
public function testCreateWithAuthorInRelationshipsAndAttributesThrowsException()
{
$item = $this->getMockBuilder('Art4\JsonApiClient\ResourceItemInterface')
->getMock();
$item = $this->createMock('Art4\JsonApiClient\ResourceItemInterface');

$item->expects($this->any())
->method('has')
Expand All @@ -186,8 +181,7 @@ public function testCreateWithAuthorInRelationshipsAndAttributesThrowsException(
*/
public function testCreateWithoutObjectThrowsException($input)
{
$item = $this->getMockBuilder('Art4\JsonApiClient\ResourceItemInterface')
->getMock();
$item = $this->createMock('Art4\JsonApiClient\ResourceItemInterface');

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

Expand Down
15 changes: 5 additions & 10 deletions tests/Unit/RelationshipLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ public function setUp()
$this->manager = $this->buildManagerMock();

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

// Mock Relationship with data
$this->relationship = $this->getMockBuilder('Art4\JsonApiClient\RelationshipInterface')
->getMock();
$this->relationship = $this->createMock('Art4\JsonApiClient\RelationshipInterface');

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

// Mock Relationship
$relationship = $this->getMockBuilder('Art4\JsonApiClient\RelationshipInterface')
->getMock();
$relationship = $this->createMock('Art4\JsonApiClient\RelationshipInterface');

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

// Mock Relationship
$relationship = $this->getMockBuilder('Art4\JsonApiClient\RelationshipInterface')
->getMock();
$relationship = $this->createMock('Art4\JsonApiClient\RelationshipInterface');

$relationship->expects($this->any())
->method('has')
->with($this->equalTo('data'))
->will($this->returnValue(true));

// Mock identifier item
$data = $this->getMockBuilder('Art4\JsonApiClient\ResourceIdentifierInterface')
->getMock();
$data = $this->createMock('Art4\JsonApiClient\ResourceIdentifierInterface');

$relationship->expects($this->any())
->method('get')
Expand Down
3 changes: 1 addition & 2 deletions tests/Unit/ResourceItemLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public function setUp()
$this->manager = $this->buildManagerMock();

// Mock parent
$this->parent = $this->getMockBuilder('Art4\JsonApiClient\AccessInterface')
->getMock();
$this->parent = $this->createMock('Art4\JsonApiClient\AccessInterface');
}

/**
Expand Down
9 changes: 3 additions & 6 deletions tests/Unit/Utils/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public function testCreateReturnsSelf()
*/
public function testCreateWithConstructorReturnsSelf()
{
$factory = $this->getMockBuilder('Art4\JsonApiClient\Utils\FactoryInterface')
->getMock();
$factory = $this->createMock('Art4\JsonApiClient\Utils\FactoryInterface');

$manager = new Manager($factory);

Expand All @@ -55,8 +54,7 @@ public function testCreateWithConstructorReturnsSelf()
*/
public function testSetFactoryReturnsSelf()
{
$factory = $this->getMockBuilder('Art4\JsonApiClient\Utils\FactoryInterface')
->getMock();
$factory = $this->createMock('Art4\JsonApiClient\Utils\FactoryInterface');

$manager = new Manager;

Expand All @@ -68,8 +66,7 @@ public function testSetFactoryReturnsSelf()
*/
public function testGetFactoryReturnsFactoryInterface()
{
$factory = $this->getMockBuilder('Art4\JsonApiClient\Utils\FactoryInterface')
->getMock();
$factory = $this->createMock('Art4\JsonApiClient\Utils\FactoryInterface');

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

Expand Down
10 changes: 10 additions & 0 deletions tests/files/16_type_and_id_as_integer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"data": {
"type": "posts",
"id": 1,
"attributes": {
"title": "Post 1 title",
"content": "Post 1 content"
}
}
}

0 comments on commit fcbf130

Please sign in to comment.