be3cdd75eb02341ee466c7866a29f0bdc0d94ea1
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Utility / ImageTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Utility;
4
5 use Drupal\Component\Utility\Image;
6 use PHPUnit\Framework\TestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Component\Utility\Image
10  * @group Image
11  */
12 class ImageTest extends TestCase {
13
14   /**
15    * Tests all control flow branches in image_dimensions_scale().
16    *
17    * @dataProvider providerTestScaleDimensions
18    */
19   public function testScaleDimensions($input, $output) {
20     // Process the test dataset.
21     $return_value = Image::scaleDimensions($input['dimensions'], $input['width'], $input['height'], $input['upscale']);
22
23     // Check the width.
24     $this->assertEquals($output['dimensions']['width'], $input['dimensions']['width'], sprintf('Computed width (%s) does not equal expected width (%s)', $output['dimensions']['width'], $input['dimensions']['width']));
25
26     // Check the height.
27     $this->assertEquals($output['dimensions']['height'], $input['dimensions']['height'], sprintf('Computed height (%s) does not equal expected height (%s)', $output['dimensions']['height'], $input['dimensions']['height']));
28
29     // Check the return value.
30     $this->assertEquals($output['return_value'], $return_value, 'Incorrect return value.');
31   }
32
33   /**
34    * Provides data for image dimension scale tests.
35    *
36    * @return array
37    *   Keyed array containing:
38    *   - 'input' - Array which contains input for Image::scaleDimensions().
39    *   - 'output' - Array which contains expected output after passing
40    *     through Image::scaleDimensions. Also contains a boolean
41    *     'return_value' which should match the expected return value.
42    *
43    * @see testScaleDimensions()
44    */
45   public function providerTestScaleDimensions() {
46     // Define input / output datasets to test different branch conditions.
47     $tests = [];
48
49     // Test branch conditions:
50     // - No height.
51     // - Upscale, don't need to upscale.
52     $tests[] = [
53       'input' => [
54         'dimensions' => [
55           'width' => 1000,
56           'height' => 2000,
57         ],
58         'width' => 200,
59         'height' => NULL,
60         'upscale' => TRUE,
61       ],
62       'output' => [
63         'dimensions' => [
64           'width' => 200,
65           'height' => 400,
66         ],
67         'return_value' => TRUE,
68       ],
69     ];
70
71     // Test branch conditions:
72     // - No width.
73     // - Don't upscale, don't need to upscale.
74     $tests[] = [
75       'input' => [
76         'dimensions' => [
77           'width' => 1000,
78           'height' => 800,
79         ],
80         'width' => NULL,
81         'height' => 140,
82         'upscale' => FALSE,
83       ],
84       'output' => [
85         'dimensions' => [
86           'width' => 175,
87           'height' => 140,
88         ],
89         'return_value' => TRUE,
90       ],
91     ];
92
93     // Test branch conditions:
94     // - Source aspect ratio greater than target.
95     // - Upscale, need to upscale.
96     $tests[] = [
97       'input' => [
98         'dimensions' => [
99           'width' => 8,
100           'height' => 20,
101         ],
102         'width' => 200,
103         'height' => 140,
104         'upscale' => TRUE,
105       ],
106       'output' => [
107         'dimensions' => [
108           'width' => 56,
109           'height' => 140,
110         ],
111         'return_value' => TRUE,
112       ],
113     ];
114
115     // Test branch condition: target aspect ratio greater than source.
116     $tests[] = [
117       'input' => [
118         'dimensions' => [
119           'width' => 2000,
120           'height' => 800,
121         ],
122         'width' => 200,
123         'height' => 140,
124         'upscale' => FALSE,
125       ],
126       'output' => [
127         'dimensions' => [
128           'width' => 200,
129           'height' => 80,
130         ],
131         'return_value' => TRUE,
132       ],
133     ];
134
135     // Test branch condition: don't upscale, need to upscale.
136     $tests[] = [
137       'input' => [
138         'dimensions' => [
139           'width' => 100,
140           'height' => 50,
141         ],
142         'width' => 200,
143         'height' => 140,
144         'upscale' => FALSE,
145       ],
146       'output' => [
147         'dimensions' => [
148           'width' => 100,
149           'height' => 50,
150         ],
151         'return_value' => FALSE,
152       ],
153     ];
154
155     return $tests;
156   }
157
158 }