ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DerivatorTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
28
29class DerivatorTest extends TestCase
30{
31 protected function getDerivator(
32 SetInterface $from_set,
33 bool $throw_exception = false
35 $repo = new class ($throw_exception) extends NullRepository {
36 public array $transferred_md = [];
37 public array $error_thrown = [];
38
39 public function __construct(protected bool $throw_exception)
40 {
41 }
42
43 public function transferMD(
44 SetInterface $from_set,
45 int $to_obj_id,
46 int $to_sub_id,
47 string $to_type,
48 bool $throw_error_if_invalid
49 ): void {
50 if ($this->throw_exception) {
51 throw new \ilMDRepositoryException('failed');
52 }
53
54 $this->transferred_md[] = [
55 'from_set' => $from_set,
56 'to_obj_id' => $to_obj_id,
57 'to_sub_id' => $to_sub_id,
58 'to_type' => $to_type
59 ];
60 $this->error_thrown[] = $throw_error_if_invalid;
61 }
62 };
63 return new class ($from_set, $repo) extends Derivator {
64 public function exposeRepository(): RepositoryInterface
65 {
66 return $this->repository;
67 }
68 };
69
70 }
71
72 public function testForObject(): void
73 {
74 $from_set = new NullSet();
75 $derivator = $this->getDerivator($from_set);
76 $derivator->forObject(78, 5, 'to_type');
77
78 $this->assertCount(1, $derivator->exposeRepository()->transferred_md);
79 $this->assertSame(
80 [
81 'from_set' => $from_set,
82 'to_obj_id' => 78,
83 'to_sub_id' => 5,
84 'to_type' => 'to_type'
85 ],
86 $derivator->exposeRepository()->transferred_md[0]
87 );
88 $this->assertCount(1, $derivator->exposeRepository()->error_thrown);
89 $this->assertTrue($derivator->exposeRepository()->error_thrown[0]);
90 }
91
92 public function testForObjectWithSubIDZero(): void
93 {
94 $from_set = new NullSet();
95 $derivator = $this->getDerivator($from_set);
96 $derivator->forObject(78, 0, 'to_type');
97
98 $this->assertCount(1, $derivator->exposeRepository()->transferred_md);
99 $this->assertSame(
100 [
101 'from_set' => $from_set,
102 'to_obj_id' => 78,
103 'to_sub_id' => 78,
104 'to_type' => 'to_type'
105 ],
106 $derivator->exposeRepository()->transferred_md[0]
107 );
108 $this->assertCount(1, $derivator->exposeRepository()->error_thrown);
109 $this->assertTrue($derivator->exposeRepository()->error_thrown[0]);
110 }
111
112 public function testForObjectException(): void
113 {
114 $from_set = new NullSet();
115 $derivator = $this->getDerivator($from_set, true);
116
117 $this->expectException(\ilMDServicesException::class);
118 $derivator->forObject(78, 0, 'to_type');
119 }
120}
getDerivator(SetInterface $from_set, bool $throw_exception=false)
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
if(!file_exists('../ilias.ini.php'))