12

I am new to using Mockery and confused with the terminology alias and overload. Can anyone please explain to me when to use which?

Hyder B.
  • 10,900
  • 5
  • 51
  • 60

3 Answers3

34

Overload is used to create an "instance mock". This will "intercept" when a new instance of a class is created and the mock will be used instead. For example if this code is to be tested:

class ClassToTest {

    public function methodToTest()
    {
        $myClass = new MyClass();
        $result = $myClass->someMethod();
        return $result;
    }
}

You would create an instance mock using overload and define the expectations like this:

 public function testMethodToTest()
 {
     $mock = Mockery::mock('overload:MyClass');
     $mock->shouldreceive('someMethod')->andReturn('someResult');

     $classToTest = new ClassToTest();
     $result = $classToTest->methodToTest();

     $this->assertEquals('someResult', $result);
 }

Alias is used to mock public static methods. For example if this code is to be tested:

class ClassToTest {

    public function methodToTest()
    {
        return MyClass::someStaticMethod();
    }
}

You would create an alias mock using alias and define the expectations like this:

public function testNewMethodToTest()
{
    $mock = Mockery::mock('alias:MyClass');
    $mock->shouldreceive('someStaticMethod')->andReturn('someResult');

    $classToTest = new ClassToTest();
    $result = $classToTest->methodToTest();

    $this->assertEquals('someResult', $result);
}
Fredrik Schöld
  • 1,588
  • 13
  • 20
2

Additional info to Fredrik Schöld's answer: Alias mocking is persistent per test cases, so you need to deactivate it each time you use it. Just add this phpDoc to each test class there you use alias mocking:

/**
 * @runTestsInSeparateProcesses
 * @preserveGlobalState disabled
 */

Update. On PHPUnit 9+ the correct annotation is (from brunofarias' comment):

/**
 * @runInSeparateProcess
 * @preserveGlobalState disabled
 */
Community
  • 1
  • 1
Serhiy Gavka
  • 21
  • 1
  • 4
-1

I found this: https://github.com/padraic/mockery-docs/blob/master/reference/startup_methods.rst

$mock = \Mockery::mock('alias:MyNamespace\MyClass');

Prefixing the valid name of a class (which is NOT currently loaded) with "alias:" will generate an "alias mock". Alias mocks create a class alias with the given classname to stdClass and are generally used to enable the mocking of public static methods. Expectations set on the new mock object which refer to static methods will be used by all static calls to this class.

$mock = \Mockery::mock('overload:MyNamespace\MyClass');

Prefixing the valid name of a class (which is NOT currently loaded) with "overload:" will generate an alias mock (as with "alias:") except that created new instances of that class will import any expectations set on the origin mock ($mock). The origin mock is never verified since it's used an expectation store for new instances. For this purpose we use the term "instance mock" to differentiate it from the simpler "alias mock".

So to me it seems that overload does the same as alias with the difference that it also imports expectations from the origin mock.

R3KHYT
  • 11
  • 2