Skip to content

Commit

Permalink
test: add tests for invalid states
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Jan 16, 2024
1 parent e564d75 commit d7c8ffd
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/Loader/SiteKitResourceHierarchyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ protected function getPrimaryParentLocation(Resource $resource): ?string

$firstParent = null;
foreach ($parentList as $parent) {
if (!is_array($parent)) {
throw new InvalidResourceException(
$resource->getLocation(),
'primary parent in ' .
'base.trees.' . $this->treeName . '.parents ' .
'is invalid'
);
}
$firstParent ??= $parent;
$isPrimary = $parent['isPrimary'] ?? false;
if ($isPrimary === true) {
Expand Down Expand Up @@ -187,7 +195,15 @@ protected function getChildrenLocationList(Resource $resource): array
return [];
}

return array_map(function ($child) {
return array_map(function ($child) use ($resource) {
if (!is_array($child)) {
throw new InvalidResourceException(
$resource->getLocation(),
'children in ' .
'base.trees.' . $this->treeName . '.children ' .
'not an array'
);
}
return $child['url'];
}, $childrenList);
}
Expand Down
1 change: 0 additions & 1 deletion test/DataBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#[CoversClass(DataBag::class)]
class DataBagTest extends TestCase
{

public function testGet(): void
{
$resource = new DataBag(
Expand Down
14 changes: 14 additions & 0 deletions test/Loader/SiteKitResourceHierarchyLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ public function testLoadChildren(): void
);
}

public function testLoadChildrenWithInvalidData(): void
{
$this->expectException(InvalidResourceException::class);
$children = $this->hierarchyLoader->loadChildren(
'/childrenWithInvalidData.php'
);
}

public function testLoadWithoutChildren(): void
{
$children = $this->hierarchyLoader->loadChildren('/c.php');
Expand Down Expand Up @@ -139,6 +147,12 @@ public function testLoadRootResourcePrimaryParentWithoutUrl(): void
$this->hierarchyLoader->loadParent('/primaryParentWithoutUrl.php');
}

public function testLoadRootResourcePrimaryParentWithInvalidData(): void
{
$this->expectException(InvalidResourceException::class);
$this->hierarchyLoader->loadParent('/primaryParentWithInvalidData.php');
}

public function testLoadRootResourcePrimaryParentWithNonStringUrl(): void
{
$this->expectException(InvalidResourceException::class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

return new \Atoolo\Resource\Resource(
'/primaryParentWithoutUrl.php',
'primaryParentWithoutUrl',
'primaryParentWithoutUrl',
'',
[
'base' => [
'trees' => [
'category' => [
'children' => [
'a' => 'invalid'
]
]
]
]
]
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

return new \Atoolo\Resource\Resource(
'/primaryParentWithoutUrl.php',
'primaryParentWithoutUrl',
'primaryParentWithoutUrl',
'',
[
'base' => [
'trees' => [
'category' => [
'parents' => [
'a' => 'invalid'
]
]
]
]
]
);

0 comments on commit d7c8ffd

Please sign in to comment.