Skip to content

Commit

Permalink
Update codebase to PHP 7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
TavoNiievez committed Oct 30, 2021
1 parent 124fa59 commit 76d044b
Show file tree
Hide file tree
Showing 28 changed files with 381 additions and 377 deletions.
1 change: 1 addition & 0 deletions RoboFile.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

require_once 'vendor/autoload.php';

class Robofile extends \Robo\Tasks
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "codeception/aspect-mock",
"description": "Experimental Mocking Framework powered by Aspects",
"license": "MIT",
"authors": [
{
"name": "Michael Bodnarchuk",
Expand All @@ -21,7 +22,7 @@
"require-dev": {
"codeception/codeception": "^4.1",
"codeception/verify": "^2.1",
"codeception/specify": "^1.4"
},
"license": "MIT"
"codeception/specify": "^1.4",
"consolidation/robo": "^3.0"
}
}
80 changes: 50 additions & 30 deletions src/AspectMock/Core/Mocker.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
<?php

declare(strict_types=1);

namespace AspectMock\Core;

use AspectMock\Intercept\FunctionInjector;
use Go\Aop\Aspect;
use AspectMock\Intercept\MethodInvocation;
use Closure;
use Go\Aop\Aspect;

class Mocker implements Aspect
{

protected $classMap = [];
protected $objectMap = [];
protected $funcMap = [];
protected $methodMap = ['__call', '__callStatic'];
protected $dynamicMethods = ['__call', '__callStatic'];
protected array $classMap = [];

protected array $objectMap = [];

protected array $funcMap = [];

protected array $methodMap = ['__call', '__callStatic'];

protected array $dynamicMethods = ['__call', '__callStatic'];

public function fakeMethodsAndRegisterCalls($class, $declaredClass, $method, $params, $static)
{
$result = __AM_CONTINUE__;
$invocation = null;

if (in_array($method, $this->methodMap)) {
$invocation = new \AspectMock\Intercept\MethodInvocation();
$invocation = new MethodInvocation();
$invocation->setThis($class);
$invocation->setMethod($method);
$invocation->setArguments($params);
Expand All @@ -39,40 +46,42 @@ public function fakeMethodsAndRegisterCalls($class, $declaredClass, $method, $pa
if (isset($this->objectMap[spl_object_hash($class)])) {
Registry::registerInstanceCall($class, $method, $params);
}

$class = get_class($class);
}

if (isset($this->classMap[$class])) {
Registry::registerClassCall($class, $method, $params);
}

if ($class != $declaredClass && isset($this->classMap[$declaredClass])) {
Registry::registerClassCall($declaredClass, $method, $params);
}

if ($invocation instanceof \AspectMock\Intercept\MethodInvocation) {
if ($invocation instanceof MethodInvocation) {
$result = $this->invokeFakedMethods($invocation);
}

return $result;
}

public function fakeFunctionAndRegisterCalls($namespace, $function, $args)
{
$result = __AM_CONTINUE__;
$fullFuncName = "$namespace\\$function";
$fullFuncName = sprintf('%s\%s', $namespace, $function);
Registry::registerFunctionCall($fullFuncName, $args);

if (isset($this->funcMap[$fullFuncName])) {
$func = $this->funcMap[$fullFuncName];
if (is_callable($func)) {
$result = call_user_func_array($func, $args);
} else {
$result = $func;
}
$result = is_callable($func) ? call_user_func_array($func, $args) : $func;
}

return $result;
}

/**
* @return mixed
*/
protected function invokeFakedMethods(MethodInvocation $invocation)
{
$method = $invocation->getMethod();
Expand Down Expand Up @@ -155,6 +164,7 @@ protected function invokeFakedMethods(MethodInvocation $invocation)
}
}
}

return __AM_CONTINUE__;
}

Expand All @@ -164,10 +174,12 @@ protected function getObjectMethodStubParams($obj, $method_name)
if (!isset($this->objectMap[$oid])) {
return false;
}

$params = $this->objectMap[$oid];
if (!array_key_exists($method_name, $params)) {
return false;
}

return $params;
}

Expand All @@ -176,10 +188,12 @@ protected function getClassMethodStubParams($class_name, $method_name)
if (!isset($this->classMap[$class_name])) {
return false;
}

$params = $this->classMap[$class_name];
if (!array_key_exists($method_name, $params)) {
return false;
}

return $params;
}

Expand All @@ -192,14 +206,15 @@ protected function stub(MethodInvocation $invocation, $params)
$replacedMethod = $this->turnToClosure($replacedMethod);

if ($invocation->isStatic()) {
$replacedMethod = \Closure::bind($replacedMethod, null, $invocation->getThis());
$replacedMethod = Closure::bind($replacedMethod, null, $invocation->getThis());
} else {
$replacedMethod = $replacedMethod->bindTo($invocation->getThis(), get_class($invocation->getThis()));
}

return call_user_func_array($replacedMethod, $invocation->getArguments());
}

protected function stubMagicMethod(MethodInvocation $invocation, $params)
protected function stubMagicMethod(MethodInvocation $invocation, array $params)
{
$args = $invocation->getArguments();
$name = array_shift($args);
Expand All @@ -208,56 +223,62 @@ protected function stubMagicMethod(MethodInvocation $invocation, $params)
$replacedMethod = $this->turnToClosure($replacedMethod);

if ($invocation->isStatic()) {
\Closure::bind($replacedMethod, null, $invocation->getThis());
Closure::bind($replacedMethod, null, $invocation->getThis());
} else {
$replacedMethod = $replacedMethod->bindTo($invocation->getThis(), get_class($invocation->getThis()));
}

return call_user_func_array($replacedMethod, $args);
}


protected function turnToClosure($returnValue)
protected function turnToClosure($returnValue): Closure
{
if ($returnValue instanceof \Closure) {
if ($returnValue instanceof Closure) {
return $returnValue;
}
return function () use ($returnValue) {
return $returnValue;
};

return fn() => $returnValue;
}

public function registerClass($class, $params = [])
public function registerClass(string $class, array $params = []): void
{
$class = ltrim($class, '\\');
if (isset($this->classMap[$class])) {
$params = array_merge($this->classMap[$class], $params);
}

$this->methodMap = array_merge($this->methodMap, array_keys($params));
$this->classMap[$class] = $params;
}

public function registerObject($object, $params = [])
public function registerObject(object $object, array $params = []): void
{
$hash = spl_object_hash($object);
if (isset($this->objectMap[$hash])) {
$params = array_merge($this->objectMap[$hash], $params);
}

$this->objectMap[$hash] = $params;
$this->methodMap = array_merge($this->methodMap, array_keys($params));
}

public function registerFunc($namespace, $func, $body)
/**
* @param string|Closure $func
*/
public function registerFunc(string $namespace, $func, $body): void
{
$namespace = ltrim($namespace, '\\');
if (!function_exists("$namespace\\$func")) {
if (!function_exists("{$namespace}\\{$func}")) {
$injector = new FunctionInjector($namespace, $func);
$injector->save();
$injector->inject();
}
$this->funcMap["$namespace\\$func"] = $body;

$this->funcMap["{$namespace}\\{$func}"] = $body;
}

public function clean($objectOrClass = null)
public function clean($objectOrClass = null): void
{
if (!$objectOrClass) {
$this->classMap = [];
Expand All @@ -270,5 +291,4 @@ public function clean($objectOrClass = null)
unset($this->classMap[$objectOrClass]);
}
}

}
59 changes: 28 additions & 31 deletions src/AspectMock/Core/Registry.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

declare(strict_types=1);

namespace AspectMock\Core;

use AspectMock\Proxy\ClassProxy;
use AspectMock\Proxy\InstanceProxy;

Expand All @@ -9,55 +13,50 @@
* Class Registry
* @package AspectMock
*/
class Registry {
class Registry
{
protected static array $classCalls = [];

protected static $classCalls = [];
protected static $instanceCalls = [];
protected static $funcCalls = [];
protected static array $instanceCalls = [];

/**
* @var Mocker
*/
public static $mocker;
protected static array $funcCalls = [];

static function registerClass($name, $params = array())
public static ?Mocker $mocker = null;

public static function registerClass($name, $params = array()): void
{
self::$mocker->registerClass($name, $params);
}

static function registerObject($object, $params = array())
public static function registerObject($object, $params = array()): void
{
self::$mocker->registerObject($object, $params);
}

static function registerFunc($namespace, $function, $resultOrClosure)
public static function registerFunc($namespace, $function, $resultOrClosure): void
{
self::$mocker->registerFunc($namespace, $function, $resultOrClosure);
}

static function getClassCallsFor($class)
public static function getClassCallsFor($class)
{
$class = ltrim($class,'\\');
return isset(self::$classCalls[$class])
? self::$classCalls[$class]
: [];
return self::$classCalls[$class] ?? [];
}

static function getInstanceCallsFor($instance)
public static function getInstanceCallsFor($instance)
{
$oid = spl_object_hash($instance);
return isset(self::$instanceCalls[$oid])
? self::$instanceCalls[$oid]
: [];
return self::$instanceCalls[$oid] ?? [];
}

static function getFuncCallsFor($func)
public static function getFuncCallsFor($func)
{
$func = ltrim($func,'\\');
return isset(self::$funcCalls[$func]) ? self::$funcCalls[$func] : [];
return self::$funcCalls[$func] ?? [];
}

static function clean($classOrInstance = null)
public static function clean($classOrInstance = null): void
{
$classOrInstance = self::getRealClassOrObject($classOrInstance);
self::$mocker->clean($classOrInstance);
Expand All @@ -73,14 +72,14 @@ static function clean($classOrInstance = null)
}
}

static function cleanInvocations()
public static function cleanInvocations(): void
{
self::$instanceCalls = [];
self::$classCalls = [];
self::$funcCalls = [];
}

static function registerInstanceCall($instance, $method, $args = array())
public static function registerInstanceCall($instance, $method, $args = array()): void
{
$oid = spl_object_hash($instance);
if (!isset(self::$instanceCalls[$oid])) self::$instanceCalls[$oid] = [];
Expand All @@ -91,7 +90,7 @@ static function registerInstanceCall($instance, $method, $args = array())

}

static function registerClassCall($class, $method, $args = array())
public static function registerClassCall($class, $method, $args = array()): void
{
if (!isset(self::$classCalls[$class])) self::$classCalls[$class] = [];

Expand All @@ -101,7 +100,7 @@ static function registerClassCall($class, $method, $args = array())

}

static function registerFunctionCall($functionName, $args)
public static function registerFunctionCall($functionName, $args): void
{
if (!isset(self::$funcCalls[$functionName])) self::$funcCalls[$functionName] = [];

Expand All @@ -113,16 +112,14 @@ static function registerFunctionCall($functionName, $args)
public static function getRealClassOrObject($classOrObject)
{
if ($classOrObject instanceof ClassProxy) return $classOrObject->className;

if ($classOrObject instanceof InstanceProxy) return $classOrObject->getObject();

return $classOrObject;
}

/**
* @param mixed $mocker
*/
public static function setMocker(Mocker $mocker)
public static function setMocker(Mocker $mocker): void
{
self::$mocker = $mocker;
}

}
Loading

0 comments on commit 76d044b

Please sign in to comment.