Skip to content

Commit

Permalink
Adding array cleaning to Error::toArray
Browse files Browse the repository at this point in the history
  • Loading branch information
elb98rm committed Mar 27, 2021
1 parent a67edcc commit 65a7c80
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 12 deletions.
42 changes: 33 additions & 9 deletions src/Models/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,39 @@ public function __construct(
*/
public function toArray(): array
{
return [
'id' => $this->getId(),
'links' => $this->getLinks(),
'status' => $this->getStatus(),
'code' => $this->getCode(),
'title' => $this->getTitle(),
'detail' => $this->getDetail(),
'source' => $this->getSource(),
];
// only include set values:

$return = [];

if($this->getId()) {
$return['id'] = $this->getId();
}

if($this->getLinks()) {
$return['links'] = $this->getLinks();
}

if($this->getStatus()) {
$return['status'] = $this->getStatus();
}

if($this->getCode()) {
$return['code'] = $this->getCode();
}

if($this->getTitle()) {
$return['title'] = $this->getTitle();
}

if($this->getDetail()) {
$return['detail'] = $this->getDetail();
}

if($this->getSource()) {
$return['source'] = $this->getSource();
}

return $return;
}

}
54 changes: 51 additions & 3 deletions tests/Unit/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
*/
class ErrorTest extends TestCase
{

use AccessorTesterTrait;

// Accessors
Expand Down Expand Up @@ -80,9 +79,8 @@ public function testBasicAccessors()
$this->assertEquals($source, $error->getSource());
}


/**
* Test Error constructor.
* Test Error::toArray()
*
* @return void
* @throws TestingToolsException
Expand Down Expand Up @@ -113,4 +111,54 @@ public function testToArray()

$this->assertEquals($error->toArray(), $array);
}

/**
* Test Error::toArray
*
* @return void
*/
public function testToArrayNullProperties()
{
$generator = new Generator();

$array = [
'id' => $generator->randomString(),
'links' => new Links(),
'status' => $generator->randomString(),
'code' => $generator->randomString(),
'title' => $generator->randomString(),
'detail' => $generator->randomString(),
'source' => new StdClass,
];

$error = new Error(
$array['id'],
$array['links'],
$array['status'],
$array['code'],
$array['title'],
$array['detail'],
$array['source']
);

// should do nothing
$this->assertEquals($error->toArray(), $array);

// remove one by one, then check
$error->setId(null);
$this->assertArrayNotHasKey('id', $error->toArray());
$error->setLinks(null);
$this->assertArrayNotHasKey('links', $error->toArray());
$error->setStatus(null);
$this->assertArrayNotHasKey('status', $error->toArray());
$error->setCode(null);
$this->assertArrayNotHasKey('code', $error->toArray());
$error->setTitle(null);
$this->assertArrayNotHasKey('title', $error->toArray());
$error->setDetail(null);
$this->assertArrayNotHasKey('detail', $error->toArray());
$error->setSource(null);
$this->assertArrayNotHasKey('source', $error->toArray());

}
}

0 comments on commit 65a7c80

Please sign in to comment.