Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Add FC for new properties in Error object
  • Loading branch information
Art4 committed Jul 19, 2024
commit 4be2ed1c058bc43c43c6465d283b310b214fd9e5
118 changes: 54 additions & 64 deletions src/V1/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,71 +33,61 @@ protected function parse(mixed $object): void
);
}

if (property_exists($object, 'id')) {
if (!is_string($object->id)) {
throw new ValidationException(
'property "id" has to be a string, "' .
gettype($object->id) . '" given.'
);
foreach (get_object_vars($object) as $key => $value) {
if ($key === 'id') {
if (!is_string($object->id)) {
throw new ValidationException(
'property "id" has to be a string, "' .
gettype($object->id) . '" given.'
);
}

$this->set('id', strval($object->id));
} elseif ($key === 'links') {
$this->set('links', $this->create('ErrorLink', $object->links));
} elseif ($key === 'status') {
if (!is_string($object->status)) {
throw new ValidationException(
'property "status" has to be a string, "' .
gettype($object->status) . '" given.'
);
}

$this->set('status', strval($object->status));
} elseif ($key === 'code') {
if (!is_string($object->code)) {
throw new ValidationException(
'property "code" has to be a string, "' .
gettype($object->code) . '" given.'
);
}

$this->set('code', strval($object->code));
} elseif ($key === 'title') {
if (!is_string($object->title)) {
throw new ValidationException(
'property "title" has to be a string, "' .
gettype($object->title) . '" given.'
);
}

$this->set('title', strval($object->title));
} elseif ($key === 'detail') {
if (!is_string($object->detail)) {
throw new ValidationException(
'property "detail" has to be a string, "' .
gettype($object->detail) . '" given.'
);
}

$this->set('detail', strval($object->detail));
} elseif ($key === 'source') {
$this->set('source', $this->create('ErrorSource', $object->source));
} elseif ($key === 'meta') {
$this->set('meta', $this->create('Meta', $object->meta));
} else {
$this->set($key, $value);
}

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

if (property_exists($object, 'links')) {
$this->set('links', $this->create('ErrorLink', $object->links));
}

if (property_exists($object, 'status')) {
if (!is_string($object->status)) {
throw new ValidationException(
'property "status" has to be a string, "' .
gettype($object->status) . '" given.'
);
}

$this->set('status', strval($object->status));
}

if (property_exists($object, 'code')) {
if (!is_string($object->code)) {
throw new ValidationException(
'property "code" has to be a string, "' .
gettype($object->code) . '" given.'
);
}

$this->set('code', strval($object->code));
}

if (property_exists($object, 'title')) {
if (!is_string($object->title)) {
throw new ValidationException(
'property "title" has to be a string, "' .
gettype($object->title) . '" given.'
);
}

$this->set('title', strval($object->title));
}

if (property_exists($object, 'detail')) {
if (!is_string($object->detail)) {
throw new ValidationException(
'property "detail" has to be a string, "' .
gettype($object->detail) . '" given.'
);
}

$this->set('detail', strval($object->detail));
}

if (property_exists($object, 'source')) {
$this->set('source', $this->create('ErrorSource', $object->source));
}

if (property_exists($object, 'meta')) {
$this->set('meta', $this->create('Meta', $object->meta));
}
}

Expand Down
18 changes: 12 additions & 6 deletions tests/Unit/V1/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,35 @@ public function testCreateWithObjectReturnsSelf(): void
$object->detail = 'detail';
$object->source = new \stdClass();
$object->meta = new \stdClass();
$object->fc = 'test property for forward compatability';

$error = new Error($object, $this->manager, $this->parent);

$this->assertInstanceOf(Error::class, $error);
$this->assertInstanceOf(Accessable::class, $error);
$this->assertSame($error->getKeys(), ['id', 'links', 'status', 'code', 'title', 'detail', 'source', 'meta']);
$this->assertSame(
['id', 'links', 'status', 'code', 'title', 'detail', 'source', 'meta', 'fc'],
$error->getKeys(),
);

$this->assertTrue($error->has('id'));
$this->assertSame($error->get('id'), 'id');
$this->assertSame('id', $error->get('id'));
$this->assertTrue($error->has('links'));
$this->assertInstanceOf(Accessable::class, $error->get('links'));
$this->assertTrue($error->has('status'));
$this->assertSame($error->get('status'), 'status');
$this->assertSame('status', $error->get('status'));
$this->assertTrue($error->has('code'));
$this->assertSame($error->get('code'), 'code');
$this->assertSame('code', $error->get('code'));
$this->assertTrue($error->has('title'));
$this->assertSame($error->get('title'), 'title');
$this->assertSame('title', $error->get('title'));
$this->assertTrue($error->has('detail'));
$this->assertSame($error->get('detail'), 'detail');
$this->assertSame('detail', $error->get('detail'));
$this->assertTrue($error->has('source'));
$this->assertInstanceOf(Accessable::class, $error->get('source'));
$this->assertTrue($error->has('meta'));
$this->assertInstanceOf(Accessable::class, $error->get('meta'));
$this->assertTrue($error->has('fc'));
$this->assertSame('test property for forward compatability', $error->get('fc'));

// test get() with not existing key throws an exception
$this->assertFalse($error->has('something'));
Expand Down