ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
FlavourMachineTest.php
Go to the documentation of this file.
1 <?php
2 
20 
43 
48 require_once __DIR__ . '/../AbstractTestBase.php';
49 require_once __DIR__ . '/DummyDefinition.php';
50 require_once __DIR__ . '/DummyMachine.php';
51 require_once __DIR__ . '/BrokenDummyMachine.php';
52 require_once __DIR__ . '/SVGDummyMachine.php';
53 
55 {
59  private $imagick_mock;
63  private $gd_mock;
67  private \PHPUnit\Framework\MockObject\MockObject $engine_factory_mock;
68 
69  private array $engine_mocks = [];
70 
71  protected function setUp(): void
72  {
73  $this->engine_factory_mock = $this->createMock(\ILIAS\ResourceStorage\Flavour\Engine\Factory::class);
74  }
75 
76  public function testFactory(): void
77  {
78  $factory = new Factory($this->engine_factory_mock, [
79  \stdClass::class => \stdClass::class,
80  BrokenDummyMachine::class => BrokenDummyMachine::class
81  ]);
82 
83  // Not a machine
84  $definition = $this->createMock(FlavourDefinition::class);
85  $definition->expects($this->once())->method('getFlavourMachineId')->willReturn(\stdClass::class);
86 
87  $null_machine = $factory->get($definition);
88  $this->assertInstanceOf(NullMachine::class, $null_machine);
89  $this->assertEquals('Machine stdClass does not implement FlavourMachine', $null_machine->getReason());
90  $this->assertEquals('null_machine', $null_machine->getId());
91  $this->assertEquals(NoEngine::class, $null_machine->dependsOnEngine());
92 
93  // Broken machine
94  $definition = $this->createMock(FlavourDefinition::class);
95  $definition->expects($this->once())->method('getFlavourMachineId')->willReturn(BrokenDummyMachine::class);
96  $null_machine = $factory->get($definition);
97  $this->assertInstanceOf(NullMachine::class, $null_machine);
98  $this->assertEquals(
99  'Could not instantiate machine ILIAS\ResourceStorage\Flavours\BrokenDummyMachine',
100  $null_machine->getReason()
101  );
102  $this->assertEquals('null_machine', $null_machine->getId());
103  $this->assertEquals(NoEngine::class, $null_machine->dependsOnEngine());
104  }
105 
106  public static function definitionsToMachines(): array
107  {
108  return [
109  [new PagesToExtract(true), ExtractPages::class, ImagickEngineWithOptionalFFMpeg::class],
110  [new CropToSquare(), CropSquare::class, GDEngine::class],
111  [new FitToSquare(), FitSquare::class, GDEngine::class],
112  [new ToGreyScale(), MakeGreyScale::class, GDEngine::class],
113  ];
114  }
115 
116 
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 
146  public function testDefaultMachineEngines(string $machine, string $engine): void
147  {
148  $factory = new \ILIAS\ResourceStorage\Flavour\Engine\Factory();
149  $engine_instance = $factory->get(new $machine());
150  $this->assertInstanceOf($engine, $engine_instance);
151  }
152 
156  public function testNullMachineFallback(FlavourDefinition $d, string $machine, string $engine): void
157  {
158  $factory = new Factory($this->engine_factory_mock);
159 
160  $engine_mock = $this->createMock(Engine::class);
161 
162  $this->engine_factory_mock->expects($this->once())
163  ->method('get')
164  ->willReturn($engine_mock);
165 
166  $engine_mock->expects($this->once())
167  ->method('isRunning')
168  ->willReturn(false);
169 
170  $machine_instance = $factory->get($d);
171  $this->assertInstanceOf(NullMachine::class, $machine_instance);
172  $this->assertEquals(
173  "Machine $machine depends on engine $engine which is not running or available.",
174  $machine_instance->getReason()
175  );
176  }
177 
178 
179  public function testMachineResult(): void
180  {
181  $svg_stream = Streams::ofString(
182  '<?xml version="1.0" encoding="UTF-8"?>
183 <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>'
184  );
185  $machine = new SVGDummyMachine();
186  ;
187  $definition = $this->createSVGColorChangeDefinition('red', 'blue');
188  $file_info = new FileInformation();
189 
190  $result = iterator_to_array($machine->processStream($file_info, $svg_stream, $definition));
191  $this->assertCount(1, $result);
192  $result_one = $result[0];
193  $this->assertInstanceOf(Result::class, $result_one);
194  $this->assertEquals($definition, $result_one->getDefinition());
195  $this->assertInstanceOf(FileStream::class, $result_one->getStream());
196  $this->assertEquals(
197  '<?xml version="1.0" encoding="UTF-8"?>
198 <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>',
199  (string)$result_one->getStream()
200  );
201  }
202 
203  private function createSVGColorChangeDefinition(string $color, string $to_color): FlavourDefinition
204  {
205  return new class ($color, $to_color) extends DummyDefinition {
206  private string $color;
207  private string $to_color;
208 
209  public function __construct(string $color, string $to_color)
210  {
211  $this->color = $color;
212  $this->to_color = $to_color;
214  'svg_color_changer',
215  'svg_color_changing_machine'
216  );
217  }
218 
219  public function getColor(): string
220  {
221  return $this->color;
222  }
223 
224  public function getToColor(): string
225  {
226  return $this->to_color;
227  }
228  };
229  }
230 }
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...
__construct(FlavourBuilder $flavour_builder, ResourceBuilder $resource_builder)
Definition: Flavours.php:34
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)
machinesToEngines
testNullMachineFallback(FlavourDefinition $d, string $machine, string $engine)
definitionsToMachines
PHPUnit Framework MockObject MockObject $engine_factory_mock
testDefaultMachines(FlavourDefinition $d, string $machine)
definitionsToMachines