Skip to content

Commit f4397a1

Browse files
committed
Add shortcut for replaceByMock method into mock proxy, so less code needs to be written
1 parent 460f4a4 commit f4397a1

4 files changed

Lines changed: 49 additions & 1 deletion

File tree

app/code/community/EcomDev/PHPUnit/Test/Case.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*
2525
* @method EcomDev_PHPUnit_Mock_Proxy mockClassAlias(string $type, $classAlias, array $methods = array(), array $constructorArgs = array())
2626
* @method EcomDev_PHPUnit_Mock_Proxy mockModel($classAlias, array $methods = array(), array $constructorArgs = array())
27+
* @method EcomDev_PHPUnit_Mock_Proxy mockResourceModel($classAlias, array $methods = array(), array $constructorArgs = array())
2728
* @method EcomDev_PHPUnit_Mock_Proxy mockBlock($classAlias, array $methods = array(), array $constructorArgs = array())
2829
* @method EcomDev_PHPUnit_Mock_Proxy mockHelper($classAlias, array $methods = array(), array $constructorArgs = array())
2930
* @method EcomDev_PHPUnit_Mock_Proxy mockSession($classAlias, array $methods = array())

app/code/community/EcomDev/PHPUnit/Test/Case/Helper/Mock.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ public function helperMockModel($classAlias, array $methods = array(), array $co
5555
return $this->helperMockClassAlias('model', $classAlias, $methods, $constructorArgs);
5656
}
5757

58+
/**
59+
* Creates a mock for a resource model by its class alias
60+
*
61+
* @param string $classAlias
62+
* @param array $methods
63+
* @param array $constructorArgs
64+
*
65+
* @return EcomDev_PHPUnit_Mock_Proxy
66+
*/
67+
public function helperMockResourceModel($classAlias, array $methods = array(), array $constructorArgs = array())
68+
{
69+
return $this->helperMockClassAlias('resource_model', $classAlias, $methods, $constructorArgs);
70+
}
71+
72+
5873
/**
5974
* Creates a mock for a block by its class alias
6075
*

app/code/community/EcomDev/PHPUnit/Test/Case/Util.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ public static function getGroupedClassName($type, $classAlias)
501501
public static function getGroupedClassMockBuilder(PHPUnit_Framework_TestCase $testCase, $type, $classAlias)
502502
{
503503
$className = self::getGroupedClassName($type, $classAlias);
504-
return new EcomDev_PHPUnit_Mock_Proxy($testCase, $className);
504+
return new EcomDev_PHPUnit_Mock_Proxy($testCase, $className, $classAlias);
505505
}
506506

507507
/**

lib/EcomDev/PHPUnit/Mock/Proxy.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ class EcomDev_PHPUnit_Mock_Proxy
2929
{
3030
protected $mockInstance;
3131

32+
/**
33+
* Original mocked class alias
34+
*
35+
* @var string
36+
*/
37+
protected $classAlias;
38+
39+
/**
40+
* Added class alias as property
41+
*
42+
* @param PHPUnit_Framework_TestCase $testCase
43+
* @param array|string $type
44+
* @param null $classAlias
45+
*/
46+
public function __construct(PHPUnit_Framework_TestCase $testCase, $type, $classAlias = null)
47+
{
48+
parent::__construct($testCase, $type);
49+
$this->classAlias = $classAlias;
50+
}
51+
3252
/**
3353
* Adds method name to a mock builder
3454
*
@@ -113,6 +133,18 @@ public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
113133
return $this->getMockInstance()->expects($matcher);
114134
}
115135

136+
/**
137+
* Invokes replaceByMock test util method with current mock object proxy instance
138+
*
139+
* @param $type
140+
* @return $this
141+
*/
142+
public function replaceByMock($type)
143+
{
144+
EcomDev_PHPUnit_Test_Case_Util::replaceByMock($type, $this->classAlias, $this);
145+
return $this;
146+
}
147+
116148
/**
117149
* Returns invocation mocker for
118150
*

0 commit comments

Comments
 (0)