Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 553831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:38:46+00:00 2026-05-13T11:38:46+00:00

I’m trying to get into Unit testing for the obvious positives it introduces, and

  • 0

I’m trying to get into Unit testing for the obvious positives it introduces, and I’m trying to write a Unit test for a class I wrote the other day. (I know this is the opposite to TDD, please bear with me)

My class, Image, is used in conjunction with some others for image manipulation.

Image essentially wraps a GD image resource and stores data along with it. For example, an instance of Image will always contain it’s current state, i.e. its new width/height if resized, the original image data, etc.

The Image class also contains methods for,

  • Creating itself from a file, string data, or URL, e.g. $image->loadFromPath()
  • Creating a new GD image resource from the properties of the current Image instance, e.g. for image resizing to maintain background transparency, etc.
  • Cloning the GD image resource for use in the manipulation classes

What I’m struggling with is how to Unit test this class properly with PHPUnit. I’ve done some reading and I have a few conflicting ideas on how to approach it and I don’t know what’s right. Do I,

  1. Write a test for each method of the class. I read somewhere that I should test each and every method. However, some of the methods run others (rightly so may I add), so you then have a chain of dependency. But I also read that each Unit test should be independent from the other. So what do I do if this is the case?
  2. Write each test as a usage route of the class. I also read somewhere that each test should instead represent 1 path/usage route you can take with the class. Therefore if you cover every usage, you’ll ultimately get complete code coverage.

So, which of these is correct, if any?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:38:47+00:00Added an answer on May 13, 2026 at 11:38 am

    Unit tests should be written to evaluate the public interface of a class. Your test case should use the class as you intend it to be used in your program. The idea here is to test the behavior (either expected, unexpected, or edge conditions) of the class.

    Both ideas you posted are correct. In theory, you should have enough test cases (routes through your code) that all your methods in the class are run.

    As was mentioned, 100% test coverage is a nice goal, but not always realistic.

    Also, in the case of GD, be careful about writing unit tests that test GD’s functionality (it’s already been tested, you don’t need to waste time testing it again). I would read up on using PHPUnit’s mocks and stubs (and mocking the filesystem) in the PHPUnit manual.

    Here’s what an example test might look like:

    public function testImageIsResized()
    {
        $image = new Image();
        $image->loadFromPath('some/path');
        $image->resize(200, 300);
        $this->assertEquals(200, $image->getWidth());
        $this->assertEquals(300, $image->getHeight());
    }
    

    Now, depending on the expected behavior of the image class, this test might pass without issue, or it might fail because it was expecting the new dimensions to be proportionally constrained to the original image dimensions. But we did not explicitly call the internal method that checks for that constraint in the test itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.