Commit 9f4cc5a2 authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

! Commit unfinished stuff, since laptop brokedown

parent 19fa6319
......@@ -23,18 +23,18 @@
class EcomDev_PHPUnit_Controller_Front extends Mage_Core_Controller_Varien_Front
{
/**
* Overriden for getting rid
* of initialization of routers for each test case
* Resets initialized routers before front controller re-initialization
* on test cases
*
* (non-PHPdoc)
* @see Mage_Core_Controller_Varien_Front::init()
*/
public function init()
{
if (!$this->_routers) {
parent::init();
if ($this->_routers) {;
$this->_routers = array();
}
return $this;
return parent::init();
}
}
......@@ -18,12 +18,33 @@
class EcomDev_PHPUnit_Model_Observer
{
const XML_PATH_TEST_HELPERS = 'phpunit/suite/helpers';
/**
* Registers default test helpers
*
*/
public function registerDefaultTestHelpers()
{
EcomDev_PHPUnit_Helper::add(new EcomDev_PHPUnit_Test_Case_Helper_Mock());
foreach (Mage::getConfig()->getNode(self::XML_PATH_TEST_HELPERS)->children() as $helperNode) {
$helperClass = (string)$helperNode;
if ($helperClass && class_exists($helperClass)) {
$helper = new $helperClass();
if (!$helper instanceof EcomDev_PHPUnit_Helper_Interface) {
throw new RuntimeException(
sprintf(
'Test helpers should implement %s, but %s is not implementing it.',
'EcomDev_PHPUnit_Helper_Interface',
$helperClass
)
);
}
EcomDev_PHPUnit_Helper::add($helper);
}
}
}
}
\ No newline at end of file
......@@ -26,6 +26,7 @@ use EcomDev_PHPUnit_Helper as TestHelper;
* @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())
* @method EcomDev_PHPUnit_Mock_Proxy mockSession($classAlias, array $methods = array())
*/
abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
{
......
<?php
class EcomDev_PHPUnit_Test_Case_Helper_Customer extends EcomDev_PHPUnit_Helper_Abstract
{
/**
* Logs in as a customer by customer id and store id
*
* @param int $customerId
* @param string|int|null $storeId
*
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public function helperCustomerSession($customerId, $storeId = null)
{
$customerSessionMock = $this->helperMockSession('customer/session', array('renewSession'));
if ($storeId === null) {
$storeId = TestUtil::app()->getAnyStoreView()->getCode();
}
TestUtil::setCurrentStore($storeId);
$customerSessionMock->loginById($customerId);
return $customerSessionMock;
}
}
\ 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_Test_Case_Util as TestUtil;
class EcomDev_PHPUnit_Test_Case_Helper_Session
extends EcomDev_PHPUnit_Helper_Abstract
implements EcomDev_PHPUnit_Helper_Listener_Interface
{
/**
* Loaded ACL model for admin session mocks
*
* @var Mage_Admin_Model_Acl
*/
protected $acl;
/**
* Creates a mockery for session
*
* @param string $classAlias
* @param array $methods
*
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public function helperMockSession($classAlias, array $methods = array())
{
$sessionMock = EcomDev_PHPUnit_Helper::invoke('mockModel', $classAlias, $methods)
->disableOriginalConstructor();
TestUtil::replaceByMock('singleton', $classAlias, $sessionMock);
return $sessionMock;
}
/**
* Helper for mocking admin panel session
*
* @param array $resources
* @return EcomDev_PHPUnit_Mock_Proxy
*/
public function helperAdminSession(array $resources = array())
{
$session = $this->helperMockSession('admin/session', array('refreshAcl'));
$user = $this->createUser();
$this->loadRules($user, $this->getAcl(), $resources);
$session->setUser($user);
return $session;
}
public function getAcl()
{
if ($this->acl === null) {
$this->acl = Mage::getModel('admin/acl');
Mage::getSingleton('admin/config')->loadAclResources($this->acl);
}
return $this->acl;
}
public function loadRules($user, $acl, array $allowedResources = array())
{
$userRole = Mage::getModel('admin/acl_role_user', Mage_Admin_Model_Acl::ROLE_TYPE_USER . $user->getId());
$acl->addRole($userRole);
if (empty($allowedResources)) {
$acl->allow($userRole);
return $this;
}
$allow = array();
foreach ($allowedResources as $resource) {
$childResources = array_filter(
$acl->getResources(),
function ($entry) use ($resource) {
return strpos($entry, 'admin/' . $resource) === 0;
}
);
$allow = array_merge($allow, array($resource), $childResources);
}
$deny = array();
foreach ($acl->getResources() as $resource) {
if (!in_array($resource, $allow)) {
$deny[] = $resource;
}
}
$deny;
}
public function createUser($entropy = 3)
{
$userId = floor(microtime(true)*pow(10, $entropy) - floor(time()/100)*100*pow(10, $entropy));
return Mage::getModel('admin/user')->setId($userId);
}
/**
* Does nothing during test setup
*
*
*/
public function setUp()
{
}
/**
* Clean ups acl roles information after test completed
*
*/
public function tearDown()
{
if ($this->acl !== null) {
$this->acl->removeRoleAll();
}
}
}
......@@ -141,6 +141,11 @@
<Namespace_MyModule />
-->
</modules>
<helpers>
<mock>EcomDev_PHPUnit_Test_Case_Helper_Mock</mock>
<session>EcomDev_PHPUnit_Test_Case_Helper_Session</session>
</helpers>
</suite>
</phpunit>
<test>
......
<?php
class EcomDev_PHPUnitTest_Test_Helper_Customer extends EcomDev_PHPUnit_Test_Case
{
}
\ No newline at end of file
<?php
class EcomDev_PHPUnitTest_Test_Helper_Session extends EcomDev_PHPUnit_Test_Case_Controller
{
public function testMockSession()
{
$sessionMock = $this->mockSession('admin/session', array('getUserId'));
$this->assertInstanceOf('EcomDev_PHPUnit_Mock_Proxy', $sessionMock);
$this->assertInstanceOf('Mage_Admin_Model_Session', $sessionMock->getMockInstance());
$this->assertSame($sessionMock->getMockInstance(), Mage::getSingleton('admin/session'));
}
/**
* Tests stubing of admin session
*
*/
public function testAdminSessionAllRights()
{
$this->adminSession();
$this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('catalog/product'));
$this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('sales/order'));
$this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('system/config'));
}
/**
* Tests creation of admin session
*
*/
public function testAdminSessionOnlyCatalog()
{
$this->adminSession(array('catalog'));
$this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('catalog/product'));
$this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('catalog/category'));
$this->assertFalse(Mage::getSingleton('admin/session')->isAllowed('sales/order'));
$this->assertFalse(Mage::getSingleton('admin/session')->isAllowed('system/config'));
}
/**
* Tests creation of admin session
*
*/
public function testAdminSessionOnlyCatalogProduct()
{
$this->adminSession(array('catalog/product'));
$this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('catalog/product'));
$this->assertFalse(Mage::getSingleton('admin/session')->isAllowed('catalog/category'));
$this->assertFalse(Mage::getSingleton('admin/session')->isAllowed('sales/order'));
$this->assertFalse(Mage::getSingleton('admin/session')->isAllowed('system/config'));
}
}
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