Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 15 additions & 1 deletion src/AbstractResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ public static function fromResource(ResourceInterface $src, DatasourceInterface
$changes['relationships'][$name] = $src->changes['relationships'][$name];
}
}

if (array_key_exists($name, $src->initializedRelationships)) {
$targ->initializedRelationships[$name] = $name;
}
}

$targ->internalUpdateFromData($data);
Expand Down Expand Up @@ -712,7 +716,16 @@ public function initialize() {
);
}
*/
$this->datasource->initializeResource($this);
try {
$this->datasource->initializeResource($this);
$this->setInitialState();
} catch (\CFX\Persistence\ResourceNotFoundException $e) {
throw new \CFX\CorruptDataException(
"Programmer: Your system has corrupt data. You've attempted to initialize a resource of ".
"type `{$this->getResourceType()}` with id `{$this->getId()}`, but that resources doesn't exist ".
"in the specified database."
);
}
}
return $this;
}
Expand All @@ -723,6 +736,7 @@ public function initialize() {
*/
public function refresh()
{
$this->initializedRelationships = [];
$this->initialized = false;
return $this->initialize();
}
Expand Down
21 changes: 18 additions & 3 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,25 @@ public function __construct(array $props) {
}

if ($k == 'links') {
if (!($v instanceof Collection)) throw new \InvalidArgumentException("Value passed with key `links` must be a JsonApi Collection containing an `about` key with a link to more information about this error.");
if (!($v instanceof Collection)) {
throw new \InvalidArgumentException(
"Value passed with key `links` must be a JsonApi Collection containing an `about` ".
"key with a link to more information about this error."
);
}
if (count($v) != 1 ||
!array_key_exists('about', $v) ||
(!is_string($v['about']) && !($v['about'] instanceof Link))) throw new \InvalidArgumentException("The Collection passed as `links` must contain exactly one item, `about`, which should be a string or a Link object.");
!isset($v['about']) ||
(!is_string($v['about']) && !($v['about'] instanceof Link))
) {
throw new \InvalidArgumentException(
"The Collection passed as `links` must contain exactly one item, `about`, which should ".
"be a string or a Link object."
);
}

if (is_string($v['about'])) {
$v['about'] = new Link(['name' => 'about', 'href' => $v['about']]);
}
} elseif ($k == 'status') {
if (!is_int($v) || $v < 100 || $v >= 600) throw new \InvalidArgumentException("Value for `status` must be an integery between 100 and 599");
}
Expand Down
37 changes: 35 additions & 2 deletions tests/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,45 @@ public function testErrorShouldValidateStatus() {
}
}

public function testErrorShouldValidateLinks() {
$this->markTestIncomplete();
public function testErrorShouldValidateLinksOnInstantiate() {
$e = new Error([
'status' => 500,
'title' => "Server Error",
"detail" => "There was an error",
"links" => new \CFX\JsonApi\Collection([
"about" => "https://test.com/about/error"
]),
]);
$this->assertInstanceOf("\\CFX\\JsonApi\\CollectionInterface", $e->getLinks());
$this->assertEquals(1, count($e->getLinks()));
$this->assertInstanceOf("\\CFX\\JsonApi\\LinkInterface", $e->getLinks()['about']);
$this->assertEquals("about", $e->getLinks()['about']->getName());
$this->assertEquals("https://test.com/about/error", $e->getLinks()['about']->getHref());
}

/**
* Per the [jsonapi spec](http://jsonapi.org/format/#errors), errors have an optional `source`
* member with a certain specification. This test should validate that the Error class enforces
* this specification.
*/
public function testErrorShouldValidateSource() {
$this->markTestIncomplete();
// TODO: Instantiate error with source property
// Should have 'pointer' property and 'parameter'

$this->assertTrue(is_array($e->getSource()));
$this->assertContains('pointer', array_keys($e->getSource()));
$this->assertRegExp("#(/[^/]+)+#", $e->getSource()['pointer']);
$this->assertContains('parameter', array_keys($e->getSource()));
$this->assertRegExp("#[^/&= ]+#", $e->getSource()['parameter']);


// TODO: Instantiate error with source property and NOT pointer or parameter

$this->assertInstanceOf("\\CFX\\JsonApi\\ErrorInterface", $e);
$this->assertNotContains('pointer', array_keys($e->getSource()));
$this->assertNotContains('parameter', array_keys($e->getSource()));
$this->assertTrue(count($e->getSource()) > 0);
}

public function testErrorShouldValidateMeta() {
Expand Down