Skip to content

Commit

Permalink
Store overrides in instances and not solely in manager (remove manager)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszdurka committed Feb 11, 2017
1 parent ef0c76f commit 9ac7da9
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 124 deletions.
3 changes: 0 additions & 3 deletions source/Mocka/Classes/ClassDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Mocka\Classes;

use Mocka\Invokables\Invokable\AbstractInvokable;
use Mocka\Invokables\Invokable\Spy;
use Mocka\Invokables\Invokable\Stub;
use Mocka\Overrides\OverridableInterface;

class ClassDefinition {
Expand Down
23 changes: 14 additions & 9 deletions source/Mocka/Classes/ClassMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Mocka\Invokables\Invokable\Stub;
use Mocka\Overrides\Manager;
use Mocka\Overrides\MethodOverrides\ClassOverrides;
use Mocka\Overrides\OverridableInterface;

class ClassMock {

Expand All @@ -19,8 +20,10 @@ class ClassMock {
/** @var string|null */
private $_parentClassName;

/** @var ClassOverrides */
private $_overrides;

/**
* ClassMock constructor.
* @param string $wrapperClassName
* @param string|null $className
*/
Expand All @@ -30,6 +33,7 @@ public function __construct($wrapperClassName, $className = null) {
$className = $wrapperClassName . '\\Mocka' . uniqid();
}
$this->_extractNameAndNamespace($className);
$this->_overrides = new ClassOverrides($this->getClassName());
}

/**
Expand Down Expand Up @@ -87,6 +91,7 @@ public function generateCode() {
if ($this->_parentClassName) {
$class->setParentClassName($this->_parentClassName);
}
$class->addInterface(OverridableInterface::class);
return $class->dump();
}

Expand All @@ -96,25 +101,21 @@ public function generateCode() {
* @return Stub
*/
public function mockMethod($name) {
$manager = Manager::getInstance();
$classOverrides = new ClassOverrides($manager, $this->getClassName());
return $classOverrides->stub($name);
return $this->_overrides->stub($name);
}

/**
* @param string $name
*/
public function unmockMethod($name) {
$manager = Manager::getInstance();
$classOverrides = new ClassOverrides($manager, $this->getClassName());
$classOverrides->remove($name);
$this->_overrides->remove($name);
}



public function load() {
$code = $this->generateCode();
eval($code);
$class = new \ReflectionClass($this->getClassName());
$class->getMethod('setClassOverrides')->invoke(null, $this->_overrides);
}

/**
Expand All @@ -128,4 +129,8 @@ private function _extractNameAndNamespace($className) {
$this->_namespace = join('\\', $parts);
}
}

public function getOverrides() {
return $this->_overrides;
}
}
59 changes: 50 additions & 9 deletions source/Mocka/Classes/ClassMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,37 @@
use Mocka\Invokables\Invokable\Stub;
use Mocka\Overrides\MethodOverrides\ClassOverrides;
use Mocka\Overrides\MethodOverrides\InstanceOverrides;
use Mocka\Overrides\Manager;

trait ClassMockTrait {

/** @var InstanceOverrides */
private $_overrides;

/** @var ClassOverrides */
public static $_classOverrides;

/**
* @return InstanceOverrides
*/
public function getOverrides() {
$manager = Manager::getInstance();
return new InstanceOverrides($manager, $this);
if (null === $this->_overrides) {
$this->_overrides = new InstanceOverrides($this);
}
return $this->_overrides;
}

/**
* @return ClassOverrides
*/
public static function getClassOverrides() {
return self::$_classOverrides;
}

/**
* @param ClassOverrides $classOverrides
*/
public static function setClassOverrides(ClassOverrides $classOverrides) {
self::$_classOverrides = $classOverrides;
}

/**
Expand All @@ -36,6 +57,26 @@ public function unmockMethod($name) {
$this->getOverrides()->remove($name);
}

public function __clone() {
$this->_overrides = clone $this->_overrides;
}

/**
* @param string $methodName
* @return Stub
*/
public function stub($methodName) {
return $this->getOverrides()->stub($methodName);
}

/**
* @param string $methodName
* @return Spy
*/
public function spy($methodName) {
return $this->getOverrides()->spy($methodName);
}

/**
* @param string $name
* @param array $arguments
Expand All @@ -47,8 +88,7 @@ private function _callMethod($name, array $arguments) {
$originalMethod = $classDefinition->findOriginalMethod($name);

$override = $this->getOverrides()->find($name);



if ($override) {
$invokable = $override->getInvokable();
if ($invokable instanceof Spy) {
Expand All @@ -58,13 +98,15 @@ private function _callMethod($name, array $arguments) {
}
$invokable->addInvocation($this, $arguments, $returnValue);
return $returnValue;
}
}
if ($invokable instanceof Stub) {
return $invokable->invoke($this, $arguments);
}
throw new Exception('Unsupported invokable');
}
if ($originalMethod) {
if ('__construct' === $name) {
}
return call_user_func_array($originalMethod, $arguments);
}

Expand All @@ -83,9 +125,8 @@ private function _callMethod($name, array $arguments) {
private static function _callStaticMethod($name, array $arguments) {
$classDefinition = new ClassDefinition(get_called_class());
$originalMethod = $classDefinition->findOriginalMethod($name);

$manager = Manager::getInstance();
$classOverrides = new ClassOverrides($manager, get_called_class());

$classOverrides = self::getClassOverrides();

$override = $classOverrides->find($name);
if ($override) {
Expand Down
12 changes: 10 additions & 2 deletions source/Mocka/Classes/ClassWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function generateCode() {
foreach ($this->_interfaces as $interface) {
$class->addInterface($interface);
}
$class->addInterface(OverridableInterface::class);
foreach ($this->_traits as $trait) {
$reflectionTrait = new \ReflectionClass($trait);
$trait = new TraitBlock($trait);
Expand All @@ -61,7 +60,16 @@ public function generateCode() {
}
$class->addUse($trait);
}
$class->addUse(new TraitBlock('\Mocka\Classes\ClassMockTrait'));

$skipTrait = false;
if ($this->_parentClassName) {
$parentClass = new \ReflectionClass($this->_parentClassName);
$skipTrait = $parentClass->implementsInterface(OverridableInterface::class);
}

if (!$skipTrait) {
$class->addUse(new TraitBlock('\Mocka\Classes\ClassMockTrait'));
}

$mockableMethods = $this->_getMockableMethods();
foreach ($mockableMethods as $reflectionMethod) {
Expand Down
5 changes: 5 additions & 0 deletions source/Mocka/Overrides/Context/AbstractContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ abstract public function equals(AbstractContext $context);
*/
abstract public function getClassName();

/**
* @return string
*/
abstract public function getMethodName();

}
54 changes: 0 additions & 54 deletions source/Mocka/Overrides/Manager.php

This file was deleted.

52 changes: 38 additions & 14 deletions source/Mocka/Overrides/MethodOverrides/AbstractOverrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
use Mocka\Invokables\Invokable\Spy;
use Mocka\Invokables\Invokable\Stub;
use Mocka\Overrides\Context\AbstractContext;
use Mocka\Overrides\Manager;
use Mocka\Overrides\Override;

abstract class AbstractOverrides {

/** @var Manager */
protected $_manager;
/** @var Override[] */
protected $_overrides;

/**
* @param $methodName
Expand All @@ -27,11 +26,21 @@ abstract public function find($methodName);
*/
abstract protected function _createContext($name);

public function __construct() {
$this->_overrides = [];
}

public function __clone() {
foreach ($this->_overrides as $key => $override) {
$this->_overrides[$key] = clone $override;
}
}

/**
* @param Manager $manager
* @return Override[]
*/
public function __construct(Manager $manager) {
$this->_manager = $manager;
public function findAll() {
return $this->_overrides;
}

/**
Expand All @@ -51,8 +60,9 @@ public function get($methodName) {
* @param string $methodName
*/
public function remove($methodName) {
$context = $this->_createContext($methodName);
$this->_manager->removeByContext($context);
$this->_overrides = \Functional\reject($this->_overrides, function (Override $override) use ($methodName) {
return $override->getContext()->getMethodName() === $methodName;
});
}

/**
Expand All @@ -61,11 +71,9 @@ public function remove($methodName) {
*/
public function stub($methodName) {
$context = $this->_createContext($methodName);
$this->_manager->removeByContext($context);

$invokable = new Stub();
$override = new Override($context, $invokable);
$this->_manager->add($override);
$this->_add($override);
return $invokable;
}

Expand All @@ -75,11 +83,27 @@ public function stub($methodName) {
*/
public function spy($methodName) {
$context = $this->_createContext($methodName);
$this->_manager->removeByContext($context);

$invokable = new Spy();
$override = new Override($context, $invokable);
$this->_manager->add($override);
$this->_add($override);
return $invokable;
}

/**
* @param Override $override
*/
protected function _add(Override $override) {
$this->remove($override->getContext()->getMethodName());
$this->_overrides[] = $override;
}

/**
* @param string $methodName
* @return Override|null
*/
protected function _find($methodName) {
return \Functional\first($this->_overrides, function (Override $override) use ($methodName) {
return $override->getContext()->getMethodName() === $methodName;
});
}
}
Loading

0 comments on commit 9ac7da9

Please sign in to comment.