1
\$\begingroup\$

I made a class that will be used to get an image properties like width, height, and MIME type.

<?php
namespace Resizer;

class ImageResizer
{
    protected $imageOrig = null;
    protected $width = null;
    protected $height = null;
    protected $mime = null;

    public function loadImage($path = null)
    {
        $data = getimagesize($path);
        $this->imageOrig = $data;
        $this->width = $data[0];
        $this->height = $data[1];
        $this->mime = $data['mime'];
    }

    public function getOriginalImage()
    {
        return $this->imageOrig;
    }

    public function getWidth()
    {
        return $this->width;
    }

    public function getHeight()
    {
        return $this->height;
    }

    public function getMime()
    {
        return $this->mime;
    }
}

And then I created tests with PHPUnit

<?php
use Resizer\ImageResizer;

class ImageResizerTest extends PHPUnit_Framework_TestCase
{
    public function testLoadImageFromPathReturnsImage()
    {
        $expected = getimagesize('./assets/tingey.jpg');
        $resizer = new ImageResizer();
        $resizer->loadImage('./assets/tingey.jpg');

        $this->assertEquals($expected, $resizer->getOriginalImage());
    }

    public function testGetImageReturnsImageData()
    {
        $resizer = new ImageResizer();
        $resizer->loadImage('./assets/tingey.jpg');
        $expected = getimagesize('./assets/tingey.jpg');
        $imageOrig = $resizer->getOriginalImage();
        $this->assertEquals($expected, $imageOrig);
    }

    public function testGetWidthReturnsImageWidth()
    {
        $resizer = new ImageResizer();
        $resizer->loadImage('./assets/tingey.jpg');
        $expected = getimagesize('./assets/tingey.jpg');
        $this->assertEquals($expected[0], $resizer->getWidth());
    }

    public function testGetHeightReturnsImageHeight()
    {
        $resizer = new ImageResizer();
        $resizer->loadImage('./assets/tingey.jpg');
        $expected = getimagesize('./assets/tingey.jpg');
        $this->assertEquals($expected[1], $resizer->getHeight());
    }

    public function testGetMimeReturnsImageMime()
    {
        $resizer = new ImageResizer();
        $resizer->loadImage('./assets/tingey.jpg');
        $expected = getimagesize('./assets/tingey.jpg');
        $this->assertEquals($expected['mime'], $resizer->getMime());
    }
}

Am I doing anything wrong in my unit testing? What can I do better?

\$\endgroup\$
  • \$\begingroup\$ I think you should also show implementation of getimagesize function used in your tests, or at least give more information about it. I would normally write things like image sizes as fixed in tests, or use some function helpers or fixtures. \$\endgroup\$ – vfsoraki Dec 18 '16 at 6:01
  • \$\begingroup\$ Probably getimagesize reads the image file. So probably this function should be mocked, because unit tests should not touch the file system. I am not sure if its possible to mock function like this, if its not, then could create a wrapper class which has same method. \$\endgroup\$ – Darius.V Feb 12 '17 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.