We will work on Apr 26th (Saturday) and will be off from Apr 30th (Wednesday) until May 2nd (Friday) for public holiday in our country

Commit 87b5ac9c authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

! Initial version of mock proxy and helpers

parent 6f8b629a
......@@ -423,7 +423,9 @@ class EcomDev_PHPUnit_Test_Case_Util
*/
public static function replaceByMock($type, $classAlias, $mock)
{
if ($mock instanceof PHPUnit_Framework_MockObject_MockBuilder) {
if ($mock instanceof EcomDev_PHPUnit_Mock_Proxy) {
$mock = $mock->getMockInstance();
} elseif ($mock instanceof PHPUnit_Framework_MockObject_MockBuilder) {
$mock = $mock->getMock();
} elseif (!$mock instanceof PHPUnit_Framework_MockObject_MockObject) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(
......
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
class EcomDev_PHPUnitTest_Test_Lib_Helper_Abstract extends PHPUnit_Framework_TestCase
{
/**
*
*
* @var EcomDev_PHPUnit_Helper_Abstract|PHPUnit_Framework_MockObject_MockObject
*/
protected $helper = null;
protected function setUp()
{
$this->helper = $this->getMockBuilder('EcomDev_PHPUnit_Helper_Abstract')
->setMethods(array('hasMethod', 'callMethod'))
->enableArgumentCloning()
->getMockForAbstractClass();
}
/**
* Creates stub for phpunit method
*
* @param $map
* @return $this
*/
protected function hasMethodStub($map)
{
$stubMap = array();
$stubResult = array();
foreach ($map as $method => $result) {
$stubMap[] = array($method, $result !== false);
if ($result instanceof PHPUnit_Framework_MockObject_Stub) {
$stubResult[$method] = $result;
}
}
$this->helper->expects($this->any())
->method('hasMethod')
->will($this->returnValueMap($stubMap));
$helper = $this->helper;
$this->helper->expects($this->any())
->method('callMethod')
->will($this->returnCallback(function ($method, array $args) use ($helper, $stubResult) {
$invocation = new PHPUnit_Framework_MockObject_Invocation_Object(
get_class($helper), $method, $args, $helper
);
return $stubResult[$method]->invoke($invocation);
}));
return $this;
}
public function testHasAction()
{
$this->hasMethodStub(array(
'helperName' => true,
'helperCamelName' => true,
'helperUnknownName' => false
));
$this->assertTrue($this->helper->has('name'));
$this->assertTrue($this->helper->has('camelName'));
$this->assertFalse($this->helper->has('unknownName'));
}
public function testInvokeAction()
{
$this->hasMethodStub(array(
'helperName' => $this->returnArgument(0),
'helperCamelName' => $this->returnArgument(1),
'helperUnknownName' => false
));
$this->assertSame('value1', $this->helper->invoke('name', array('value1', 'value2')));
$this->assertSame('value2', $this->helper->invoke('camelName', array('value1', 'value2')));
$this->setExpectedException('RuntimeException', 'Helper "unknownName" is not invokable.');
$this->helper->invoke('unknownName', array());
}
public function testSetTestCase()
{
$this->assertObjectHasAttribute('testCase', $this->helper);
$this->helper->setTestCase($this);
$this->assertAttributeSame($this, 'testCase', $this->helper);
}
}
\ No newline at end of file
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
class EcomDev_PHPUnitTest_Test_Lib_Mock_Proxy extends PHPUnit_Framework_TestCase
{
/**
* @var EcomDev_PHPUnit_Mock_Proxy
*/
protected $mockProxy;
/**
*
*/
protected function setUp()
{
$this->mockProxy = new EcomDev_PHPUnit_Mock_Proxy($this, 'EcomDev_PHPUnit_Constraint_Abstract');
$this->mockProxy->disableOriginalConstructor();
}
/**
* Test addition of the method into mock proxy
*
*
*/
public function testAddMethod()
{
$this->assertAttributeEquals(array(), 'methods', $this->mockProxy);
$this->mockProxy->addMethod('methodName');
$this->assertAttributeEquals(array('methodName'), 'methods', $this->mockProxy);
$this->mockProxy->addMethod('methodName2');
$this->assertAttributeEquals(array('methodName', 'methodName2'), 'methods', $this->mockProxy);
}
public function testRemoveMethod()
{
EcomDev_Utils_Reflection::setRestrictedPropertyValue($this->mockProxy, 'methods', array(
'methodName', 'methodName2', 'methodName3',
));
$this->mockProxy->removeMethod('methodName2');
$this->assertAttributeEquals(array('methodName', 'methodName3'), 'methods', $this->mockProxy);
$this->mockProxy->removeMethod('methodName');
$this->assertAttributeEquals(array('methodName3'), 'methods', $this->mockProxy);
}
public function testPreserveMethods()
{
$this->assertAttributeSame(array(), 'methods', $this->mockProxy);
$this->mockProxy->preserveMethods();
$this->assertAttributeSame(null, 'methods', $this->mockProxy);
}
public function testGetMockInstance()
{
$mockInstance = $this->mockProxy->getMockInstance();
$this->assertInstanceOf(
'PHPUnit_Framework_MockObject_MockObject',
$mockInstance
);
$this->assertInstanceOf(
'EcomDev_PHPUnit_Constraint_Abstract',
$mockInstance
);
$this->assertSame($mockInstance, $this->mockProxy->getMockInstance());
}
public function testGetMockClass()
{
$this->assertSame(
get_class($this->mockProxy->getMockInstance()),
$this->mockProxy->getMockClass()
);
}
public function testExpects()
{
$this->assertAttributeEmpty('mockInstance', $this->mockProxy);
$this->assertInstanceOf(
'PHPUnit_Framework_MockObject_Builder_InvocationMocker',
$this->mockProxy->expects($this->any())->method('compareValues')
);
$this->assertAttributeInstanceOf('EcomDev_PHPUnit_Constraint_Abstract', 'mockInstance', $this->mockProxy);
}
public function testStaticExpects()
{
$this->setExpectedException('RuntimeException', 'staticExpectsProxy');
$this->mockProxy->staticExpects($this->any());
}
public function testStaticExpectsProxy()
{
$this->assertAttributeEmpty('mockInstance', $this->mockProxy);
$this->assertInstanceOf(
'PHPUnit_Framework_MockObject_Builder_InvocationMocker',
$this->mockProxy->staticExpectsProxy($this->any())->method('compareValues')
);
$this->assertAttributeInstanceOf('EcomDev_PHPUnit_Constraint_Abstract', 'mockInstance', $this->mockProxy);
}
public function testGetInvocationMocker()
{
$this->assertAttributeEmpty('mockInstance', $this->mockProxy);
$this->setExpectedException('RuntimeException', 'getMockInstance');
$this->mockProxy->__phpunit_getInvocationMocker();
}
public function testGetStaticInvocationMocker()
{
$this->assertAttributeEmpty('mockInstance', $this->mockProxy);
$this->setExpectedException('RuntimeException', 'getMockInstance');
$this->mockProxy->__phpunit_getStaticInvocationMocker();
}
public function testVerify()
{
$this->assertAttributeEmpty('mockInstance', $this->mockProxy);
$this->setExpectedException('RuntimeException', 'getMockInstance');
$this->mockProxy->__phpunit_verify();
}
public function testCall()
{
// Just checks that call is forwarded to mocked class functionality
$this->assertEquals(false, $this->mockProxy->compareValues('value1', 'value2'));
$this->assertEquals(true, $this->mockProxy->compareValues('value1', 'value1'));
}
}
\ No newline at end of file
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
/**
* Base helper implementation
*/
abstract class EcomDev_PHPUnit_Helper_Abstract implements EcomDev_PHPunit_Helper_Interface
{
/**
* @var PHPUnit_Framework_TestCase
*/
protected $testCase;
/**
* Checks existence of helper action
*
* @param string $action
* @return bool
*/
public function has($action)
{
return $this->hasMethod('helper' . ucfirst($action));
}
/**
* Invokes defined helper action
*
* @param string $action
* @param array $args
*
* @throws RuntimeException
* @return mixed
*/
public function invoke($action, array $args)
{
if (!$this->has($action)) {
throw new RuntimeException(sprintf('Helper "%s" is not invokable.', $action));
}
$methodName = 'helper' . ucfirst($action);
return $this->callMethod($methodName, $args);
}
/**
* Call method to make testable of the invoke method
*
* @param $method
* @param $args
* @return mixed
*/
protected function callMethod($method, $args)
{
return call_user_func_array(array($this, $method), $args);
}
/**
* Has method for making abstract testable
*
* @param array $method
* @return bool
*/
protected function hasMethod($method)
{
$reflection = EcomDev_Utils_Reflection::getReflection($this);
return $reflection->hasMethod($method);
}
/**
* Sets test case property for helper
*
* @param PHPUnit_Framework_TestCase $testCase
*
* @return $this
*/
public function setTestCase(PHPUnit_Framework_TestCase $testCase)
{
$this->testCase = $testCase;
return $this;
}
}
\ No newline at end of file
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
/**
* Interface for PHPUnit Test Helpers
*/
interface EcomDev_PHPunit_Helper_Interface
{
/**
* Checks if helper has action for invocation
*
* @param string $action
* @return bool
*/
public function has($action);
/**
* Invokes helper action
*
* @param string $action
* @param array $args
*
* @return mixed
*/
public function invoke($action, array $args);
/**
* Sets test case for usage in helper
*
* @param PHPUnit_Framework_TestCase $testCase
* @return $this
*/
public function setTestCase(PHPUnit_Framework_TestCase $testCase);
}
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
/**
* PHPUnit Mock Object Proxy
*
* Used to support mock builder auto-apply as soon as expects method is called.
*/
class EcomDev_PHPUnit_Mock_Proxy
extends PHPUnit_Framework_MockObject_MockBuilder
implements PHPUnit_Framework_MockObject_MockObject
{
protected $mockInstance;
/**
* Adds method name to a mock builder
*
* @param string $methodName
* @return $this
*/
public function addMethod($methodName)
{
$this->methods[] = $methodName;
return $this;
}
/**
* Removes method name from a mock builder
*
* @param string $methodName
* @return $this
*/
public function removeMethod($methodName)
{
$methodIndex = array_search($methodName, $this->methods);
if ($methodIndex !== false) {
array_splice($this->methods, $methodIndex, 1);
}
return $this;
}
/**
* Preserves methods from override in mocked object
*
* @return $this
*/
public function preserveMethods()
{
$this->setMethods(null);
return $this;
}
/**
* Proxied mock instance retrieval
*
* @return PHPUnit_Framework_MockObject_MockObject
*/
public function getMockInstance()
{
if ($this->mockInstance === null) {
$reflection = EcomDev_Utils_Reflection::getReflection($this->className);
$this->mockInstance = ($reflection->isAbstract() || $reflection->isInterface())
? $this->getMockForAbstractClass() : $this->getMock();
}
return $this->mockInstance;
}
/**
* Returns mock class name for generated mock instance
*
* @return string
*/
public function getMockClass()
{
return get_class($this->getMockInstance());
}
/**
* Registers a new expectation in the mock object and returns the match
* object which can be infused with further details.
*
* @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
{
return $this->getMockInstance()->expects($matcher);
}
/**
* Registers a new static expectation in the mock object and returns the
* match object which can be infused with further details.
*
* @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
* @throws RuntimeException in case if you call it
*/
public static function staticExpects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
{
throw new RuntimeException(
'This method cannot be called on mock proxy, use staticExpectsProxy instead'
);
}
/**
* Registers a new static expectation in the mock object and returns the
* match object which can be infused with further details.
*
* @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function staticExpectsProxy(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
{
return $this->getMockInstance()->staticExpects($matcher);
}
/**
* Returns invocation mocker for
*
* @return PHPUnit_Framework_MockObject_InvocationMocker
*/
public function __phpunit_getInvocationMocker()
{
throw new RuntimeException(
'Mock object proxy cannot be used for retrieving invocation mockers, '
. 'use getMockInstance method for real mock object'
);
}
/**
* @return PHPUnit_Framework_MockObject_InvocationMocker
*/
public static function __phpunit_getStaticInvocationMocker()
{
throw new RuntimeException(
'Mock object proxy cannot be used for retrieving invocation mockers, '
. 'use getMockInstance method for real mock object'
);
}
/**
* Verifies that the current expectation is valid. If everything is OK the
* code should just return, if not it must throw an exception.
*
* @throws PHPUnit_Framework_ExpectationFailedException
*/
public function __phpunit_verify()
{
throw new RuntimeException(
'Mock object proxy cannot be used for verifying mock'
. 'use getMockInstance method for real mock object'
);
}
/**
* Forwards all method calls to mock instance
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments)
{
return call_user_func_array(
array($this->getMockInstance(), $name),
$arguments
);
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment