Skip to content

Commit

Permalink
Support multiple parts with the same component ID
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasbestle committed Nov 12, 2022
1 parent aad6d0a commit 5e5c0d1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 33 deletions.
12 changes: 1 addition & 11 deletions src/classes/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,7 @@ public function parts(): Collection
throw new InvalidArgumentException('Invalid part ' . $num);
}

$part = new Part($part);

// double-check uninitialized property before access (normally should not happen);
// the Psalm error is suppressed because Psalm assumes all props to be
// initialized in the `Part` constructor (which `Obj` does not ensure)
/** @psalm-suppress TypeDoesNotContainType */
if (isset($part->componentId) !== true) {
throw new InvalidArgumentException('Part ' . $num . ' does not have a component ID');
}

$parts[$part->componentId] = $part;
$parts[] = new Part($part);
}

return new Collection($parts);
Expand Down
29 changes: 7 additions & 22 deletions tests/Roomle/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ public function testParts()
'componentId' => 'some:component1',
'label' => 'Some component 1'
],
[
'componentId' => 'some:component1',
'label' => 'Some component 1 with different properties'
],
[
'componentId' => 'some:component2',
'label' => 'Some component 2'
Expand All @@ -144,16 +148,16 @@ public function testParts()

$parts = $configuration->parts();

$this->assertSame(2, $parts->count());
$this->assertSame(['some:component1', 'some:component2'], $parts->keys());
$this->assertSame(3, $parts->count());
$this->assertSame(['some:component1', 'some:component1', 'some:component2'], $parts->pluck('componentId'));
$this->assertInstanceOf(Part::class, $parts->first());
$this->assertSame('Some component 1', $parts->first()->label());
}

/**
* @covers ::parts
*/
public function testParts_Invalid1()
public function testParts_Invalid()
{
$this->expectException('Kirby\Exception\InvalidArgumentException');
$this->expectExceptionMessage('Invalid part 0');
Expand All @@ -167,25 +171,6 @@ public function testParts_Invalid1()
$configuration->parts();
}

/**
* @covers ::parts
*/
public function testParts_Invalid2()
{
$this->expectException('Kirby\Exception\InvalidArgumentException');
$this->expectExceptionMessage('Part 0 does not have a component ID');

$configuration = new Configuration([
'parts' => [
[
'label' => 'Some part without component ID'
]
]
]);

$configuration->parts();
}

/**
* @covers ::perspectiveImage
*/
Expand Down

0 comments on commit 5e5c0d1

Please sign in to comment.