ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
FlavourMachineTest.php
Go to the documentation of this file.
1 <?php
2 
20 
44 
49 require_once __DIR__ . '/../AbstractTestBase.php';
50 require_once __DIR__ . '/DummyDefinition.php';
51 require_once __DIR__ . '/DummyMachine.php';
52 require_once __DIR__ . '/BrokenDummyMachine.php';
53 require_once __DIR__ . '/SVGDummyMachine.php';
54 
56 {
60  private $imagick_mock;
64  private $gd_mock;
68  private MockObject $engine_factory_mock;
69 
70  private array $engine_mocks = [];
71 
72  #[\Override]
73  protected function setUp(): void
74  {
75  $this->engine_factory_mock = $this->createMock(\ILIAS\ResourceStorage\Flavour\Engine\Factory::class);
76  }
77 
78  public function testFactory(): void
79  {
80  $factory = new Factory($this->engine_factory_mock, [
81  \stdClass::class => \stdClass::class,
82  BrokenDummyMachine::class => BrokenDummyMachine::class
83  ]);
84 
85  // Not a machine
86  $definition = $this->createMock(FlavourDefinition::class);
87  $definition->expects($this->once())->method('getFlavourMachineId')->willReturn(\stdClass::class);
88 
89  $null_machine = $factory->get($definition);
90  $this->assertInstanceOf(NullMachine::class, $null_machine);
91  $this->assertEquals('Machine stdClass does not implement FlavourMachine', $null_machine->getReason());
92  $this->assertEquals('null_machine', $null_machine->getId());
93  $this->assertEquals(NoEngine::class, $null_machine->dependsOnEngine());
94 
95  // Broken machine
96  $definition = $this->createMock(FlavourDefinition::class);
97  $definition->expects($this->once())->method('getFlavourMachineId')->willReturn(BrokenDummyMachine::class);
98  $null_machine = $factory->get($definition);
99  $this->assertInstanceOf(NullMachine::class, $null_machine);
100  $this->assertEquals(
101  'Could not instantiate machine ILIAS\ResourceStorage\Flavours\BrokenDummyMachine',
102  $null_machine->getReason()
103  );
104  $this->assertEquals('null_machine', $null_machine->getId());
105  $this->assertEquals(NoEngine::class, $null_machine->dependsOnEngine());
106  }
107 
108  public static function definitionsToMachines(): array
109  {
110  return [
111  [new PagesToExtract(true), ExtractPages::class, ImagickEngineWithOptionalFFMpeg::class],
112  [new CropToSquare(), CropSquare::class, GDEngine::class],
113  [new FitToSquare(), FitSquare::class, GDEngine::class],
114  [new ToGreyScale(), MakeGreyScale::class, GDEngine::class],
115  ];
116  }
117 
118 
119  #[DataProvider('definitionsToMachines')]
120  public function testDefaultMachines(FlavourDefinition $d, string $machine): void
121  {
122  $factory = new Factory($this->engine_factory_mock);
123  $this->engine_factory_mock->expects($this->exactly(1))
124  ->method('get')
125  ->willReturn(new NoEngine());
126 
127  $machine_instance = $factory->get($d);
128  $this->assertInstanceOf($machine, $machine_instance);
129  $machine_instance_second_get = $factory->get($d);
130  $this->assertSame($machine_instance, $machine_instance_second_get);
131  }
132 
133  public static function machinesToEngines(): array
134  {
135  return [
136  [ExtractPages::class, ImagickEngine::class],
137  [CropSquare::class, GDEngine::class],
138  [FitSquare::class, GDEngine::class],
139  [MakeGreyScale::class, GDEngine::class],
140  ];
141  }
142 
143  #[DataProvider('machinesToEngines')]
144  public function testDefaultMachineEngines(string $machine, string $engine): void
145  {
146  $factory = new \ILIAS\ResourceStorage\Flavour\Engine\Factory();
147  $engine_instance = $factory->get(new $machine());
148  $this->assertInstanceOf($engine, $engine_instance);
149  }
150 
151  #[DataProvider('definitionsToMachines')]
152  public function testNullMachineFallback(FlavourDefinition $d, string $machine, string $engine): void
153  {
154  $factory = new Factory($this->engine_factory_mock);
155 
156  $engine_mock = $this->createMock(Engine::class);
157 
158  $this->engine_factory_mock->expects($this->once())
159  ->method('get')
160  ->willReturn($engine_mock);
161 
162  $engine_mock->expects($this->once())
163  ->method('isRunning')
164  ->willReturn(false);
165 
166  $machine_instance = $factory->get($d);
167  $this->assertInstanceOf(NullMachine::class, $machine_instance);
168  $this->assertEquals(
169  "Machine $machine depends on engine $engine which is not running or available.",
170  $machine_instance->getReason()
171  );
172  }
173 
174 
175  public function testMachineResult(): void
176  {
177  $svg_stream = Streams::ofString(
178  '<?xml version="1.0" encoding="UTF-8"?>
179 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 155 155"><defs><style>.cls-1{fill:red;}</style></defs><g><g><rect class="cls-1" x="3" y="3" width="150" height="150"/><path d="M151.14,6V151.14H6V6H151.14m6-6H0V157.14H157.14V0h0Z"/></g></g></svg>'
180  );
181  $machine = new SVGDummyMachine();
182  ;
183  $definition = $this->createSVGColorChangeDefinition('red', 'blue');
184  $file_info = new FileInformation();
185 
186  $result = iterator_to_array($machine->processStream($file_info, $svg_stream, $definition));
187  $this->assertCount(1, $result);
188  $result_one = $result[0];
189  $this->assertInstanceOf(Result::class, $result_one);
190  $this->assertEquals($definition, $result_one->getDefinition());
191  $this->assertInstanceOf(FileStream::class, $result_one->getStream());
192  $this->assertEquals(
193  '<?xml version="1.0" encoding="UTF-8"?>
194 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 155 155"><defs><style>.cls-1{fill:blue;}</style></defs><g><g><rect class="cls-1" x="3" y="3" width="150" height="150"/><path d="M151.14,6V151.14H6V6H151.14m6-6H0V157.14H157.14V0h0Z"/></g></g></svg>',
195  (string) $result_one->getStream()
196  );
197  }
198 
199  private function createSVGColorChangeDefinition(string $color, string $to_color): FlavourDefinition
200  {
201  return new class ($color, $to_color) extends DummyDefinition {
202  public function __construct(private string $color, private string $to_color)
203  {
205  'svg_color_changer',
206  'svg_color_changing_machine'
207  );
208  }
209 
210  public function getColor(): string
211  {
212  return $this->color;
213  }
214 
215  public function getToColor(): string
216  {
217  return $this->to_color;
218  }
219  };
220  }
221 }
Interface Observer Contains several chained tasks and infos about them.
createSVGColorChangeDefinition(string $color, string $to_color)
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
__construct(Container $dic, ilPlugin $plugin)
testDefaultMachineEngines(string $machine, string $engine)
__construct(protected FlavourBuilder $flavour_builder, protected ResourceBuilder $resource_builder)
Definition: Flavours.php:31
testNullMachineFallback(FlavourDefinition $d, string $machine, string $engine)
testDefaultMachines(FlavourDefinition $d, string $machine)