4

I've read some people do test them and some people don't. When would you unit test a getter/setter?

If I would like to skip unittesting getters and setters, how would I manage that with PHPUnit (skip ALL getters and setters)?

3 Answers 3

9

Test the methods that enclose business logic. A setter is usually just an assignment, a getter most of the times just returns a property of the object or, sometimes, a hard-coded constant. Visual inspection during pair-programming and code review should be all the test these methods need.

PHPUnit doesn't test anything. It is just a tool that helps you write tests and run them. You write the tests, you decide what methods you test in each test.

If your question regarding including/excluding methods is about code-coverage then, imho, you are on the wrong path. Code coverage is not a goal by itself and it doesn't show anything. It's easy to have 100% code coverage and a successful test suite that doesn't, actually, test the code.

Make a goal on writing tests that describe the behaviour of the code. Don't use setters for required object properties; add as many constructor parameters you need to initialize the object. Use setters only for optional properties. Use as less getters as needed. Encapsulate the behaviour specific to the object in its methods, don't use the objects the same way you use arrays (just to store values).

7

It really depends on what you have in those setters and getters but, to me, it really makes sense to test them even if they are as simple as setting a private/protected property.

Your program has an expected/desired behaviour given a certain input and unit tests make sure that there will be no regression issues.

For example, given a class with concrete setters/getters, you will be tempted to copy paste some of the methods and you might just forget to change the property name in one of the setters ending with 2 different setters for the same value:

class User 
{
    private $firstName;
    private $lastName;

    public function setFirstName($value) 
    {
        $this->firstName = $value;
    }

    public function getFirstName() 
    {
        return $this->firstName;
    }

    public function setLastName($value) 
    {
        $this->firstName = $value;
    }

    public function getLastName() 
    {
        return $this->lastName;
    }
}

and your test could be something like

public function testSetLastName_validValue_successful() 
{
    $user = new User;
    $value = 'Doe';
    $user->setLastName($value);
    $this->assertEquals($value, $user->getLastName());
}

This will immediately show the error in setLastName and it's a nice example of how TDD helps :)

Writing a few very simple unit tests for each setter/getter pair would save you from some annoyance.

Even When using overloading, unit tests are useful to ensure the domain of the magic setters/getters.

As for 'how to skip all getters and setters' ... just don't write tests for them ?

2
  • 3
    You are wasting time and money with such approach. If you really want to test getter/setter, you should write some utility to automate this with introspection.
    – Toilal
    Feb 2, 2018 at 8:18
  • @toilal: I disagree, these tests are extremely fast to write, and they can prevent copy/paste errors. In the current project I am working on, I found several bugs with these kinds of tests. You never waste time and money writing tests, you are when you have a bug in production because of a stupid copy/paste error. Even they seem useless, they are still a safeguard.
    – COil
    Oct 26, 2022 at 5:50
0

You can also do this in a one-liner, so the tests are concise and test both getters and setters at the same time. In this case, your setters should implement the fluent interface:

public function testUserSetLastNameSuccessful() 
{
    $this->assertEquals('Doe', (new User())->setLastName('Doe')->getLastName());
}

The new getter:

public function setLastName($value): self
{
  $this->lastName = $value;

  return $this;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.