ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
ilOrgUnitOperationContextRegisteredObjectiveTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\TestCase;
23
25{
26 protected int $next_id = 3;
27 protected string $context = 'first context';
28
29 protected function getMockEnviroment(
30 bool $expect_insert,
31 int $expected_parent_id = 0
32 ): Environment {
33 $db = $this->createMock(ilDBInterface::class);
34 if ($expect_insert) {
35 $db
36 ->expects(self::once())
37 ->method('insert')
38 ->with('il_orgu_op_contexts', [
39 'id' => ['integer', $this->next_id],
40 'context' => ['text', $this->context],
41 'parent_context_id' => ['integer', $expected_parent_id]
42 ]);
43 $db
44 ->expects(self::once())
45 ->method('nextId')
46 ->with('il_orgu_op_contexts')
47 ->willReturn($this->next_id);
48 }
49
50 $env = $this->createMock(Environment::class);
51 $env->expects($this->atLeastOnce())
52 ->method('getResource')
53 ->with(Environment::RESOURCE_DATABASE)
54 ->willReturn($db);
55
56 return $env;
57 }
58
59 protected function getMockObjective(
60 int $existing_context_id,
61 int $parent_id,
62 string $context_name,
63 ?string $parent_context = null
65 $obj = $this
66 ->getMockBuilder(
68 )
69 ->setConstructorArgs([$context_name, $parent_context])
70 ->onlyMethods(['getContextId'])
71 ->getMock();
72
73 $consecutive = [
74 [$context_name, $existing_context_id],
75 [$parent_context, $parent_id]
76 ];
77
78 $obj
79 ->method('getContextId')
80 ->willReturnCallback(
81 function ($db, $context) use (&$consecutive) {
82 list($econtext, $ret) = array_shift($consecutive);
83 $this->assertInstanceOf(ilDBInterface::class, $db);
84 $this->assertEquals($econtext, $context);
85 return $ret;
86 }
87 );
88
89 return $obj;
90 }
91
92 protected function testGetHash(): void
93 {
94 $obj1 = $this->getMockObjective(0, 0, $this->context);
95 $obj2 = $this->getMockObjective(0, 0, 'other context');
96 $this->assertNotEquals(
97 $obj1->getHash(),
98 $obj2->getHash()
99 );
100 }
101
102 protected function testGetPreconditions(): void
103 {
104 $obj = $this->getMockObjective(0, 0, $this->context);
105 $this->assertContainsOnlyInstancesOf(
106 ilDatabaseInitializedObjective::class,
107 $obj->getPreconditions($this->createStub(Environment::class))
108 );
109 }
110
111 public function testIsApplicable(): void
112 {
113 $env = $this->getMockEnviroment(false);
114
115 $obj = $this->getMockObjective(
116 0,
117 0,
118 $this->context
119 );
120 $this->assertTrue($obj->isApplicable($env));
121
122 $obj = $this->getMockObjective(
123 7,
124 0,
125 $this->context
126 );
127 $this->assertNotTrue($obj->isApplicable($env));
128
129 $obj = $this->getMockObjective(
130 0,
131 9,
132 $this->context,
133 'parent'
134 );
135 $this->assertTrue($obj->isApplicable($env));
136
137 $obj = $this->getMockObjective(
138 7,
139 9,
140 $this->context,
141 'parent'
142 );
143 $this->assertNotTrue($obj->isApplicable($env));
144 }
145
147 {
148 $env = $this->getMockEnviroment(false);
149
150 $obj = $this->getMockObjective(
151 0,
152 0,
153 $this->context,
154 'parent'
155 );
156 $this->expectException(Exception::class);
157 $obj->isApplicable($env);
158 }
159
160 public function testAchieve(): void
161 {
162 $env = $this->getMockEnviroment(true, 13);
163 $obj = $this->getMockObjective(
164 0,
165 13,
166 $this->context,
167 'parent'
168 );
169 $obj->achieve($env);
170 }
171}
getMockObjective(int $existing_context_id, int $parent_id, string $context_name, ?string $parent_context=null)
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
Interface ilDBInterface.