forked from BedrockStreaming/RedisMock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request BedrockStreaming#7 from M6Web/more-flexibility
Adding possibility to instantiate itself the mock
- Loading branch information
Showing
3 changed files
with
113 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
* The mock can have undesired behavior if your parent class uses unsupported features. | ||
* | ||
* @author Adrien Samson <[email protected]> | ||
* @author Florent Dubost <[email protected]> | ||
*/ | ||
class RedisMockFactory | ||
{ | ||
|
@@ -158,24 +159,35 @@ class RedisMockFactory | |
); | ||
|
||
protected $classTemplate = <<<'CLASS' | ||
namespace {{namespace}}; | ||
class {{class}} extends \{{baseClass}} | ||
{ | ||
protected $mock; | ||
public function __construct($mock) | ||
protected $clientMock; | ||
public function setClientMock($clientMock) | ||
{ | ||
$this->clientMock = $clientMock; | ||
} | ||
public function getClientMock() | ||
{ | ||
$this->mock = $mock; | ||
if (!isset($this->clientMock)) { | ||
$this->clientMock = new RedisMock(); | ||
} | ||
return $this->clientMock; | ||
} | ||
public function __call($method, $args) | ||
{ | ||
$methodName = strtolower($method); | ||
if (!method_exists('M6Web\Component\RedisMock\RedisMock', $methodName)) { | ||
throw new \M6Web\Component\RedisMock\UnsupportedException(sprintf('Redis command `%s` is not supported by RedisMock.', $methodName)); | ||
throw new UnsupportedException(sprintf('Redis command `%s` is not supported by RedisMock.', $methodName)); | ||
} | ||
return call_user_func_array(array($this->mock, $methodName), $args); | ||
return call_user_func_array(array($this->getClientMock(), $methodName), $args); | ||
} | ||
{{methods}} | ||
} | ||
|
@@ -185,38 +197,61 @@ public function __call($method, $args) | |
public function {{method}}({{signature}}) | ||
{ | ||
return $this->mock->{{method}}({{args}}); | ||
return $this->getClientMock()->{{method}}({{args}}); | ||
} | ||
|
||
METHOD; | ||
|
||
protected $constructorTemplate = <<<'CONSTRUCTOR' | ||
public function getAdapter($classToExtend, $redisMock) | ||
public function __construct() | ||
{ | ||
$newClassName = sprintf('RedisMock_%s_Adapter', str_replace('\\', '_', $classToExtend)); | ||
$namespace = __NAMESPACE__; | ||
$class = $namespace . '\\'. $newClassName; | ||
} | ||
CONSTRUCTOR; | ||
|
||
|
||
if (class_exists($class)) { | ||
return new $class($redisMock); | ||
public function getAdapter($classToExtend) | ||
{ | ||
list($namespace, $newClassName, $class) = $this->getAdapterClassName($classToExtend); | ||
|
||
if (!class_exists($class)) { | ||
$classCode = $this->getClassCode($namespace, $newClassName, new \ReflectionClass($classToExtend), true); | ||
eval($classCode); | ||
} | ||
|
||
return new $class(); | ||
} | ||
|
||
public function getAdapterClass($classToExtend) | ||
{ | ||
list($namespace, $newClassName, $class) = $this->getAdapterClassName($classToExtend, '_NativeConstructor'); | ||
|
||
if (!class_exists($class)) { | ||
$classCode = $this->getClassCode($namespace, $newClassName, new \ReflectionClass($classToExtend)); | ||
eval($classCode); | ||
} | ||
|
||
$classCode = $this->getClassCode($namespace, $newClassName, new \ReflectionClass($classToExtend)); | ||
return $class; | ||
} | ||
|
||
eval($classCode); | ||
protected function getAdapterClassName($classToExtend, $suffix = '') | ||
{ | ||
$newClassName = sprintf('RedisMock_%s_Adapter%s', str_replace('\\', '_', $classToExtend), $suffix); | ||
$namespace = __NAMESPACE__; | ||
$class = $namespace . '\\'. $newClassName; | ||
|
||
return new $class($redisMock); | ||
return [$namespace, $newClassName, $class]; | ||
} | ||
|
||
protected function getClassCode($namespace, $newClassName, \ReflectionClass $class) | ||
protected function getClassCode($namespace, $newClassName, \ReflectionClass $class, $orphanizeConstructor = false) | ||
{ | ||
$methodsCode = ''; | ||
$methodsCode = $orphanizeConstructor ? $this->constructorTemplate : ''; | ||
|
||
foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { | ||
$methodName = strtolower($method->getName()); | ||
|
||
if (!method_exists('M6Web\Component\RedisMock\RedisMock', $methodName) && in_array($methodName, $this->redisCommands)) { | ||
throw new \M6Web\Component\RedisMock\UnsupportedException(sprintf('Redis command `%s` is not supported by RedisMock.', $methodName)); | ||
throw new UnsupportedException(sprintf('Redis command `%s` is not supported by RedisMock.', $methodName)); | ||
} elseif (method_exists('M6Web\Component\RedisMock\RedisMock', $methodName)) { | ||
$methodsCode .= strtr($this->methodTemplate, array( | ||
'{{method}}' => $methodName, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters