ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
FlavourTest.php
Go to the documentation of this file.
1<?php
2
20
21use PHPUnit\Framework\MockObject\MockObject;
39
44require_once __DIR__ . '/../AbstractTestBase.php';
45
47{
48 public MockObject $resource_builder;
49 private const string BASE_DIR = '/var';
55 private MockObject $flavour_repo;
57
58 #[\Override]
59 protected function setUp(): void
60 {
61 $this->machine_factory = new Factory(new \ILIAS\ResourceStorage\Flavour\Engine\Factory());
62 $storage_handler_mock = $this->createMock(StorageHandler::class);
63 $storage_handler_mock->method('isPrimary')->willReturn(true);
64 $this->storage_handler_factory = new StorageHandlerFactory([
65 $storage_handler_mock
66 ], self::BASE_DIR);
67 $this->flavour_repo = $this->createMock(FlavourRepository::class);
68 $this->resource_builder = $this->createMock(ResourceBuilder::class);
69 $this->stream_access = new StreamAccess(self::BASE_DIR, $this->storage_handler_factory);
70 }
71
72
73 public function testDefinitionVariantNameLengths(): void
74 {
75 $flavour_builder = new FlavourBuilder(
76 $this->flavour_repo,
77 $this->machine_factory,
78 $this->resource_builder,
79 $this->storage_handler_factory,
80 $this->stream_access,
81 new Subject()
82 );
83
84 // Length OK
85 $flavour_definition = $this->createMock(FlavourDefinition::class);
86 $flavour_definition->expects($this->exactly(2))
87 ->method('getVariantName')
88 ->willReturn(str_repeat('a', 768));
89
90 $flavour_builder->has(
92 $flavour_definition
93 );
94 $this->assertTrue(true); // no exception thrown
95
96 // Too long
97 $flavour_definition = $this->createMock(FlavourDefinition::class);
98 $flavour_definition->expects($this->exactly(2))
99 ->method('getVariantName')
100 ->willReturn(str_repeat('a', 769));
101
102 $this->expectException(\InvalidArgumentException::class);
103 $flavour_builder->has(
104 new ResourceIdentification('1'),
105 $flavour_definition
106 );
107 }
108
109 public function testHasFlavour(): void
110 {
111 // Data
112 $rid_one = new ResourceIdentification('1');
113 $rid_two = new ResourceIdentification('2');
114 $flavour_builder = new FlavourBuilder(
115 $this->flavour_repo,
116 $this->machine_factory,
117 $this->resource_builder,
118 $this->storage_handler_factory,
119 $this->stream_access,
120 new Subject()
121 );
122
123 // Expectations
124 $flavour_definition = $this->createMock(FlavourDefinition::class);
125 $flavour_definition->expects($this->exactly(4))
126 ->method('getVariantName')
127 ->willReturn('short');
128
129
130 $consecutive = [
131 [[$rid_one, 0, $flavour_definition], false],
132 [[$rid_two, 0, $flavour_definition], true],
133 ];
134 $this->flavour_repo
135 ->expects($this->exactly(2))
136 ->method('has')
137 ->willReturnCallback(
138 function ($rid, $mid, $def) use (&$consecutive) {
139 [$expected, $ret] = array_shift($consecutive);
140 $this->assertEquals($expected, [$rid, $mid, $def]);
141 return $ret;
142 }
143 );
144
145 // Assertions
146 $this->assertFalse(
147 $flavour_builder->has(
148 $rid_one,
149 $flavour_definition
150 )
151 );
152 $this->assertTrue(
153 $flavour_builder->has(
154 $rid_two,
155 $flavour_definition
156 )
157 );
158 }
159
160 public function testNewFlavour(): void
161 {
162 // Data
163 $rid_one = new ResourceIdentification('1');
164 $flavour_builder = new FlavourBuilder(
165 $this->flavour_repo,
166 $this->machine_factory,
167 $this->resource_builder,
168 $this->storage_handler_factory,
169 $this->stream_access,
170 new Subject()
171 );
172
173 // Expectations
174 $flavour_definition = $this->createMock(FlavourDefinition::class);
175 $flavour_definition
176 ->method('getVariantName')
177 ->willReturn('short');
178
179 $this->flavour_repo->expects($this->once())
180 ->method('has')
181 ->with($rid_one, 0, $flavour_definition)
182 ->willReturn(false);
183
184 $this->resource_builder->expects($this->once())
185 ->method('has')
186 ->with($rid_one)
187 ->willReturn(true);
188
189 $revision = $this->createMock(FileRevision::class);
190
191 $revision->expects($this->once())
192 ->method('getInformation')
193 ->willReturn($this->createMock(FileInformation::class));
194
195 $stream = Streams::ofString('test');
196
197 $resource = $this->createMock(StorableResource::class);
198
199 $this->resource_builder->expects($this->once())
200 ->method('get')
201 ->with($rid_one)
202 ->willReturn($resource);
203
204 $resource
205 ->method('getCurrentRevision')
206 ->willReturn($revision);
207
208
209 $this->resource_builder->expects($this->once())
210 ->method('extractStream')
211 ->with($revision)
212 ->willReturn($stream);
213
214
215 // Assertions
216 $flavour = $flavour_builder->get(
217 $rid_one,
218 $flavour_definition
219 );
220 $this->assertInstanceOf(Flavour::class, $flavour);
221 $stream_resolvers = $flavour->getStreamResolvers();
222 $this->assertCount(1, $stream_resolvers);
223 $first_stream_access = $stream_resolvers[0];
224 $this->assertInstanceOf(StreamResolver::class, $first_stream_access);
225 $resolved_stream = $first_stream_access->getStream();
226 $this->assertSame('empty', (string) $resolved_stream);
227 }
228}
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.