annotate core/tests/Drupal/Tests/Component/Utility/RectangleTest.php @ 0:4c8ae668cc8c

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