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