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 if (method_exists($this, 'expectException')) {
21 $this->expectException(\InvalidArgumentException::class);
24 $this->setExpectedException(\InvalidArgumentException::class);
26 $rect = new Rectangle(-40, 20);
30 * Tests wrong rectangle height.
34 public function testWrongHeight() {
35 if (method_exists($this, 'expectException')) {
36 $this->expectException(\InvalidArgumentException::class);
39 $this->setExpectedException(\InvalidArgumentException::class);
41 $rect = new Rectangle(40, 0);
45 * Tests getting rectangle dimensions after a rotation operation.
48 * The width of the rectangle.
50 * The height of the rectangle.
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.
59 * @covers ::getBoundingWidth
60 * @covers ::getBoundingHeight
62 * @dataProvider providerPhp55RotateDimensions
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());
72 * Provides data for image dimension rotation tests.
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
77 * - second, for 500 random float rotation angle in the range -360 to 360 on
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
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.
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);
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
108 * @see testRotateDimensions()
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'));