ToolkitTest.php in Drupal 9
File
core/tests/Drupal/KernelTests/Core/Image/ToolkitTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Image;
use Drupal\Core\Image\ImageInterface;
use Drupal\Core\ImageToolkit\ImageToolkitInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\Traits\Core\Image\ToolkitTestTrait;
class ToolkitTest extends KernelTestBase {
use ToolkitTestTrait;
protected static $modules = [
'image_test',
'system',
];
protected $image;
protected function setUp() : void {
parent::setUp();
$this->image = $this
->getImage();
$this
->imageTestReset();
}
public function testGetAvailableToolkits() {
$manager = $this->container
->get('image.toolkit.manager');
$toolkits = $manager
->getAvailableToolkits();
$this
->assertArrayHasKey('test', $toolkits);
$this
->assertArrayHasKey('test:derived_toolkit', $toolkits);
$this
->assertArrayNotHasKey('broken', $toolkits);
$this
->assertToolkitOperationsCalled([]);
}
public function testLoad() {
$image = $this
->getImage();
$this
->assertInstanceOf(ImageInterface::class, $image);
$this
->assertEquals('test', $image
->getToolkitId());
$this
->assertToolkitOperationsCalled([
'parseFile',
]);
}
public function testSave() {
$this
->assertFalse($this->image
->save());
$this
->assertToolkitOperationsCalled([
'save',
]);
}
public function testApply() {
$data = [
'p1' => 1,
'p2' => TRUE,
'p3' => 'text',
];
$this
->assertTrue($this->image
->apply('my_operation', $data));
$this
->assertToolkitOperationsCalled([
'apply',
]);
$calls = $this
->imageTestGetAllCalls();
$this
->assertEquals('my_operation', $calls['apply'][0][0]);
$this
->assertEquals(1, $calls['apply'][0][1]['p1']);
$this
->assertTrue($calls['apply'][0][1]['p2']);
$this
->assertEquals('text', $calls['apply'][0][1]['p3']);
}
public function testApplyNoParameters() {
$this
->assertTrue($this->image
->apply('my_operation'));
$this
->assertToolkitOperationsCalled([
'apply',
]);
$calls = $this
->imageTestGetAllCalls();
$this
->assertEquals('my_operation', $calls['apply'][0][0]);
$this
->assertSame([], $calls['apply'][0][1]);
}
public function testDerivative() {
$toolkit_manager = $this->container
->get('image.toolkit.manager');
$operation_manager = $this->container
->get('image.toolkit.operation.manager');
$toolkit = $toolkit_manager
->createInstance('test:derived_toolkit');
$this
->assertInstanceOf(ImageToolkitInterface::class, $toolkit);
$blur = $operation_manager
->getToolkitOperation($toolkit, 'blur');
$invert = $operation_manager
->getToolkitOperation($toolkit, 'invert');
$this
->assertEquals('foo_derived', $blur
->getPluginId());
$this
->assertEquals('bar', $invert
->getPluginId());
}
}