3 namespace Drupal\Tests\Component\Utility;
5 use Drupal\Component\Utility\Rectangle;
6 use PHPUnit\Framework\TestCase;
9 * @coversDefaultClass \Drupal\Component\Utility\Rectangle
12 class RectangleTest extends TestCase {
15 * Tests wrong rectangle width.
19 public function testWrongWidth() {
20 $this->setExpectedException(\InvalidArgumentException::class);
21 $rect = new Rectangle(-40, 20);
25 * Tests wrong rectangle height.
29 public function testWrongHeight() {
30 $this->setExpectedException(\InvalidArgumentException::class);
31 $rect = new Rectangle(40, 0);
35 * Tests getting rectangle dimensions after a rotation operation.
38 * The width of the rectangle.
40 * The height of the rectangle.
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.
49 * @covers ::getBoundingWidth
50 * @covers ::getBoundingHeight
52 * @dataProvider providerPhp55RotateDimensions
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());
62 * Provides data for image dimension rotation tests.
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
67 * - second, for 500 random float rotation angle in the range -360 to 360 on
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
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.
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);
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
98 * @see testRotateDimensions()
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'));