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