ToolkitTestBase.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Drupal\FunctionalTests\Image;
  3. use Drupal\Component\Render\FormattableMarkup;
  4. use Drupal\Tests\BrowserTestBase;
  5. use Drupal\Tests\TestFileCreationTrait;
  6. /**
  7. * Base class for image manipulation testing.
  8. */
  9. abstract class ToolkitTestBase extends BrowserTestBase {
  10. use TestFileCreationTrait {
  11. getTestFiles as drupalGetTestFiles;
  12. }
  13. /**
  14. * Modules to enable.
  15. *
  16. * @var array
  17. */
  18. public static $modules = ['image_test'];
  19. /**
  20. * The URI for the file.
  21. *
  22. * @var string
  23. */
  24. protected $file;
  25. /**
  26. * The image factory service.
  27. *
  28. * @var \Drupal\Core\Image\ImageFactory
  29. */
  30. protected $imageFactory;
  31. /**
  32. * The image object for the test file.
  33. *
  34. * @var \Drupal\Core\Image\ImageInterface
  35. */
  36. protected $image;
  37. protected function setUp() {
  38. parent::setUp();
  39. // Set the image factory service.
  40. $this->imageFactory = $this->container->get('image.factory');
  41. // Pick a file for testing.
  42. $file = current($this->drupalGetTestFiles('image'));
  43. $this->file = $file->uri;
  44. // Setup a dummy image to work with.
  45. $this->image = $this->getImage();
  46. // Clear out any hook calls.
  47. $this->imageTestReset();
  48. }
  49. /**
  50. * Sets up an image with the custom toolkit.
  51. *
  52. * @return \Drupal\Core\Image\ImageInterface
  53. * The image object.
  54. */
  55. protected function getImage() {
  56. $image = $this->imageFactory->get($this->file, 'test');
  57. $this->assertTrue($image->isValid(), 'Image file was parsed.');
  58. return $image;
  59. }
  60. /**
  61. * Assert that all of the specified image toolkit operations were called
  62. * exactly once once, other values result in failure.
  63. *
  64. * @param $expected
  65. * Array with string containing with the operation name, e.g. 'load',
  66. * 'save', 'crop', etc.
  67. */
  68. public function assertToolkitOperationsCalled(array $expected) {
  69. // If one of the image operations is expected, apply should be expected as
  70. // well.
  71. $operations = [
  72. 'resize',
  73. 'rotate',
  74. 'crop',
  75. 'desaturate',
  76. 'create_new',
  77. 'scale',
  78. 'scale_and_crop',
  79. 'my_operation',
  80. 'convert',
  81. ];
  82. if (count(array_intersect($expected, $operations)) > 0 && !in_array('apply', $expected)) {
  83. $expected[] = 'apply';
  84. }
  85. // Determine which operations were called.
  86. $actual = array_keys(array_filter($this->imageTestGetAllCalls()));
  87. // Determine if there were any expected that were not called.
  88. $uncalled = array_diff($expected, $actual);
  89. if (count($uncalled)) {
  90. $this->assertTrue(FALSE, new FormattableMarkup('Expected operations %expected to be called but %uncalled was not called.', ['%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled)]));
  91. }
  92. else {
  93. $this->assertTrue(TRUE, new FormattableMarkup('All the expected operations were called: %expected', ['%expected' => implode(', ', $expected)]));
  94. }
  95. // Determine if there were any unexpected calls.
  96. // If all unexpected calls are operations and apply was expected, we do not
  97. // count it as an error.
  98. $unexpected = array_diff($actual, $expected);
  99. if (count($unexpected) && (!in_array('apply', $expected) || count(array_intersect($unexpected, $operations)) !== count($unexpected))) {
  100. $this->assertTrue(FALSE, new FormattableMarkup('Unexpected operations were called: %unexpected.', ['%unexpected' => implode(', ', $unexpected)]));
  101. }
  102. else {
  103. $this->assertTrue(TRUE, 'No unexpected operations were called.');
  104. }
  105. }
  106. /**
  107. * Resets/initializes the history of calls to the test toolkit functions.
  108. */
  109. protected function imageTestReset() {
  110. // Keep track of calls to these operations
  111. $results = [
  112. 'parseFile' => [],
  113. 'save' => [],
  114. 'settings' => [],
  115. 'apply' => [],
  116. 'resize' => [],
  117. 'rotate' => [],
  118. 'crop' => [],
  119. 'desaturate' => [],
  120. 'create_new' => [],
  121. 'scale' => [],
  122. 'scale_and_crop' => [],
  123. 'convert' => [],
  124. ];
  125. \Drupal::state()->set('image_test.results', $results);
  126. }
  127. /**
  128. * Gets an array of calls to the test toolkit.
  129. *
  130. * @return array
  131. * An array keyed by operation name ('parseFile', 'save', 'settings',
  132. * 'resize', 'rotate', 'crop', 'desaturate') with values being arrays of
  133. * parameters passed to each call.
  134. */
  135. protected function imageTestGetAllCalls() {
  136. return \Drupal::state()->get('image_test.results') ?: [];
  137. }
  138. }