49d08333ef4cecd2d9e5161e6b0298895527cfc9
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Utility / RectangleTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Utility;
4
5 use Drupal\Component\Utility\Rectangle;
6 use PHPUnit\Framework\TestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Component\Utility\Rectangle
10  * @group Image
11  */
12 class RectangleTest extends TestCase {
13
14   /**
15    * Tests wrong rectangle width.
16    *
17    * @covers ::rotate
18    */
19   public function testWrongWidth() {
20     $this->setExpectedException(\InvalidArgumentException::class);
21     $rect = new Rectangle(-40, 20);
22   }
23
24   /**
25    * Tests wrong rectangle height.
26    *
27    * @covers ::rotate
28    */
29   public function testWrongHeight() {
30     $this->setExpectedException(\InvalidArgumentException::class);
31     $rect = new Rectangle(40, 0);
32   }
33
34   /**
35    * Tests getting rectangle dimensions after a rotation operation.
36    *
37    * @param int $width
38    *   The width of the rectangle.
39    * @param int $height
40    *   The height of the rectangle.
41    * @param float $angle
42    *   The angle for rotation.
43    * @param int $exp_width
44    *   The expected width of the rotated rectangle.
45    * @param int $exp_height
46    *   The expected height of the rotated rectangle.
47    *
48    * @covers ::rotate
49    * @covers ::getBoundingWidth
50    * @covers ::getBoundingHeight
51    *
52    * @dataProvider providerPhp55RotateDimensions
53    */
54   public function testRotateDimensions($width, $height, $angle, $exp_width, $exp_height) {
55     $rect = new Rectangle($width, $height);
56     $rect->rotate($angle);
57     $this->assertEquals($exp_width, $rect->getBoundingWidth());
58     $this->assertEquals($exp_height, $rect->getBoundingHeight());
59   }
60
61   /**
62    * Provides data for image dimension rotation tests.
63    *
64    * This dataset sample was generated by running on PHP 5.5 the function below
65    * - first, for all integer rotation angles (-360 to 360) on a rectangle
66    *   40x20;
67    * - second, for 500 random float rotation angle in the range -360 to 360 on
68    *   a rectangle 40x20;
69    * - third, on 1000 rectangles of random WxH rotated to a random float angle
70    *   in the range -360 to 360
71    * - fourth, on 2000 rectangles of random WxH rotated to a random integer
72    *   angle multiple of 30 degrees in the range -360 to 360 (which is the most
73    *   tricky case).
74    * Using the GD toolkit operations gives us true data coming from the GD
75    * library that can be used to match against the Rectangle class under test.
76    * @code
77    *   protected function rotateResults($width, $height, $angle, &$new_width, &$new_height) {
78    *     $image = \Drupal::service('image.factory')->get(NULL, 'gd');
79    *     $image->createNew($width, $height);
80    *     $old_res = $image->getToolkit()->getResource();
81    *     $image->rotate($angle);
82    *     $new_width = $image->getWidth();
83    *     $new_height = $image->getHeight();
84    *     if (is_resource($old_res)) {
85    *       imagedestroy($old_res);
86    *     }
87    *   }
88    * @endcode
89    *
90    * @return array[]
91    *   A simple array of simple arrays, each having the following elements:
92    *   - original image width
93    *   - original image height
94    *   - rotation angle in degrees
95    *   - expected image width after rotation
96    *   - expected image height after rotation
97    *
98    * @see testRotateDimensions()
99    */
100   public function providerPhp55RotateDimensions() {
101     // The dataset is stored in a .json file because it is very large and causes
102     // problems for PHPCS.
103     return json_decode(file_get_contents(__DIR__ . '/fixtures/RectangleTest.json'));
104   }
105
106 }