What is the difference between overloading and alias in Mockery?
Overload
used to create a "layout instance". This will "intercept" when a new instance of the class is instantiated 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 will instantiate mock with Overload
and define 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
used to mock public static methods. For example, if this code is to be tested:
class ClassToTest {
public function methodToTest()
{
return MyClass::someStaticMethod();
}
}
You should create a mock alias with Alias
and define 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);
}
source to share
I found this: https://github.com/padraic/mockery-docs/blob/master/reference/startup_methods.rst
$mock = \Mockery::mock('alias:MyNamespace\MyClass');
Prefixing a valid class name (which is not currently loaded) with "alias:" will generate a "mock alias". Alias mocks create an alias for a class with a given class in stdClass and are commonly used to include public static methods in mocks. Expectations set on the new mock object that refer to static methods will be used by all static calls to this class.
$mock = \Mockery::mock('overload:MyNamespace\MyClass');
Prefixing a valid class name (which is not currently loaded) with "overload:" will generate a mock alias (as with "alias:"), except that newly created instances of this class will import any expectations set on the origin mock ($ mock). The original layout is never checked out as it used the wait store for new instances. For this, we use the term "layout instance" to distinguish it from the simpler "alias".
So it seems to me that the overload does exactly the same as the alias with the difference that it also imports expectations from the origin layout.
source to share