James Dickinson
James Dickinson

Reputation: 75

Private function error with PHPUnit testing

I am getting the following error in my PHPunit test

Warning: Invalid callback Vc_Manager::__sleep, cannot access private method Vc_Manager::__sleep()

This is caused because the function __sleep() is private and the __contruct() function for the class is also private.

How can I using this function in my phpunit testing without turning the function public?

Full code:

require_once('../splitTestNew.php');



class SplitTestTest extends PHPUnit_Framework_TestCase
{


    public function setUp(){

    }
    public function tearDown(){ }
    /**
     * @runInSeparateProcess
     */
    public function testDefaultParameterValues()
    {

        $obj = new BSSplitTest();
        $this->assertTrue($obj->params['A'] == 50);
        $this->assertTrue($obj->params['B'] == 50);
        $this->assertTrue($obj->params['SplitCookieA'] == 'ACounter');
        $this->assertTrue($obj->params['SplitCookieB'] == 'BCounter');
        $this->assertTrue($obj->params['TableA'] == 'counter_a');
        $this->assertTrue($obj->params['TableB'] == 'counter_b');

    }

Upvotes: 0

Views: 510

Answers (2)

walther
walther

Reputation: 13600

We NEVER test private methods, it doesn't make any sense. You either need to make the function public or test functions that use the private method.

Also, if the constructor is private too, you first need to make an instance of the class (for instance using Factory pattern) and then invoke a function.

Upvotes: 0

the-noob
the-noob

Reputation: 1342

If your constructor is private then you can't have

 $obj = new BSSplitTest();

That implies your constructor is public, you'll need a static factory constructor.

 $obj = BSSplitTest::getInstance(); //or createInstance if not singleton 

Upvotes: 1

Related Questions