ILIAS  release_8 Revision v8.24
ilStudyProgrammeProgressTreeTest.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
23
24require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
25
26class NodeMock extends Node
27{
28 protected int $score;
29 public function withScore(int $score): self
30 {
31 $clone = clone $this;
32 $clone->score = $score;
33 return $clone;
34 }
35 public function getScore(): int
36 {
37 return $this->score;
38 }
39}
40
41class ilStudyProgrammeProgressTreeTest extends \PHPUnit\Framework\TestCase
42{
43 protected function build(array $data, $node_id): Node
44 {
45 $subnodes = [];
46 foreach ($data[$node_id] as $subnode_id) {
47 $subnodes[] = $this->build($data, $subnode_id);
48 }
49 return (new NodeMock($node_id))->setSubnodes($subnodes);
50 }
51
52 public function setUp(): void
53 {
54 $data = [
55 'top' => ['1.2', '1.1'],
56 '1.1' => [],
57 '1.2' => ['1.2.1', '1.2.2'],
58 '1.2.1' => ['1.2.1.1'],
59 '1.2.2' => [],
60 '1.2.1.1' => []
61 ];
62
63 $this->topnode = $this->build($data, 'top');
64 }
65
66 public function testPGSTreeCreation(): void
67 {
68 $n = $this->topnode;
69 $this->assertEquals($n->getId(), 'top');
70 $this->assertInstanceOf(Node::class, $n->getSubnode('1.1'));
71
72 $this->assertEquals(
73 ['top', '1.2', '1.2.2'],
74 $n->getSubnode('1.2')->getSubnode('1.2.2')->getPath()
75 );
76
77 $this->assertEquals(
78 ['top', '1.2', '1.2.1'],
79 $n->findSubnodePath('1.2.1')
80 );
81 }
82
83 public function testPGSTreeZipperNav(): void
84 {
85 $zipper = new Zipper($this->topnode);
86 $this->assertInstanceOf(Zipper::class, $zipper->toChild('1.1'));
87 $this->assertInstanceOf(
88 Zipper::class,
89 $zipper->toPath($this->topnode->findSubnodePath('1.2.1'))->toParent()
90 );
91 $this->assertInstanceOf(
92 Node::class,
93 $zipper->toPath($this->topnode->findSubnodePath('1.2.1'))->getRoot()
94 );
95 }
96
97 public function testPGSTreeZipperManipulation(): void
98 {
99 $zipper = new Zipper($this->topnode);
100
101 $path = $this->topnode->findSubnodePath('1.2.1');
102 $modified = $zipper
103 ->toPath($path)->modifyFocus(fn ($n) => $n->withScore(7))
104 ->toParent()->modifyFocus(fn ($n) => $n->withScore(6))
105 ->getRoot();
106
107 $zipper = new Zipper($modified);
108 $other_path = $this->topnode->findSubnodePath('1.1');
109 $modified = $zipper
110 ->toPath($other_path)->modifyFocus(fn ($n) => $n->withScore(8))
111 ->getRoot();
112 $this->assertEquals(7, $modified->getSubnode('1.2')->getSubnode('1.2.1')->getScore());
113 $this->assertEquals(6, $modified->getSubnode('1.2')->getScore());
114 $this->assertEquals(8, $modified->getSubnode('1.1')->getScore());
115
116 //should not change others...
117 $zipper = new Zipper($modified);
118 $modified = $zipper
119 ->toPath($path)->modifyFocus(fn ($n) => $n->withScore(17))
120 ->getRoot();
121 $this->assertEquals(17, $modified->getSubnode('1.2')->getSubnode('1.2.1')->getScore());
122 $this->assertEquals(6, $modified->getSubnode('1.2')->getScore());
123 $this->assertEquals(8, $modified->getSubnode('1.1')->getScore());
124 }
125
126 public function testPGSTreeZipperManipulateAll(): void
127 {
128 $zipper = new Zipper($this->topnode);
129 $modified = $zipper
130 ->modifyAll(fn ($n) => $n->withScore(count($n->getSubnodes())))
131 ->getRoot();
132
133 $this->assertEquals(2, $modified->getScore());
134 $this->assertEquals(0, $modified->getSubnode('1.1')->getScore());
135 $this->assertEquals(2, $modified->getSubnode('1.2')->getScore());
136 $this->assertEquals(1, $modified->getSubnode('1.2')->getSubnode('1.2.1')->getScore());
137 $this->assertEquals(0, $modified->getSubnode('1.2')->getSubnode('1.2.1')->getSubnode('1.2.1.1')->getScore());
138 $this->assertEquals(0, $modified->getSubnode('1.2')->getSubnode('1.2.2')->getScore());
139 }
140}
$path
Definition: ltiservices.php:32