ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
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{
51 public MockObject $resource_builder;
52 private const string BASE_DIR = '/var';
61 private MockObject $flavour_repo;
63
64 #[\Override]
65 protected function setUp(): void
66 {
67 $this->machine_factory = new Factory(new \ILIAS\ResourceStorage\Flavour\Engine\Factory());
68 $storage_handler_mock = $this->createMock(StorageHandler::class);
69 $storage_handler_mock->method('isPrimary')->willReturn(true);
70 $this->storage_handler_factory = new StorageHandlerFactory([
71 $storage_handler_mock
72 ], self::BASE_DIR);
73 $this->flavour_repo = $this->createMock(FlavourRepository::class);
74 $this->resource_builder = $this->createMock(ResourceBuilder::class);
75 $this->stream_access = new StreamAccess(self::BASE_DIR, $this->storage_handler_factory);
76 }
77
78
79 public function testDefinitionVariantNameLengths(): void
80 {
81 $flavour_builder = new FlavourBuilder(
82 $this->flavour_repo,
83 $this->machine_factory,
84 $this->resource_builder,
85 $this->storage_handler_factory,
86 $this->stream_access,
87 new Subject()
88 );
89
90 // Length OK
91 $flavour_definition = $this->createMock(FlavourDefinition::class);
92 $flavour_definition->expects($this->exactly(2))
93 ->method('getVariantName')
94 ->willReturn(str_repeat('a', 638));
95
96 $flavour_builder->has(
98 $flavour_definition
99 );
100 $this->assertTrue(true); // no exception thrown
101
102 // Too long
103 $flavour_definition = $this->createMock(FlavourDefinition::class);
104 $flavour_definition->expects($this->exactly(2))
105 ->method('getVariantName')
106 ->willReturn(str_repeat('a', 769));
107
108 $this->expectException(\InvalidArgumentException::class);
109 $flavour_builder->has(
110 new ResourceIdentification('1'),
111 $flavour_definition
112 );
113 }
114
115 public function testHasFlavour(): void
116 {
117 // Data
118 $rid_one = new ResourceIdentification('1');
119 $rid_two = new ResourceIdentification('2');
120 $flavour_builder = new FlavourBuilder(
121 $this->flavour_repo,
122 $this->machine_factory,
123 $this->resource_builder,
124 $this->storage_handler_factory,
125 $this->stream_access,
126 new Subject()
127 );
128
129 // Expectations
130 $flavour_definition = $this->createMock(FlavourDefinition::class);
131 $flavour_definition->expects($this->exactly(4))
132 ->method('getVariantName')
133 ->willReturn('short');
134
135
136 $consecutive = [
137 [[$rid_one, 0, $flavour_definition], false],
138 [[$rid_two, 0, $flavour_definition], true],
139 ];
140 $this->flavour_repo
141 ->expects($this->exactly(2))
142 ->method('has')
143 ->willReturnCallback(
144 function ($rid, $mid, $def) use (&$consecutive) {
145 [$expected, $ret] = array_shift($consecutive);
146 $this->assertEquals($expected, [$rid, $mid, $def]);
147 return $ret;
148 }
149 );
150
151 // Assertions
152 $this->assertFalse(
153 $flavour_builder->has(
154 $rid_one,
155 $flavour_definition
156 )
157 );
158 $this->assertTrue(
159 $flavour_builder->has(
160 $rid_two,
161 $flavour_definition
162 )
163 );
164 }
165
166 public function testNewFlavour(): void
167 {
168 // Data
169 $rid_one = new ResourceIdentification('1');
170 $flavour_builder = new FlavourBuilder(
171 $this->flavour_repo,
172 $this->machine_factory,
173 $this->resource_builder,
174 $this->storage_handler_factory,
175 $this->stream_access,
176 new Subject()
177 );
178
179 // Expectations
180 $flavour_definition = $this->createMock(FlavourDefinition::class);
181 $flavour_definition
182 ->method('getVariantName')
183 ->willReturn('short');
184
185 $this->flavour_repo->expects($this->once())
186 ->method('has')
187 ->with($rid_one, 0, $flavour_definition)
188 ->willReturn(false);
189
190 $this->resource_builder->expects($this->once())
191 ->method('has')
192 ->with($rid_one)
193 ->willReturn(true);
194
195 $revision = $this->createMock(FileRevision::class);
196
197 $revision->expects($this->once())
198 ->method('getInformation')
199 ->willReturn($this->createStub(FileInformation::class));
200
201 $stream = Streams::ofString('test');
202
203 $resource = $this->createMock(StorableResource::class);
204
205 $this->resource_builder->expects($this->once())
206 ->method('get')
207 ->with($rid_one)
208 ->willReturn($resource);
209
210 $resource
211 ->method('getCurrentRevision')
212 ->willReturn($revision);
213
214
215 $this->resource_builder->expects($this->once())
216 ->method('extractStream')
217 ->with($revision)
218 ->willReturn($stream);
219
220
221 // Assertions
222 $flavour = $flavour_builder->get(
223 $rid_one,
224 $flavour_definition
225 );
226 $this->assertInstanceOf(Flavour::class, $flavour);
227 $stream_resolvers = $flavour->getStreamResolvers();
228 $this->assertCount(1, $stream_resolvers);
229 $first_stream_access = $stream_resolvers[0];
230 $this->assertInstanceOf(StreamResolver::class, $first_stream_access);
231 $resolved_stream = $first_stream_access->getStream();
232 $this->assertSame('empty', (string) $resolved_stream);
233 }
234}
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.