-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ fixes wrong node used for condition to call $onParentUnlinked + fixes an issue with node keys by providing a way to calculate them instead + more edge case tests
- Loading branch information
Showing
6 changed files
with
206 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dakujem\Test; | ||
|
||
use Dakujem\Oliva\Exceptions\InvalidNodeFactoryReturnValue; | ||
use Dakujem\Oliva\MaterializedPath; | ||
use Dakujem\Oliva\Recursive; | ||
use Dakujem\Oliva\Simple\NodeBuilder; | ||
use Dakujem\Oliva\Simple\TreeWrapper; | ||
use Tester\Assert; | ||
|
||
require_once __DIR__ . '/setup.php'; | ||
|
||
// Test some edge cases | ||
(function () { | ||
$testFactory = function (callable $badNodeFactory): void { | ||
Assert::throws(function () use ($badNodeFactory) { | ||
(new TreeWrapper($badNodeFactory, fn() => []))->wrap(['item']); | ||
}, InvalidNodeFactoryReturnValue::class); | ||
|
||
Assert::throws(function () use ($badNodeFactory) { | ||
(new NodeBuilder($badNodeFactory))->node('item'); | ||
}, InvalidNodeFactoryReturnValue::class); | ||
|
||
Assert::throws(function () use ($badNodeFactory) { | ||
(new MaterializedPath\TreeBuilder($badNodeFactory, fn() => []))->build(['item']); | ||
}, InvalidNodeFactoryReturnValue::class); | ||
|
||
Assert::throws(function () use ($badNodeFactory) { | ||
(new Recursive\TreeBuilder($badNodeFactory, fn() => 1234, fn() => null))->build(['item']); | ||
}, InvalidNodeFactoryReturnValue::class); | ||
}; | ||
|
||
// These factories do not return a valid Node instance (node contract). | ||
$testFactory(fn() => null); | ||
$testFactory(fn() => 'foo'); | ||
$testFactory(fn($data) => new NotNodeAtAll($data)); | ||
$testFactory(fn($data) => new NotMovable($data)); | ||
|
||
// The following can be uncommented to test that the failing tests actually do not fail... lol. | ||
// $testFactory(fn($data) => new Node($data)); | ||
})(); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dakujem\Test; | ||
|
||
use Dakujem\Oliva\Exceptions\NodeNotMovable; | ||
use Dakujem\Oliva\Node; | ||
use Dakujem\Oliva\Simple\NodeBuilder; | ||
use Dakujem\Oliva\Tree; | ||
use Tester\Assert; | ||
|
||
require_once __DIR__ . '/setup.php'; | ||
|
||
// Test tree manipulation edge cases | ||
(function () { | ||
$nodeWithUnmovableChild = new Node('A', ['one' => new Node('B'), 'two' => new NotMovable('impostor')]); | ||
$nodeWithUnmovableParent = new Node('A', parent: new NotMovable('impostor')); | ||
|
||
Assert::throws(function () use ($nodeWithUnmovableChild) { | ||
Tree::unlinkChildren($nodeWithUnmovableChild); | ||
}, NodeNotMovable::class, 'Encountered a non-movable node while manipulating a tree.'); | ||
|
||
Assert::throws(function () use ($nodeWithUnmovableParent) { | ||
Tree::unlink($nodeWithUnmovableParent); | ||
}, NodeNotMovable::class, 'Encountered a non-movable node while manipulating a tree.'); | ||
|
||
Assert::throws(function () use ($nodeWithUnmovableParent) { | ||
Tree::link($nodeWithUnmovableParent, new Node(null)); | ||
}, NodeNotMovable::class, 'Encountered a non-movable node while manipulating a tree.'); | ||
|
||
Assert::throws(function () use ($nodeWithUnmovableChild) { | ||
Tree::reindexTree($nodeWithUnmovableChild, null, null); | ||
}, NodeNotMovable::class, 'Encountered a non-movable node while manipulating a tree.'); | ||
})(); | ||
|
||
(function () { | ||
$proxy = new NodeBuilder(fn(mixed $data) => new Node($data)); | ||
|
||
$one = $proxy->node('A', $childrenOfOne = [ | ||
$b = $proxy->node('B'), | ||
$c = $proxy->node('C'), | ||
]); | ||
$two = $proxy->node('X', $childrenOfTwo = [ | ||
$y = $proxy->node('Y'), | ||
$z = $proxy->node('Z'), | ||
]); | ||
|
||
$shouldBeTwo = Tree::link($y, $one); | ||
$shouldBeOne = Tree::link($y, $two); | ||
Assert::same($two, $shouldBeTwo); | ||
Assert::same($one, $shouldBeOne); | ||
|
||
$counter = 0; | ||
Tree::linkChildren($one, $childrenOfTwo, function ($parent) use (&$counter, $two) { | ||
Assert::same($two, $parent); | ||
$counter += 1; | ||
}); | ||
Assert::same(2, $counter); | ||
})(); | ||
|
||
(function () { | ||
// | ||
})(); | ||
|