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