Browse Source

Image tests: validate rotate() via getColorAt()

Fabian Peter Hammerle 6 years ago
parent
commit
3cff6eab5e
2 changed files with 77 additions and 8 deletions
  1. 1 1
      colors/RGBA.php
  2. 76 7
      tests/ImageTest.php

+ 1 - 1
colors/RGBA.php

@@ -21,7 +21,7 @@ class RGBA extends RGB
 
     /**
      * alpha 0 => 100% transparency
-     * alpha 1 => 0% transparency, opaque
+     * alpha 1 => opaque, 0% transparency
      */
     public function setAlpha($alpha)
     {

+ 76 - 7
tests/ImageTest.php

@@ -38,22 +38,91 @@ class ImageTest extends \PHPUnit_Framework_TestCase
     public function rotateProvider()
     {
         return [
-            [__DIR__ . '/data/chainring.jpg', 90, __DIR__ . '/data/chainring-rotated-left.jpg'],
-            [__DIR__ . '/data/chainring.jpg', 270, __DIR__ . '/data/chainring-rotated-right.jpg'],
+            [
+                __DIR__ . '/data/color.png',
+                0,
+                [
+                    [0, 0, new colors\RGBA(0, 1, 0, 1)],
+                    [1, 0, new colors\RGBA(0, 0, 1, 1)],
+                    [3, 0, new colors\RGBA(0, 0, 0, 0)],
+                    [0, 1, new colors\RGBA(1, 0, 0, 1)],
+                    [1, 1, new colors\RGBA(0, 0, 0, 1)],
+                    [3, 2, new colors\RGBA(1, 1, 1, 1)],
+                    ],
+                ],
+            [
+                __DIR__ . '/data/color.png',
+                90,
+                [
+                    [0, 3, new colors\RGBA(0, 1, 0, 1)],
+                    [0, 2, new colors\RGBA(0, 0, 1, 1)],
+                    [0, 0, new colors\RGBA(0, 0, 0, 0)],
+                    [1, 3, new colors\RGBA(1, 0, 0, 1)],
+                    [1, 2, new colors\RGBA(0, 0, 0, 1)],
+                    [2, 0, new colors\RGBA(1, 1, 1, 1)],
+                    ],
+                ],
+            [
+                __DIR__ . '/data/color.png',
+                180,
+                [
+                    [3, 2, new colors\RGBA(0, 1, 0, 1)],
+                    [2, 2, new colors\RGBA(0, 0, 1, 1)],
+                    [0, 2, new colors\RGBA(0, 0, 0, 0)],
+                    [3, 1, new colors\RGBA(1, 0, 0, 1)],
+                    [2, 1, new colors\RGBA(0, 0, 0, 1)],
+                    [0, 0, new colors\RGBA(1, 1, 1, 1)],
+                    ],
+                ],
+            [
+                __DIR__ . '/data/color.png',
+                270,
+                [
+                    [2, 0, new colors\RGBA(0, 1, 0, 1)],
+                    [2, 1, new colors\RGBA(0, 0, 1, 1)],
+                    [2, 3, new colors\RGBA(0, 0, 0, 0)],
+                    [1, 0, new colors\RGBA(1, 0, 0, 1)],
+                    [1, 1, new colors\RGBA(0, 0, 0, 1)],
+                    [0, 3, new colors\RGBA(1, 1, 1, 1)],
+                    ],
+                ],
+            [
+                __DIR__ . '/data/color.png',
+                360,
+                [
+                    [0, 0, new colors\RGBA(0, 1, 0, 1)],
+                    [1, 0, new colors\RGBA(0, 0, 1, 1)],
+                    [3, 0, new colors\RGBA(0, 0, 0, 0)],
+                    [0, 1, new colors\RGBA(1, 0, 0, 1)],
+                    [1, 1, new colors\RGBA(0, 0, 0, 1)],
+                    [3, 2, new colors\RGBA(1, 1, 1, 1)],
+                    ],
+                ],
+            [
+                __DIR__ . '/data/color.png',
+                360 + 90,
+                [
+                    [0, 3, new colors\RGBA(0, 1, 0, 1)],
+                    [0, 2, new colors\RGBA(0, 0, 1, 1)],
+                    [0, 0, new colors\RGBA(0, 0, 0, 0)],
+                    [1, 3, new colors\RGBA(1, 0, 0, 1)],
+                    [1, 2, new colors\RGBA(0, 0, 0, 1)],
+                    [2, 0, new colors\RGBA(1, 1, 1, 1)],
+                    ],
+                ],
             ];
     }
 
     /**
      * @dataProvider rotateProvider
      */
-    public function testRotate($source_path, $angle, $expected_path)
+    public function testRotate($source_path, $angle, $expected_pixels)
     {
         $img = Image::fromFile($source_path);
-        $tmp_path = tempnam(sys_get_temp_dir(), 'image');
         $img->rotate($angle);
-        $img->saveJpeg($tmp_path);
-        $this->assertFileEquals($expected_path, $tmp_path);
-        unlink($tmp_path);
+        foreach($expected_pixels as $px) {
+            $this->assertTrue($px[2]->equals($img->getColorAt($px[0], $px[1])));
+        }
     }
 
     public function rotateLeftProvider()