Skip to content

Commit b55af95

Browse files
authored
make sure contents of resource arrays are written into envelope (#28)
1 parent 101aa8b commit b55af95

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

src/ResourceBuilder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ public function buildFromData(array $data, $spaceName = null, AssetDecoratorInte
159159
return new Link($metadata, $spaceName);
160160
case 'Array':
161161
$this->addToEnvelope((isset($data['includes'])) ? $data['includes'] : [], $buildFromData);
162-
163-
return new ResourceArray(
162+
$resources = new ResourceArray(
164163
array_map(function ($itemData) use ($buildFromData) {
165164
$envelopeResource = $this->resolveResourceDataToEnvelopeResource($itemData);
166165
if ($envelopeResource) {
@@ -176,6 +175,9 @@ public function buildFromData(array $data, $spaceName = null, AssetDecoratorInte
176175
intval($data['skip']),
177176
$this->envelope
178177
);
178+
$this->envelope->insert($resources);
179+
180+
return $resources;
179181
default:
180182
break;
181183
}

src/ResourceEnvelope.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,30 @@ public function hasContentType($contentTypeId)
116116
return isset($this->contentTypes[$contentTypeId]);
117117
}
118118

119+
/**
120+
* @param ResourceInterface|ResourceArray $resource
121+
* @return $this
122+
*/
123+
public function insert($resource)
124+
{
125+
if ($resource instanceof ResourceArray) {
126+
foreach ($resource as $resourceItem) {
127+
$this->insert($resourceItem);
128+
}
129+
}
130+
if ($resource instanceof EntryInterface) {
131+
return $this->insertEntry($resource);
132+
}
133+
if ($resource instanceof AssetInterface) {
134+
return $this->insertAsset($resource);
135+
}
136+
if ($resource instanceof ContentTypeInterface) {
137+
return $this->insertContentType($resource);
138+
}
139+
140+
return $this;
141+
}
142+
119143
/**
120144
* @param EntryInterface $entry
121145
* @return self

tests/ResourceEnvelopeTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,35 @@ public function testSetAndAccessContentTypes()
9797
$this->assertSame($contentType1, $this->envelope->findContentTypeByName($contentTypeName1));
9898
$this->assertNull($this->envelope->findContentTypeByName('unknown'));
9999
}
100+
101+
public function testSetAndAccessUsingGenericInsertMethod()
102+
{
103+
$contentType1 = m::mock(ContentTypeInterface::class);
104+
$id1 = 'id1';
105+
$contentType1
106+
->shouldReceive('getId')
107+
->andReturn($id1);
108+
$contentTypeName1 = 'name1';
109+
$contentType1
110+
->shouldReceive('getName')
111+
->andReturn($contentTypeName1);
112+
$contentType2 = m::mock(ContentTypeInterface::class);
113+
$id2 = 'id2';
114+
$contentType2
115+
->shouldReceive('getId')
116+
->andReturn($id2);
117+
$contentTypeName2 = 'name2';
118+
$contentType2
119+
->shouldReceive('getName')
120+
->andReturn($contentTypeName2);
121+
$this->envelope->insert($contentType1);
122+
$this->envelope->insert($contentType2);
123+
$this->assertSame($contentType2, $this->envelope->findContentType($id2));
124+
$this->assertTrue($this->envelope->hasContentType($id2));
125+
$this->assertNull($this->envelope->findContentType('unknown'));
126+
$this->assertFalse($this->envelope->hasContentType('unknown'));
127+
$this->assertEquals(2, $this->envelope->getContentTypeCount());
128+
$this->assertSame($contentType1, $this->envelope->findContentTypeByName($contentTypeName1));
129+
$this->assertNull($this->envelope->findContentTypeByName('unknown'));
130+
}
100131
}

0 commit comments

Comments
 (0)