Commit 192345c2 authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

! Mock helpers implementation

parent e25e4d0e
<?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_PHPUnit_Model_Observer
{
/**
* Registers default test helpers
*
*/
public function registerDefaultTestHelpers()
{
EcomDev_PHPUnit_Helper::add(new EcomDev_PHPUnit_Test_Case_Helper_Mock());
}
}
\ No newline at end of file
......@@ -17,11 +17,15 @@
*/
use EcomDev_PHPUnit_Test_Case_Util as TestUtil;
use EcomDev_PHPUnit_Helper as TestHelper;
/**
* Basic test case class
*
* Basic test case class, implements test helpers for easy working with Magento
*
* @method EcomDev_PHPUnit_Mock_Proxy mockClassAlias(string $type, $classAlias, array $methods = array(), array $constructorArgs = array())
* @method EcomDev_PHPUnit_Mock_Proxy mockModel($classAlias, array $methods = array(), array $constructorArgs = array())
* @method EcomDev_PHPUnit_Mock_Proxy mockBlock($classAlias, array $methods = array(), array $constructorArgs = array())
* @method EcomDev_PHPUnit_Mock_Proxy mockHelper($classAlias, array $methods = array(), array $constructorArgs = array())
*/
abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
{
......@@ -372,20 +376,18 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
*
* @param string $type block|model|helper
* @param string $classAlias
* @return PHPUnit_Framework_MockObject_MockBuilder
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public function getGroupedClassMockBuilder($type, $classAlias)
{
$className = $this->getGroupedClassName($type, $classAlias);
return $this->getMockBuilder($className);
return TestUtil::getGroupedClassMockBuilder($this, $type, $classAlias);
}
/**
* Retrieves a mock builder for a block class alias
*
* @param string $classAlias
* @return PHPUnit_Framework_MockObject_MockBuilder
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public function getBlockMockBuilder($classAlias)
{
......@@ -396,7 +398,7 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
* Retrieves a mock builder for a model class alias
*
* @param string $classAlias
* @return PHPUnit_Framework_MockObject_MockBuilder
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public function getModelMockBuilder($classAlias)
{
......@@ -407,7 +409,7 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
* Retrieves a mock builder for a resource model class alias
*
* @param string $classAlias
* @return PHPUnit_Framework_MockObject_MockBuilder
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public function getResourceModelMockBuilder($classAlias)
{
......@@ -418,7 +420,7 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
* Retrieves a mock builder for a helper class alias
*
* @param string $classAlias
* @return PHPUnit_Framework_MockObject_MockBuilder
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public function getHelperMockBuilder($classAlias)
{
......@@ -436,7 +438,7 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
* @param boolean $callOriginalConstructor
* @param boolean $callOriginalClone
* @param boolean $callAutoload
* @return PHPUnit_Framework_MockObject_MockObject
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public function getModelMock($classAlias, $methods = array(), $isAbstract = false,
array $constructorArguments = array(),
......@@ -460,7 +462,7 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
* @param boolean $callOriginalConstructor
* @param boolean $callOriginalClone
* @param boolean $callAutoload
* @return PHPUnit_Framework_MockObject_MockObject
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public function getResourceModelMock($classAlias, $methods = array(), $isAbstract = false,
array $constructorArguments = array(),
......@@ -484,7 +486,7 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
* @param boolean $callOriginalConstructor
* @param boolean $callOriginalClone
* @param boolean $callAutoload
* @return PHPUnit_Framework_MockObject_MockObject
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public function getHelperMock($classAlias, $methods = array(), $isAbstract = false,
array $constructorArguments = array(),
......@@ -508,7 +510,7 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
* @param boolean $callOriginalConstructor
* @param boolean $callOriginalClone
* @param boolean $callAutoload
* @return PHPUnit_Framework_MockObject_MockObject
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public function getBlockMock($classAlias, $methods = array(), $isAbstract = false,
array $constructorArguments = array(),
......@@ -529,11 +531,7 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
*/
protected function getGroupedClassName($type, $classAlias)
{
if ($type === 'resource_model') {
return $this->app()->getConfig()->getResourceModelClassName($classAlias);
}
return $this->app()->getConfig()->getGroupedClassName($type, $classAlias);
return TestUtil::getGroupedClassName($type, $classAlias);
}
/**
......@@ -705,4 +703,16 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
TestUtil::setCurrentStore($store);
return $this;
}
/**
* Calling of the helper method
*
* @param string $method
* @param array $args
* @return mixed
*/
public function __call($method, array $args)
{
return TestUtil::call($method, $args);
}
}
<?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>
*/
use EcomDev_PHPUnit_Test_Case_Util as TestUtil;
/**
* Mock helper for Test Case
*
*/
class EcomDev_PHPUnit_Test_Case_Helper_Mock extends EcomDev_PHPUnit_Helper_Abstract
{
/**
* Creates a mockery for a class alias of particular type
*
* @param string $type
* @param string $classAlias
* @param array $methods
* @param array $constructorArgs
*
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public function helperMockClassAlias($type, $classAlias, array $methods = array(), array $constructorArgs = array())
{
return TestUtil::getGroupedClassMockBuilder($this->testCase, $type, $classAlias)
->setConstructorArgs($constructorArgs)
->setMethods($methods);
}
/**
* Creates a mock for a model by its class alias
*
* @param string $classAlias
* @param array $methods
* @param array $constructorArgs
*
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public function helperMockModel($classAlias, array $methods = array(), array $constructorArgs = array())
{
return $this->helperMockClassAlias('model', $classAlias, $methods, $constructorArgs);
}
/**
* Creates a mock for a block by its class alias
*
* @param string $classAlias
* @param array $methods
* @param array $constructorArgs
*
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public function helperMockBlock($classAlias, array $methods = array(), array $constructorArgs = array())
{
return $this->helperMockClassAlias('block', $classAlias, $methods, $constructorArgs);
}
/**
* Creates a mock for a block by its class alias
*
* @param string $classAlias
* @param array $methods
* @param array $constructorArgs
*
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public function helperMockHelper($classAlias, array $methods = array(), array $constructorArgs = array())
{
return $this->helperMockClassAlias('helper', $classAlias, $methods, $constructorArgs);
}
}
......@@ -16,13 +16,14 @@
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
use EcomDev_PHPUnit_Helper as TestHelper;
class EcomDev_PHPUnit_Test_Case_Util
{
const XML_PATH_DEFAULT_FIXTURE_MODEL = 'phpunit/suite/fixture/model';
const XML_PATH_DEFAULT_EXPECTATION_MODEL = 'phpunit/suite/expectation/model';
const XML_PATH_DEFAULT_YAML_LOADER_MODEL = 'phpunit/suite/yaml/model';
/**
* List of replaced registry keys for current test case run
*
......@@ -465,6 +466,36 @@ class EcomDev_PHPUnit_Test_Case_Util
self::$replacedRegistry[$key] = $oldValue;
}
/**
* Returns class name by grouped class alias
*
* @param string $type block/model/helper/resource_model
* @param string $classAlias
* @return string
*/
public static function getGroupedClassName($type, $classAlias)
{
if ($type === 'resource_model') {
return self::app()->getConfig()->getResourceModelClassName($classAlias);
}
return self::app()->getConfig()->getGroupedClassName($type, $classAlias);
}
/**
* Retrieve mock builder for grouped class alias
*
* @param PHPUnit_Framework_TestCase $testCase
* @param string $type block|model|helper
* @param string $classAlias
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public static function getGroupedClassMockBuilder(PHPUnit_Framework_TestCase $testCase, $type, $classAlias)
{
$className = self::getGroupedClassName($type, $classAlias);
return new EcomDev_PHPUnit_Mock_Proxy($testCase, $className);
}
/**
* Called for each test case
*
......@@ -491,4 +522,36 @@ class EcomDev_PHPUnit_Test_Case_Util
self::app()->replaceRegistry($registryPath, $originalValue);
}
}
/**
* Implementation of __call method functionality that can be used from a test case
*
* @param string $method
* @param array $args
*
* @throws ErrorException
* @return bool
*/
public static function call($method, $args)
{
if (TestHelper::has($method)) {
return TestHelper::invokeArgs($method, $args);
}
$backTraceCalls = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 3);
$previousCall = $backTraceCalls[2];
throw new ErrorException(
sprintf(
'Call to undefined function %s%s%s()',
$previousCall['class'],
$previousCall['type'],
$previousCall['function']
),
0,
E_USER_ERROR,
$previousCall['file'],
$previousCall['line']
);
}
}
......@@ -59,8 +59,6 @@ class EcomDev_PHPUnit_Test_Listener implements PHPUnit_Framework_TestListener
$this->getAppReflection()->getMethod('applyTestScope')->invoke(null);
}
$this->firstLevelTestSuite = $suite;
Mage::dispatchEvent('phpunit_suite_start_after', array(
'suite' => $suite,
......@@ -151,6 +149,7 @@ class EcomDev_PHPUnit_Test_Listener implements PHPUnit_Framework_TestListener
->apply();
EcomDev_PHPUnit_Test_Case_Util::setUp();
EcomDev_PHPUnit_Helper::setUp();
}
Mage::dispatchEvent('phpunit_test_start_after', array(
'test' => $test,
......@@ -181,6 +180,7 @@ class EcomDev_PHPUnit_Test_Listener implements PHPUnit_Framework_TestListener
}
EcomDev_PHPUnit_Test_Case_Util::tearDown();
EcomDev_PHPUnit_Helper::tearDown();
}
Mage::dispatchEvent('phpunit_test_end_after', array(
......
......@@ -42,6 +42,16 @@
</ecomdev_phpunit>
</types>
</cache>
<events>
<phpunit_suite_start_after>
<observers>
<ecomdev_phpunit>
<class>ecomdev_phpunit/observer</class>
<method>registerDefaultTestHelpers</method>
</ecomdev_phpunit>
</observers>
</phpunit_suite_start_after>
</events>
</global>
<phpunit>
<suite>
......
<?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_Helper_Call extends EcomDev_PHPUnit_Test_Case
{
/**
* @var EcomDev_PHPUnit_Helper_Interface|PHPUnit_Framework_MockObject_MockObject
*/
protected $helper;
/**
* Creates new helper for test
*/
protected function setUp()
{
$this->helper = $this->getMockForAbstractClass('EcomDev_PHPUnit_Helper_Interface');
$this->helper->expects($this->any())
->method('invoke')
->with($this->equalTo('someCustomHelper'))
->will($this->returnSelf());
$this->helper->expects($this->any())
->method('has')
->will($this->returnValueMap(array(
array('someCustomHelper', true)
)));
EcomDev_PHPUnit_Helper::add($this->helper);
}
/**
* Testing calling of helper via test case
*
*/
public function testCall()
{
$this->assertSame(
$this->helper, $this->someCustomHelper()
);
}
/**
* @expectedException ErrorException
* @expectedExceptionMessage Call to undefined function EcomDev_PHPUnitTest_Test_Helper_Call->unknownHelper()
*/
public function testCallError()
{
$this->unknownHelper('');
}
public function tearDown()
{
EcomDev_PHPUnit_Helper::remove($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_Helper_Mock extends EcomDev_PHPUnit_Test_Case
{
public function testMockClassAlias()
{
$mock = $this->mockClassAlias('model', 'catalog/product',
array('getId'),
array(array('entity_id' => 1))
);
$this->assertInstanceOf('EcomDev_PHPUnit_Mock_Proxy', $mock);
$this->assertAttributeEquals($this->getGroupedClassName('model', 'catalog/product'), 'className', $mock);
$this->assertAttributeContains('getId', 'methods', $mock);
$this->assertAttributeContains(array('entity_id' => 1), 'constructorArgs', $mock);
}
public function testModelMock()
{
$mock = $this->mockModel('catalog/product',
array('getId'),
array(array('entity_id' => 1))
);
$this->assertInstanceOf('EcomDev_PHPUnit_Mock_Proxy', $mock);
$this->assertAttributeEquals($this->getGroupedClassName('model', 'catalog/product'), 'className', $mock);
$this->assertAttributeContains('getId', 'methods', $mock);
$this->assertAttributeContains(array('entity_id' => 1), 'constructorArgs', $mock);
}
public function testBlockMock()
{
$mock = $this->mockBlock('catalog/product_view',
array('getTemplate'),
array(array('product_id' => 1))
);
$this->assertInstanceOf('EcomDev_PHPUnit_Mock_Proxy', $mock);
$this->assertAttributeEquals($this->getGroupedClassName('block', 'catalog/product_view'), 'className', $mock);
$this->assertAttributeContains('getTemplate', 'methods', $mock);
$this->assertAttributeContains(array('product_id' => 1), 'constructorArgs', $mock);
}
public function testHelperMock()
{
$mock = $this->mockBlock('catalog/category',
array('getStoreCategories'),
array('some_value')
);
$this->assertInstanceOf('EcomDev_PHPUnit_Mock_Proxy', $mock);
$this->assertAttributeEquals($this->getGroupedClassName('block', 'catalog/category'), 'className', $mock);
$this->assertAttributeContains('getStoreCategories', 'methods', $mock);
$this->assertAttributeContains('some_value', 'constructorArgs', $mock);
}
}
\ 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>
*/
use EcomDev_PHPUnit_Helper as Helper;
......@@ -43,9 +59,15 @@ class EcomDev_PHPUnitTest_Test_Lib_Helper extends PHPUnit_Framework_TestCase
{
$result = array();
$helperInterfaces = array(
true => 'EcomDev_PHPUnit_Helper_Interface',
false => 'EcomDev_PHPUnit_Helper_Listener_Interface'
);
for ($i = 0; $i < $count; $i ++) {
$helperInterface = $helperInterfaces[$i % 2 === 0];
$result[] = $this->getMockForAbstractClass(
'EcomDev_PHPUnit_Helper_Interface', array(), 'Test_Helper_Name' . $i
$helperInterface, array(), 'Test_Helper_Name' . $i
);
}
......@@ -331,6 +353,48 @@ class EcomDev_PHPUnitTest_Test_Lib_Helper extends PHPUnit_Framework_TestCase
EcomDev_PHPUnit_Helper::setTestCase($this);
}
/**
* Test that when set up is invoked,
* test helpers that support setUp method invoked as well
*
*/
public function testSetUp()
{
$helpers = $this->getHelpersForTest(4, true);
$helpers[0]->expects($this->never())
->method('setUp');
$helpers[1]->expects($this->once())
->method('setUp');
$helpers[2]->expects($this->never())
->method('setUp');
$helpers[3]->expects($this->once())
->method('setUp');
EcomDev_PHPUnit_Helper::setUp();
}
/**
* Test that when set up is invoked,
* test helpers that support setUp method invoked as well
*
*/
public function testTearDown()
{
$helpers = $this->getHelpersForTest(4, true);
$helpers[0]->expects($this->never())
->method('tearDown');
$helpers[1]->expects($this->once())
->method('tearDown');
$helpers[2]->expects($this->never())
->method('tearDown');
$helpers[3]->expects($this->once())
->method('tearDown');
EcomDev_PHPUnit_Helper::tearDown();
}
protected function tearDown()
{
// Revert helpers in helper class
......
......@@ -103,5 +103,4 @@ class EcomDev_PHPUnitTest_Test_Lib_Helper_Abstract extends PHPUnit_Framework_Tes
$this->assertAttributeSame($this, 'testCase', $this->helper);
}
}
\ No newline at end of file
}
......@@ -147,4 +147,4 @@ class EcomDev_PHPUnitTest_Test_Lib_Mock_Proxy extends PHPUnit_Framework_TestCase
$this->assertEquals(false, $this->mockProxy->compareValues('value1', 'value2'));
$this->assertEquals(true, $this->mockProxy->compareValues('value1', 'value1'));
}
}
\ No newline at end of file
}
......@@ -151,6 +151,35 @@ class EcomDev_PHPUnit_Helper
}
}
/**
* Calls setUp method on helper,
* that implements EcomDev_PHPUnit_Helper_Listener_Interface
*
*/
public static function setUp()
{
foreach (self::$helpers as $helper) {
if ($helper instanceof EcomDev_PHPUnit_Helper_Listener_Interface) {
$helper->setUp();
}
}
}
/**
* Calls tearDown method on helper,
* that implements EcomDev_PHPUnit_Helper_Listener_Interface
*
*/
public static function tearDown()
{
foreach (self::$helpers as $helper) {
if ($helper instanceof EcomDev_PHPUnit_Helper_Listener_Interface) {
$helper->tearDown();
}
}
}
/**
* Finds a helper instance by class name
*
......@@ -164,5 +193,4 @@ class EcomDev_PHPUnit_Helper
});
}
}
\ No newline at end of file
}
<?php
/**
* Interface for helpers that support setUp() and tearDown() methods
*
* These methods are invoked when test setUp() or tearDown() is executed
*
*/
interface EcomDev_PHPUnit_Helper_Listener_Interface extends EcomDev_PHPunit_Helper_Interface
{
public function setUp();
public function tearDown();
}
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