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
53633a2d
Commit
53633a2d
authored
Feb 17, 2013
by
Ivan Chepurnyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
! New test helpers implementation
parent
9f4cc5a2
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
162 additions
and
16 deletions
+162
-16
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
+30
-2
app/code/community/EcomDev/PHPUnit/Test/Case/Helper/Session.php
...de/community/EcomDev/PHPUnit/Test/Case/Helper/Session.php
+44
-6
app/code/community/EcomDev/PHPUnit/etc/config.xml
app/code/community/EcomDev/PHPUnit/etc/config.xml
+1
-0
app/code/community/EcomDev/PHPUnitTest/Test/Helper/Customer.php
...de/community/EcomDev/PHPUnitTest/Test/Helper/Customer.php
+20
-1
app/code/community/EcomDev/PHPUnitTest/Test/Helper/Session.php
...ode/community/EcomDev/PHPUnitTest/Test/Helper/Session.php
+13
-7
app/code/community/EcomDev/PHPUnitTest/Test/Helper/_data/dp-testCustomerSession.yaml
...PHPUnitTest/Test/Helper/_data/dp-testCustomerSession.yaml
+6
-0
app/code/community/EcomDev/PHPUnitTest/Test/Helper/_data/ex-testCustomerSession.yaml
...PHPUnitTest/Test/Helper/_data/ex-testCustomerSession.yaml
+8
-0
app/code/community/EcomDev/PHPUnitTest/Test/Helper/_data/fx-customers.yaml
...y/EcomDev/PHPUnitTest/Test/Helper/_data/fx-customers.yaml
+39
-0
No files found.
app/code/community/EcomDev/PHPUnit/Test/Case.php
View file @
53633a2d
...
...
@@ -27,6 +27,7 @@ use EcomDev_PHPUnit_Helper as TestHelper;
* @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())
* @method EcomDev_PHPUnit_Mock_Proxy adminSession(array $resources = array())
*/
abstract
class
EcomDev_PHPUnit_Test_Case
extends
PHPUnit_Framework_TestCase
{
...
...
app/code/community/EcomDev/PHPUnit/Test/Case/Helper/Customer.php
View file @
53633a2d
<?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
TestHelper
;
use
EcomDev_PHPUnit_Test_Case_Util
as
TestUtil
;
/**
* Helper for stubbing customer session
*
*
*/
class
EcomDev_PHPUnit_Test_Case_Helper_Customer
extends
EcomDev_PHPUnit_Helper_Abstract
{
/**
...
...
@@ -12,7 +36,11 @@ class EcomDev_PHPUnit_Test_Case_Helper_Customer extends EcomDev_PHPUnit_Helper_A
*/
public
function
helperCustomerSession
(
$customerId
,
$storeId
=
null
)
{
$customerSessionMock
=
$this
->
helperMockSession
(
'customer/session'
,
array
(
'renewSession'
));
$customerSessionMock
=
TestHelper
::
invoke
(
'mockSession'
,
'customer/session'
,
array
(
'renewSession'
)
);
if
(
$storeId
===
null
)
{
$storeId
=
TestUtil
::
app
()
->
getAnyStoreView
()
->
getCode
();
...
...
app/code/community/EcomDev/PHPUnit/Test/Case/Helper/Session.php
View file @
53633a2d
...
...
@@ -18,6 +18,10 @@
use
EcomDev_PHPUnit_Test_Case_Util
as
TestUtil
;
/**
* Session mocks helper
*
*/
class
EcomDev_PHPUnit_Test_Case_Helper_Session
extends
EcomDev_PHPUnit_Helper_Abstract
implements
EcomDev_PHPUnit_Helper_Listener_Interface
...
...
@@ -57,10 +61,17 @@ class EcomDev_PHPUnit_Test_Case_Helper_Session
$session
=
$this
->
helperMockSession
(
'admin/session'
,
array
(
'refreshAcl'
));
$user
=
$this
->
createUser
();
$this
->
loadRules
(
$user
,
$this
->
getAcl
(),
$resources
);
$session
->
setAcl
(
$this
->
getAcl
());
$session
->
setUser
(
$user
);
return
$session
;
}
/**
* Returns an instance of ACL object that will be used for all
* admin stubs
*
* @return Mage_Admin_Model_Acl
*/
public
function
getAcl
()
{
if
(
$this
->
acl
===
null
)
{
...
...
@@ -70,42 +81,69 @@ class EcomDev_PHPUnit_Test_Case_Helper_Session
return
$this
->
acl
;
}
public
function
loadRules
(
$user
,
$acl
,
array
$allowedResources
=
array
())
/**
* Loads role rules into ACL for admin user
*
* @param Mage_Admin_Model_User $user
* @param Mage_Admin_Model_Acl $acl
* @param array $allowedResources
*
* @return $this
*/
public
function
loadRules
(
Mage_Admin_Model_User
$user
,
Mage_Admin_Model_Acl
$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
);
$acl
->
allow
(
$userRole
,
$acl
->
getResources
());
return
$this
;
}
$aclResources
=
$acl
->
getResources
();
$allow
=
array
();
foreach
(
$allowedResources
as
$resource
)
{
$childResources
=
array_filter
(
$acl
->
getResources
()
,
$acl
Resources
,
function
(
$entry
)
use
(
$resource
)
{
return
strpos
(
$entry
,
'admin/'
.
$resource
)
===
0
;
}
);
$allow
=
array_merge
(
$allow
,
array
(
$resource
),
$childResources
);
$allow
=
array_merge
(
$allow
,
$childResources
);
}
$deny
=
array
();
foreach
(
$acl
->
getResources
()
as
$resource
)
{
foreach
(
$acl
Resources
as
$resource
)
{
if
(
!
in_array
(
$resource
,
$allow
))
{
$deny
[]
=
$resource
;
}
}
$deny
;
$acl
->
allow
(
$userRole
,
$allow
);
$acl
->
deny
(
$userRole
,
$deny
);
return
$this
;
}
/**
* Creates a new instance of user with unique id
*
* Used for stubbing admin user roles
*
* @param int $entropy
* @return Mage_Admin_Model_User
*/
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
);
// Fix for EE gws functionality
$userRole
=
Mage
::
getModel
(
'admin/roles'
);
$userRole
->
setGwsIsAll
(
1
);
$user
=
Mage
::
getModel
(
'admin/user'
);
$user
->
setId
(
$userId
);
EcomDev_Utils_Reflection
::
setRestrictedPropertyValue
(
$user
,
'_role'
,
$userRole
);
return
$user
;
}
/**
...
...
app/code/community/EcomDev/PHPUnit/etc/config.xml
View file @
53633a2d
...
...
@@ -145,6 +145,7 @@
<helpers>
<mock>
EcomDev_PHPUnit_Test_Case_Helper_Mock
</mock>
<session>
EcomDev_PHPUnit_Test_Case_Helper_Session
</session>
<customer>
EcomDev_PHPUnit_Test_Case_Helper_Customer
</customer>
</helpers>
</suite>
</phpunit>
...
...
app/code/community/EcomDev/PHPUnitTest/Test/Helper/Customer.php
View file @
53633a2d
<?php
/**
* Tests for customer session creation
*
* @loadSharedFixture customers
*/
class
EcomDev_PHPUnitTest_Test_Helper_Customer
extends
EcomDev_PHPUnit_Test_Case
{
/**
*
* @dataProvider dataProvider
*/
public
function
testCustomerSession
(
$customerId
)
{
$customerSession
=
$this
->
customerSession
(
$customerId
);
$expected
=
$this
->
expected
(
'auto'
);
$this
->
assertEquals
(
$expected
->
getIsLoggedIn
(),
$customerSession
->
isLoggedIn
());
if
(
$expected
->
getName
())
{
$this
->
assertEquals
(
$expected
->
getName
(),
$customerSession
->
getCustomer
()
->
getName
());
}
}
}
app/code/community/EcomDev/PHPUnitTest/Test/Helper/Session.php
View file @
53633a2d
...
...
@@ -2,6 +2,10 @@
class
EcomDev_PHPUnitTest_Test_Helper_Session
extends
EcomDev_PHPUnit_Test_Case_Controller
{
/**
* Tests stubbing of any session
*
*/
public
function
testMockSession
()
{
$sessionMock
=
$this
->
mockSession
(
'admin/session'
,
array
(
'getUserId'
));
...
...
@@ -19,7 +23,8 @@ class EcomDev_PHPUnitTest_Test_Helper_Session extends EcomDev_PHPUnit_Test_Case_
public
function
testAdminSessionAllRights
()
{
$this
->
adminSession
();
$this
->
assertTrue
(
Mage
::
getSingleton
(
'admin/session'
)
->
isAllowed
(
'catalog/product'
));
$this
->
assertTrue
(
Mage
::
getSingleton
(
'admin/session'
)
->
isLoggedIn
());
$this
->
assertTrue
(
Mage
::
getSingleton
(
'admin/session'
)
->
isAllowed
(
'catalog/products'
));
$this
->
assertTrue
(
Mage
::
getSingleton
(
'admin/session'
)
->
isAllowed
(
'sales/order'
));
$this
->
assertTrue
(
Mage
::
getSingleton
(
'admin/session'
)
->
isAllowed
(
'system/config'
));
}
...
...
@@ -31,9 +36,9 @@ class EcomDev_PHPUnitTest_Test_Helper_Session extends EcomDev_PHPUnit_Test_Case_
public
function
testAdminSessionOnlyCatalog
()
{
$this
->
adminSession
(
array
(
'catalog'
));
$this
->
assertTrue
(
Mage
::
getSingleton
(
'admin/session'
)
->
isAllowed
(
'catalog/product'
));
$this
->
assertTrue
(
Mage
::
getSingleton
(
'admin/session'
)
->
isAllowed
(
'catalog/categor
y
'
));
$this
->
assertTrue
(
Mage
::
getSingleton
(
'admin/session'
)
->
isLoggedIn
());
$this
->
assertTrue
(
Mage
::
getSingleton
(
'admin/session'
)
->
isAllowed
(
'catalog/product
s
'
));
$this
->
assertTrue
(
Mage
::
getSingleton
(
'admin/session'
)
->
isAllowed
(
'catalog/categor
ies
'
));
$this
->
assertFalse
(
Mage
::
getSingleton
(
'admin/session'
)
->
isAllowed
(
'sales/order'
));
$this
->
assertFalse
(
Mage
::
getSingleton
(
'admin/session'
)
->
isAllowed
(
'system/config'
));
}
...
...
@@ -44,10 +49,11 @@ class EcomDev_PHPUnitTest_Test_Helper_Session extends EcomDev_PHPUnit_Test_Case_
*/
public
function
testAdminSessionOnlyCatalogProduct
()
{
$this
->
adminSession
(
array
(
'catalog/product'
));
$this
->
adminSession
(
array
(
'catalog/product
s
'
));
$this
->
assertTrue
(
Mage
::
getSingleton
(
'admin/session'
)
->
isAllowed
(
'catalog/product'
));
$this
->
assertFalse
(
Mage
::
getSingleton
(
'admin/session'
)
->
isAllowed
(
'catalog/category'
));
$this
->
assertTrue
(
Mage
::
getSingleton
(
'admin/session'
)
->
isLoggedIn
());
$this
->
assertTrue
(
Mage
::
getSingleton
(
'admin/session'
)
->
isAllowed
(
'catalog/products'
));
$this
->
assertFalse
(
Mage
::
getSingleton
(
'admin/session'
)
->
isAllowed
(
'catalog/categories'
));
$this
->
assertFalse
(
Mage
::
getSingleton
(
'admin/session'
)
->
isAllowed
(
'sales/order'
));
$this
->
assertFalse
(
Mage
::
getSingleton
(
'admin/session'
)
->
isAllowed
(
'system/config'
));
}
...
...
app/code/community/EcomDev/PHPUnitTest/Test/Helper/_data/dp-testCustomerSession.yaml
0 → 100644
View file @
53633a2d
john_doe
:
-
1
jane_doe
:
-
2
unknown_customer
:
-
3
\ No newline at end of file
app/code/community/EcomDev/PHPUnitTest/Test/Helper/_data/ex-testCustomerSession.yaml
0 → 100644
View file @
53633a2d
john_doe
:
name
:
John Doe
is_logged_in
:
true
jane_doe
:
name
:
Jane Doe
is_logged_in
:
true
unknown_customer
:
is_logged_in
:
false
\ No newline at end of file
app/code/community/EcomDev/PHPUnitTest/Test/Helper/_data/fx-customers.yaml
0 → 100644
View file @
53633a2d
eav
:
customer
:
-
entity_id
:
1
# Customer with default billing and shipping addresses
attribute_set_id
:
0
# Fix issue with customer entity
website_id
:
1
store_id
:
1
created_in
:
Default Store
group_id
:
1
firstname
:
John
lastname
:
Doe
email
:
john@doe.com
pasword_hash
:
d1bcc360d05a6bdd7484784f43660c88:hG
default_billing
:
1
default_shipping
:
1
-
entity_id
:
2
# Customer without addresses
attribute_set_id
:
0
# Fix issue with customer entity
website_id
:
1
store_id
:
1
created_in
:
Default Store
group_id
:
1
firstname
:
Jane
lastname
:
Doe
email
:
jane@doe.com
pasword_hash
:
d1bcc360d05a6bdd7484784f43660c88:hG
customer_address
:
-
entity_id
:
1
# Let him to be a guy from Nevada
attribute_set_id
:
0
# Fix issue with customer address entity
customer_id
:
1
parent_id
:
1
firstname
:
John
lastname
:
Doe
country_id
:
US
region_id
:
39
region_code
:
NV
region
:
Nevada
postcode
:
89001
city
:
Alamo
telephone
:
555-55-55
street
:
Address 123
\ No newline at end of file
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