ILIAS  release_8 Revision v8.24
ilObjectTest.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21use PHPUnit\Framework\TestCase;
23
24class ilObjectTest extends TestCase
25{
31
32 protected function setUp(): void
33 {
34 global $DIC;
35 $this->dic_backup = is_object($DIC) ? clone $DIC : $DIC;
36
37 $DIC = new Container();
38 $DIC['ilias'] = $this->createMock(ILIAS::class);
39 $DIC['objDefinition'] = $this->createMock(ilObjectDefinition::class);
40 $DIC['ilDB'] = $this->db_mock = $this->createMock(ilDBInterface::class);
41 $DIC['ilLog'] = $this->createMock(ilLogger::class);
42 $DIC['ilErr'] = $this->createMock(ilErrorHandling::class);
43 $DIC['tree'] = $this->createMock(ilTree::class);
44 $DIC['ilAppEventHandler'] = $this->createMock(ilAppEventHandler::class);
45 $DIC['ilUser'] = $this->createMock(ilObjUser::class);
46 }
47
48 protected function tearDown(): void
49 {
50 global $DIC;
52 }
53
54 public function testCreationDeletion(): void
55 {
56 $obj = new ilObject();
57 $obj->setType("xxx");
58
59 $this->db_mock->expects($this->any())
60 ->method('nextId')
62 ->willReturnOnConsecutiveCalls(21, 22, 23);
63
64 $str = '2022-04-28 08:00:00';
65 $this->db_mock->expects($this->any())
66 ->method('fetchAssoc')
67 ->willReturnOnConsecutiveCalls(
68 ['last_update' => $str, 'create_date' => $str],
69 ['last_update' => $str, 'create_date' => $str],
70 ['last_update' => $str, 'create_date' => $str]
71 );
72
73 $obj->create();
74 $id = $obj->getId();
75 $this->assertEquals(21, $id);
76
77 $obj->create();
78 $id = $obj->getId();
79 $this->assertEquals(22, $id);
80
81 $obj->create();
82 $id = $obj->getId();
83 $this->assertEquals(23, $id);
84 }
85
86 /*
87
88 alex, 25.1.2023
89
90 I outcommented this test, since it fails "sometimes".
91 The way fetchAssoc is mocked seems to run into issues, if unexpected tables are queried
92 during the object instantiation.
93
94 e.g.:
95 ilObjectTest::testSetGetLookup
96 Undefined array key "keyword"
97
98 /home/runner/work/ILIAS/ILIAS/Services/Administration/classes/class.ilSetting.php:92
99 /home/runner/work/ILIAS/ILIAS/Services/Administration/classes/class.ilSetting.php:62
100 /home/runner/work/ILIAS/ILIAS/Services/Logging/classes/class.ilLoggingDBSettings.php:37
101 /home/runner/work/ILIAS/ILIAS/Services/Logging/classes/class.ilLoggingDBSettings.php:46
102 /home/runner/work/ILIAS/ILIAS/Services/Logging/classes/public/class.ilLoggerFactory.php:69
103 /home/runner/work/ILIAS/ILIAS/Services/Logging/classes/public/class.ilLoggerFactory.php:91
104 /home/runner/work/ILIAS/ILIAS/Services/Object/classes/class.ilObject.php:90
105 /home/runner/work/ILIAS/ILIAS/Services/Object/test/ilObjectTest.php:106
106
107 public function testSetGetLookup(): void
108 {
109 global $DIC;
110 $ilUser = $DIC->user();
111
112 $this->db_mock->expects($this->any())
113 ->method('nextId')
114 ->withConsecutive([ilObject::TABLE_OBJECT_DATA], ['object_reference'])
115 ->willReturnOnConsecutiveCalls(21, 22);
116
117 $str = '2022-04-28 08:00:00';
118 $this->db_mock->expects($this->any())
119 ->method('fetchAssoc')
120 ->willReturnOnConsecutiveCalls(
121 ['last_update' => $str, 'create_date' => $str],
122 ['last_update' => $str, 'create_date' => $str],
123 ['last_update' => $str, 'create_date' => $str]
124 );
125
126
127 $obj = new ilObject();
128 $obj->setType("xxx"); // otherwise type check will fail
129 $obj->setTitle("TestObject");
130 $obj->setDescription("TestDescription");
131 $obj->setImportId("imp_44");
132 $obj->create();
133 $obj->createReference();
134 $id = $obj->getId();
135 $ref_id = $obj->getRefId();
136 $this->assertEquals(21, $id);
137 $this->assertEquals(22, $ref_id);
138
139
140 // Reading
141 $DIC['ilDB'] = $this->db_mock = $this->createMock(ilDBInterface::class);
142 $ilDBStatement = $this->createMock(ilDBStatement::class);
143 $this->db_mock->expects($this->any())
144 ->method('query')
145 ->with("SELECT obj_id, type, title, description, owner, create_date, last_update, import_id, offline" . PHP_EOL
146 . "FROM " . ilObject::TABLE_OBJECT_DATA . PHP_EOL
147 . "WHERE obj_id = " . $this->db_mock->quote(21, "integer") . PHP_EOL)
148 ->willReturn($ilDBStatement);
149
150 $this->db_mock->expects($this->once())
151 ->method('numRows')
152 ->with($ilDBStatement)
153 ->willReturn(1);
154
155 $this->db_mock->expects($this->once())
156 ->method('fetchAssoc')
157 ->with($ilDBStatement)
158 ->willReturn([
159 'obj_id' => 21,
160 'type' => 'xxx',
161 'title' => 'TestObject',
162 'description' => 'TestDescription',
163 'owner' => 6,
164 'create_date' => '',
165 'last_update' => '',
166 'import_id' => 'imp_44',
167 'offline' => false,
168 ]);
169
170 $obj = new ilObject($id, false);
171
172 $this->assertEquals(21, $obj->getId());
173 $this->assertEquals('TestObject', $obj->getTitle());
174 $this->assertEquals('TestDescription', $obj->getDescription());
175 $this->assertEquals('imp_44', $obj->getImportId());
176 $this->assertEquals(6, $obj->getOwner());
177 }
178 */
179}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:32
ilDBInterface $db_mock
ILIAS DI Container $dic_backup
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const TABLE_OBJECT_DATA
global $DIC
Definition: feed.php:28
Interface ilDBInterface.