Skip to content

Commit cc12a95

Browse files
authored
Merge pull request #14 from cfxmarkets/v1.4.x
Releasing v1.4.4
2 parents 2fbaba3 + 502dba9 commit cc12a95

3 files changed

Lines changed: 68 additions & 6 deletions

File tree

src/AbstractResource.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ public static function fromResource(ResourceInterface $src, DatasourceInterface
226226
$changes['relationships'][$name] = $src->changes['relationships'][$name];
227227
}
228228
}
229+
230+
if (array_key_exists($name, $src->initializedRelationships)) {
231+
$targ->initializedRelationships[$name] = $name;
232+
}
229233
}
230234

231235
$targ->internalUpdateFromData($data);
@@ -712,7 +716,16 @@ public function initialize() {
712716
);
713717
}
714718
*/
715-
$this->datasource->initializeResource($this);
719+
try {
720+
$this->datasource->initializeResource($this);
721+
$this->setInitialState();
722+
} catch (\CFX\Persistence\ResourceNotFoundException $e) {
723+
throw new \CFX\CorruptDataException(
724+
"Programmer: Your system has corrupt data. You've attempted to initialize a resource of ".
725+
"type `{$this->getResourceType()}` with id `{$this->getId()}`, but that resources doesn't exist ".
726+
"in the specified database."
727+
);
728+
}
716729
}
717730
return $this;
718731
}
@@ -723,6 +736,7 @@ public function initialize() {
723736
*/
724737
public function refresh()
725738
{
739+
$this->initializedRelationships = [];
726740
$this->initialized = false;
727741
return $this->initialize();
728742
}

src/Error.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,25 @@ public function __construct(array $props) {
2626
}
2727

2828
if ($k == 'links') {
29-
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.");
29+
if (!($v instanceof Collection)) {
30+
throw new \InvalidArgumentException(
31+
"Value passed with key `links` must be a JsonApi Collection containing an `about` ".
32+
"key with a link to more information about this error."
33+
);
34+
}
3035
if (count($v) != 1 ||
31-
!array_key_exists('about', $v) ||
32-
(!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.");
36+
!isset($v['about']) ||
37+
(!is_string($v['about']) && !($v['about'] instanceof Link))
38+
) {
39+
throw new \InvalidArgumentException(
40+
"The Collection passed as `links` must contain exactly one item, `about`, which should ".
41+
"be a string or a Link object."
42+
);
43+
}
44+
45+
if (is_string($v['about'])) {
46+
$v['about'] = new Link(['name' => 'about', 'href' => $v['about']]);
47+
}
3348
} elseif ($k == 'status') {
3449
if (!is_int($v) || $v < 100 || $v >= 600) throw new \InvalidArgumentException("Value for `status` must be an integery between 100 and 599");
3550
}

tests/ErrorTest.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,45 @@ public function testErrorShouldValidateStatus() {
3838
}
3939
}
4040

41-
public function testErrorShouldValidateLinks() {
42-
$this->markTestIncomplete();
41+
public function testErrorShouldValidateLinksOnInstantiate() {
42+
$e = new Error([
43+
'status' => 500,
44+
'title' => "Server Error",
45+
"detail" => "There was an error",
46+
"links" => new \CFX\JsonApi\Collection([
47+
"about" => "https://test.com/about/error"
48+
]),
49+
]);
50+
$this->assertInstanceOf("\\CFX\\JsonApi\\CollectionInterface", $e->getLinks());
51+
$this->assertEquals(1, count($e->getLinks()));
52+
$this->assertInstanceOf("\\CFX\\JsonApi\\LinkInterface", $e->getLinks()['about']);
53+
$this->assertEquals("about", $e->getLinks()['about']->getName());
54+
$this->assertEquals("https://test.com/about/error", $e->getLinks()['about']->getHref());
4355
}
4456

57+
/**
58+
* Per the [jsonapi spec](http://jsonapi.org/format/#errors), errors have an optional `source`
59+
* member with a certain specification. This test should validate that the Error class enforces
60+
* this specification.
61+
*/
4562
public function testErrorShouldValidateSource() {
4663
$this->markTestIncomplete();
64+
// TODO: Instantiate error with source property
65+
// Should have 'pointer' property and 'parameter'
66+
67+
$this->assertTrue(is_array($e->getSource()));
68+
$this->assertContains('pointer', array_keys($e->getSource()));
69+
$this->assertRegExp("#(/[^/]+)+#", $e->getSource()['pointer']);
70+
$this->assertContains('parameter', array_keys($e->getSource()));
71+
$this->assertRegExp("#[^/&= ]+#", $e->getSource()['parameter']);
72+
73+
74+
// TODO: Instantiate error with source property and NOT pointer or parameter
75+
76+
$this->assertInstanceOf("\\CFX\\JsonApi\\ErrorInterface", $e);
77+
$this->assertNotContains('pointer', array_keys($e->getSource()));
78+
$this->assertNotContains('parameter', array_keys($e->getSource()));
79+
$this->assertTrue(count($e->getSource()) > 0);
4780
}
4881

4982
public function testErrorShouldValidateMeta() {

0 commit comments

Comments
 (0)