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