annotate core/tests/Drupal/Tests/Component/Utility/RectangleTest.php @ 19:fa3358dc1485 tip

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