box / shmock Goto Github PK
View Code? Open in Web Editor NEWSHorthand for MOCKing in PHPUnit
License: Apache License 2.0
SHorthand for MOCKing in PHPUnit
License: Apache License 2.0
PHP Fatal error: Class Shmock\InvocationImpl contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (PHPUnit_Framework_MockObject_Invocation::generateReturnValue) in /vendor/box/shmock/src/Shmock/StaticSpec.php on line 572
e.g.
class MagicMethodTest
{
/**
* @param string $methodName
* @param array $argument
*
* @return string
*/
public function __call($methodName, array $argument)
{
return $methodName;
}
}
class MagicMethodUsage
{
/**
* @param \Box\PHPUnitTestGenerator\tests\_fixture\_input\MagicMethodTest $magicTest
*
* @return string
*/
public function callMagicMethod(
MagicMethodTest $magicTest
)
{
$rc = $magicTest->foo();
return $rc;
}
}
<?php
class MagicMethodUsageGenTest extends \PHPUnit_Framework_TestCase
{
use \Shmock\Shmockers;
public function testCallMagicMethod()
{
// Setup mocks for parameters to the method under test.
$mockMagicMethodTest0 = $this->shmock(
'MagicMethodTest',
function (
$shmock
) {
// $shmock->dont_preserve_original_methods();
/** @var $mock \Shmock\Spec */
$mock = $shmock->__call('foo', array (
));
$mock->return_value('bar');
}
);
// Execute the method under test.
$objectUnderTest = new MagicMethodUsage();
$executionResult = $objectUnderTest->callMagicMethod($mockMagicMethodTest0);
// Validate the execution result.
$expected = 'bar';
$this->assertSame(
$expected,
$executionResult,
'Variable ( executionResult ) doesn\'t have the expected value.'
);
}
}
Here is an example that illustrates the issue:
shmock_class( 'Box_Helper_URL', function ( $shmock /** @var Box_Helper_URL|\Shmock\ClassBuilderStaticClass $shmock */ ) { // Keep track of the order of calls made on this mock. $shmock->order_matters(); // Mock all methods, return null by default unless overwritten by the expectations below. $shmock->dont_preserve_original_methods(); $shmock->disable_original_constructor(); $mock = $shmock->url_is_box('ab.com'); /** @var $mock \Shmock\Spec */ $mock->return_value(true); } ); \Statics::overwrite('Box_Helper_URL', $mockBox_Helper_URL0); // Execute the method under test. $objectUnderTest = new \StaticsUsageExample(); $executionResult = $objectUnderTest->isBoxUrl('ab.com'); // Validate the execution result. $expected = true; $this->assertSame( $expected, $executionResult, 'Variable ( executionResult ) doesn\'t have the expected value.' ); ``` } } The test failed. The url_is_box call on the mock object returned null instead of true as I expected. # Here is my analysis of the issue: This is what I found that caused the failure: dont_preserve_original_methods calls $this->__call($method->getName(), [])->any()->return_null(); to overwrite the static method calls of the class being mocked. This has the same effect as specifying $shmock->url_is_box()->any()->return_value(null); in the mock object expectations. Since order_matter is set, in effect the expectation becomes that url_is_box will be called once first and return null and it should be called again and this time return true. This effect is not what I expected. And there is no documentation that I can find that describes this behavior. The non static class builder has a different implementation and doesn't seem to exhibit this behavior.I am using this library in my project called TestScribe
Because shmock currently requires 1.1 version of diff component, I got the following error when trying to use a newer version of phpunit.
Output below:
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Conclusion: remove sebastian/diff 1.1.0
- box/shmock 2.0.0-alpha6 requires sebastian/diff 1.1 -> satisfiable by sebastian/diff[1.1.0].
- box/shmock 2.0.0-alpha6 requires sebastian/diff 1.1 -> satisfiable by sebastian/diff[1.1.0].
- Conclusion: don't install sebastian/diff 1.1.0|keep sebastian/diff 1.1.0
- Installation request for box/shmock 2.0.0-alpha6 -> satisfiable by box/shmock[2.0.0-alpha6].
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.