ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilOrgUnitOperationRegisteredObjectiveTest.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 $operation = 'first operation';
28 protected string $description = 'description';
29
30 protected function getMockEnviroment(
31 bool $expect_insert,
32 int $expected_context_id = 0
33 ): Environment {
34 $db = $this->createMock(ilDBInterface::class);
35 if ($expect_insert) {
36 $db
37 ->expects(self::once())
38 ->method('insert')
39 ->with('il_orgu_operations', [
40 'operation_id' => ['integer', $this->next_id],
41 'operation_string' => ['text', $this->operation],
42 'description' => ['text', $this->description],
43 'list_order' => ['integer', 0],
44 'context_id' => ['integer', $expected_context_id],
45 ]);
46 $db
47 ->expects(self::once())
48 ->method('nextId')
49 ->with('il_orgu_operations')
50 ->willReturn($this->next_id);
51 }
52
53 $env = $this->createMock(Environment::class);
54 $env
55 ->method('getResource')
56 ->with(Environment::RESOURCE_DATABASE)
57 ->willReturn($db);
58
59 return $env;
60 }
61
62 protected function getMockObjective(
63 bool $does_op_already_exist,
64 int $context_id,
65 string $operation_name
67 $obj = $this
68 ->getMockBuilder(
70 )
71 ->setConstructorArgs([$operation_name, $this->description, 'context'])
72 ->onlyMethods(['getContextId', 'doesOperationExistInContext'])
73 ->getMock();
74
75 $obj
76 ->method('getContextId')
77 ->with($this->isInstanceOf(ilDBInterface::class), 'context')
78 ->willReturn($context_id);
79 $obj
80 ->method('doesOperationExistInContext')
81 ->with($this->isInstanceOf(ilDBInterface::class), $context_id, $operation_name)
82 ->willReturn($does_op_already_exist);
83
84 return $obj;
85 }
86
87 protected function testGetHash(): void
88 {
89 $obj1 = $this->getMockObjective(true, 0, $this->operation);
90 $obj2 = $this->getMockObjective(true, 0, 'other op');
91 $this->assertNotEquals(
92 $obj1->getHash(),
93 $obj2->getHash()
94 );
95 }
96
97 protected function testGetPreconditions(): void
98 {
99 $env = $this->createMock(Environment::class);
100 $obj = $this->getMockObjective(true, 0, $this->operation, '');
101 $this->assertContainsOnlyInstancesOf(
102 ilDatabaseInitializedObjective::class,
103 $obj->getPreconditions($env)
104 );
105 }
106
107 public function testIsApplicable(): void
108 {
109 $env = $this->getMockEnviroment(false);
110
111 $obj = $this->getMockObjective(false, 9, $this->operation);
112 $this->assertTrue($obj->isApplicable($env));
113
114 $obj = $this->getMockObjective(true, 9, $this->operation);
115 $this->assertNotTrue($obj->isApplicable($env));
116 }
117
119 {
120 $env = $this->getMockEnviroment(false);
121
122 $obj = $this->getMockObjective(false, 0, $this->operation);
123 $this->expectException(Exception::class);
124 $obj->isApplicable($env);
125 }
126
127 public function testAchieve(): void
128 {
129 $env = $this->getMockEnviroment(true, 13);
130 $obj = $this->getMockObjective(false, 13, $this->operation);
131 $obj->achieve($env);
132 }
133}
getMockObjective(bool $does_op_already_exist, int $context_id, string $operation_name)
getMockEnviroment(bool $expect_insert, int $expected_context_id=0)
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
Interface ilDBInterface.