3

I am new to TDD. I have a simple class method

private function getSelfSubmissionMsg($type)
{
    switch ($type) {
        case 'efl':
            return 'Some message.';
        case 'split':
            return 'Some other message.';
    }
}

How can I test this method using phpunit

Ashish
  • 647
  • 7
  • 18

2 Answers2

2

You do not need a test for each function, instead you should focus on getting tests for all execution paths. So there is no need to test individual private functions, since they should be part of the implementation details and only are a sub part of the unit you are testing.

If you test all private functions you will have to update the tests every time you refactor the code, but if you test the code contracts (public methods) you can refactor the implementation details all you want while having the tests show you if you by accident change the contract.

Ps. it is normal to want to test private methods when starting out with TDD, but you will soon experience that it is not necessary

rypskar
  • 2,012
  • 13
  • 13
1

Here's a sample test case:

<?php
use PHPUnit\Framework\TestCase;

class SumissionsTest extends TestCase
{
    public function testSelfSubmissionMsg()
    {
        //Whatever your class name is
        $submission = new Submission();
        $elf   = $submission->getSelfSubmissionMsg('elf');
        $split = $submission->getSelfSubmissionMsg('split');
        $this->assertEquals($elf, 'Some message.');
        $this->assertEquals($split, 'Some other message.');
    }
}
Blue
  • 22,608
  • 7
  • 62
  • 92
  • The method getSelfSubmissionMsg() is private. And what changes are needed if the method is in Trait ? – Ashish Aug 08 '16 at 05:39
  • See [this](https://jtreminio.com/2013/03/unit-testing-tutorial-part-3-testing-protected-private-methods-coverage-reports-and-crap/) article for debugging private methods – Blue Aug 08 '16 at 05:44