View source
<?php
namespace Drupal\system\Tests\Image;
@trigger_error(__FILE__ . ' is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Use Drupal\\FunctionalTests\\Image\\ToolkitTestBase instead. See https://www.drupal.org/node/2862641.', E_USER_DEPRECATED);
use Drupal\simpletest\WebTestBase;
use Drupal\Component\Render\FormattableMarkup;
abstract class ToolkitTestBase extends WebTestBase {
public static $modules = [
'image_test',
];
protected $file;
protected $imageFactory;
protected $image;
protected function setUp() {
parent::setUp();
$this->imageFactory = $this->container
->get('image.factory');
$file = current($this
->drupalGetTestFiles('image'));
$this->file = $file->uri;
$this->image = $this
->getImage();
$this
->imageTestReset();
}
protected function getImage() {
$image = $this->imageFactory
->get($this->file, 'test');
$this
->assertTrue($image
->isValid(), 'Image file was parsed.');
return $image;
}
public function assertToolkitOperationsCalled(array $expected) {
$operations = [
'resize',
'rotate',
'crop',
'desaturate',
'create_new',
'scale',
'scale_and_crop',
'my_operation',
'convert',
];
if (count(array_intersect($expected, $operations)) > 0 && !in_array('apply', $expected)) {
$expected[] = 'apply';
}
$actual = array_keys(array_filter($this
->imageTestGetAllCalls()));
$uncalled = array_diff($expected, $actual);
if (count($uncalled)) {
$this
->assertTrue(FALSE, new FormattableMarkup('Expected operations %expected to be called but %uncalled was not called.', [
'%expected' => implode(', ', $expected),
'%uncalled' => implode(', ', $uncalled),
]));
}
else {
$this
->assertTrue(TRUE, new FormattableMarkup('All the expected operations were called: %expected', [
'%expected' => implode(', ', $expected),
]));
}
$unexpected = array_diff($actual, $expected);
if (count($unexpected) && (!in_array('apply', $expected) || count(array_intersect($unexpected, $operations)) !== count($unexpected))) {
$this
->assertTrue(FALSE, new FormattableMarkup('Unexpected operations were called: %unexpected.', [
'%unexpected' => implode(', ', $unexpected),
]));
}
else {
$this
->assertTrue(TRUE, 'No unexpected operations were called.');
}
}
public function imageTestReset() {
$results = [
'parseFile' => [],
'save' => [],
'settings' => [],
'apply' => [],
'resize' => [],
'rotate' => [],
'crop' => [],
'desaturate' => [],
'create_new' => [],
'scale' => [],
'scale_and_crop' => [],
'convert' => [],
];
\Drupal::state()
->set('image_test.results', $results);
}
public function imageTestGetAllCalls() {
return \Drupal::state()
->get('image_test.results') ?: [];
}
}