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
7e2a9459
Commit
7e2a9459
authored
Feb 01, 2013
by
Ivan Chepurnyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
! Initial version of mock proxy and helpers
parent
a09c086d
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
593 additions
and
1 deletion
+593
-1
app/code/community/EcomDev/PHPUnit/Test/Case/Util.php
app/code/community/EcomDev/PHPUnit/Test/Case/Util.php
+3
-1
app/code/community/EcomDev/PHPUnitTest/Test/Lib/Helper/Abstract.php
...ommunity/EcomDev/PHPUnitTest/Test/Lib/Helper/Abstract.php
+107
-0
app/code/community/EcomDev/PHPUnitTest/Test/Lib/Mock/Proxy.php
...ode/community/EcomDev/PHPUnitTest/Test/Lib/Mock/Proxy.php
+150
-0
lib/EcomDev/PHPUnit/Helper/Abstract.php
lib/EcomDev/PHPUnit/Helper/Abstract.php
+96
-0
lib/EcomDev/PHPUnit/Helper/Interface.php
lib/EcomDev/PHPUnit/Helper/Interface.php
+49
-0
lib/EcomDev/PHPUnit/Mock/Proxy.php
lib/EcomDev/PHPUnit/Mock/Proxy.php
+188
-0
No files found.
app/code/community/EcomDev/PHPUnit/Test/Case/Util.php
View file @
7e2a9459
...
...
@@ -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
(
...
...
app/code/community/EcomDev/PHPUnitTest/Test/Lib/Helper/Abstract.php
0 → 100644
View file @
7e2a9459
<?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
app/code/community/EcomDev/PHPUnitTest/Test/Lib/Mock/Proxy.php
0 → 100644
View file @
7e2a9459
<?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
lib/EcomDev/PHPUnit/Helper/Abstract.php
0 → 100644
View file @
7e2a9459
<?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
lib/EcomDev/PHPUnit/Helper/Interface.php
0 → 100644
View file @
7e2a9459
<?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
);
}
lib/EcomDev/PHPUnit/Mock/Proxy.php
0 → 100644
View file @
7e2a9459
<?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
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