5

Target.php

<?php

class Target
{
    public function validate()
    {
        $this->getData();
        return true;
    }

    public function getData()
    {
        return array();
    }
}

TargetTest.php

<?php

class TargetTest extends PHPUnit_Framework_TestCase
{
    public function testValidate()
    {
        $mock = m::mock('Target');
        $mock->shouldReceive('getData')
            ->once();
        $expected = $this->exp->validate();

        $this->assertTrue($expected);
    }
}

result

Mockery\Exception\InvalidCountException: Method getData() from Mockery_1_ExpWarning should be called exactly 1 times but called 0 times.

I use Mockery as mock tool, the example always about how to mock with DI, I would like to know can I mock internal method?

Matteo
  • 37,680
  • 11
  • 100
  • 115
Chan
  • 1,947
  • 6
  • 25
  • 37
  • your TargetTest class is little bit confused. Is the ExpWarning the real class name of Target class? Who his $this->exp object? why not $mock? please explain better – Matteo Sep 22 '15 at 04:56

1 Answers1

3

You can use the Partial Mocks features of the testing framework for mock only the getData method and describe an expectation.

As (working) Example:

use Mockery as m;

class TargetTest extends \PHPUnit_Framework_TestCase
{
    public function testValidate()
    {
        $mock = m::mock('Target[getData]');
        $mock->shouldReceive('getData')
            ->once();
        $expected = $mock->validate();

        $this->assertTrue($expected);
    }
}

Hope this help

Matteo
  • 37,680
  • 11
  • 100
  • 115