Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
EcomDev_PHPUnit
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Open
EcomDev_PHPUnit
Commits
9f4cc5a2
Commit
9f4cc5a2
authored
Feb 09, 2013
by
Ivan Chepurnyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
! Commit unfinished stuff, since laptop brokedown
parent
19fa6319
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
250 additions
and
7 deletions
+250
-7
app/code/community/EcomDev/PHPUnit/Controller/Front.php
app/code/community/EcomDev/PHPUnit/Controller/Front.php
+6
-6
app/code/community/EcomDev/PHPUnit/Model/Observer.php
app/code/community/EcomDev/PHPUnit/Model/Observer.php
+22
-1
app/code/community/EcomDev/PHPUnit/Test/Case.php
app/code/community/EcomDev/PHPUnit/Test/Case.php
+1
-0
app/code/community/EcomDev/PHPUnit/Test/Case/Helper/Customer.php
...e/community/EcomDev/PHPUnit/Test/Case/Helper/Customer.php
+25
-0
app/code/community/EcomDev/PHPUnit/Test/Case/Helper/Session.php
...de/community/EcomDev/PHPUnit/Test/Case/Helper/Session.php
+131
-0
app/code/community/EcomDev/PHPUnit/etc/config.xml
app/code/community/EcomDev/PHPUnit/etc/config.xml
+5
-0
app/code/community/EcomDev/PHPUnitTest/Test/Helper/Customer.php
...de/community/EcomDev/PHPUnitTest/Test/Helper/Customer.php
+6
-0
app/code/community/EcomDev/PHPUnitTest/Test/Helper/Session.php
...ode/community/EcomDev/PHPUnitTest/Test/Helper/Session.php
+54
-0
No files found.
app/code/community/EcomDev/PHPUnit/Controller/Front.php
View file @
9f4cc5a2
...
...
@@ -23,18 +23,18 @@
class
EcomDev_PHPUnit_Controller_Front
extends
Mage_Core_Controller_Varien_Front
{
/**
*
Overriden for getting rid
* o
f initialization of routers for each test case
*
Resets initialized routers before front controller re-initialization
* o
n 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
()
;
}
}
\ No newline at end of file
}
app/code/community/EcomDev/PHPUnit/Model/Observer.php
View file @
9f4cc5a2
...
...
@@ -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
app/code/community/EcomDev/PHPUnit/Test/Case.php
View file @
9f4cc5a2
...
...
@@ -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
{
...
...
app/code/community/EcomDev/PHPUnit/Test/Case/Helper/Customer.php
0 → 100644
View file @
9f4cc5a2
<?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
app/code/community/EcomDev/PHPUnit/Test/Case/Helper/Session.php
0 → 100644
View file @
9f4cc5a2
<?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
();
}
}
}
app/code/community/EcomDev/PHPUnit/etc/config.xml
View file @
9f4cc5a2
...
...
@@ -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>
...
...
app/code/community/EcomDev/PHPUnitTest/Test/Helper/Customer.php
0 → 100644
View file @
9f4cc5a2
<?php
class
EcomDev_PHPUnitTest_Test_Helper_Customer
extends
EcomDev_PHPUnit_Test_Case
{
}
\ No newline at end of file
app/code/community/EcomDev/PHPUnitTest/Test/Helper/Session.php
0 → 100644
View file @
9f4cc5a2
<?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'
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment