public function fakeMethodsAndRegisterCalls($class, $declaredClass, $method, $params, $static) { // $method = $invocation->getMethod(); // $obj = $invocation->getThis(); $result = __AM_CONTINUE__; if (in_array($method, $this->methodMap)) { $invocation = new \AspectMock\Intercept\MethodInvocation(); $invocation->setThis($class); $invocation->setMethod($method); $invocation->setArguments($params); $invocation->isStatic($static); $invocation->setDeclaredClass($declaredClass); $result = $this->invokeFakedMethods($invocation); } if (!$static) { 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); } return $result; }
public function testVerifyClassMethodCalled() { $user = new UserModel(); $userProxy = new ClassProxy('demo\\UserModel'); double::registerClass('demo\\UserModel'); $user->setName('davert'); $user->setName('jon'); $userProxy->verifyInvokedMultipleTimes('setName', 2); $userProxy->verifyInvokedOnce('setName', ['jon']); $userProxy->verifyNeverInvoked('save'); $userProxy->verifyNeverInvoked('setName', ['bob']); verify($user->getName())->equals('jon'); }
public function fakeFunctionAndRegisterCalls($namespace, $function, $args) { $result = __AM_CONTINUE__; $fullFuncName = "{$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; } } return $result; }
public function testVerifyInvocationClosures() { $info = array('address' => 'foo', 'email' => '*****@*****.**'); $user = new UserModel(); double::registerObject($user); $user = new InstanceProxy($user); $user->setInfo($info); $user->setInfo([]); $matcher = function ($params) use($info) { $args = $params[0][0]; // first call, first arg $empty = $params[1][0]; // second call, first arg verify($info)->equals($args); verify($empty)->isEmpty(); }; $this->specify('closure was called', function () use($user, $info, $matcher) { $user->verifyInvokedMultipleTimes('setInfo', 2, $matcher); $user->verifyInvoked('setInfo', $matcher); }); }
public function testVerifyMagicMethods() { $this->specify('works for class proxy', function () { // Set up user object. double::registerClass("demo\\UserModel", ['renameUser' => "Bob Jones", 'save' => null]); $userProxy = new ClassProxy("demo\\UserModel"); $user = new UserModel(['name' => "John Smith"]); // Rename the user via magic method. UserService::renameStatic($user, "Bob Jones"); // Assert rename was counted. $userProxy->verifyInvoked('renameUser'); }); $this->specify('works for instance proxy', function () { // Set up user object. $user = new UserModel(['name' => "John Smith"]); double::registerObject($user); $user = new InstanceProxy($user); // Rename the user via magic method. $user->renameUser("Bob Jones"); // Assert rename was counted. $user->verifyInvoked('renameUser'); }); }
protected function configureAop(AspectContainer $container) { Registry::setMocker(new Core\Mocker()); }
public function getCallsForMethod($method) { $calls = Registry::getClassCallsFor($this->getClassName()); return isset($calls[$method]) ? $calls[$method] : []; }
public static function cleanInvocations() { Core\Registry::cleanInvocations(); }
/** * Adds a namespace / namespaces for classes to be searched. * Useful if you have long namespaces and classes there. * * ``` php * <?php * test::ns('Company\App\ProjectBundle'); * test::double('Entity\User'); // => Company\App\ProjectBundle\Entity\User * * ?> * ``` * Using `ns` helps in refactoring: test doubles do not depend on long class names. * * When declared in `test::double` not exists, AspectMock will try to match it by prepending a namespace. * To ignore namespace guessing, use `\` in the beginning of class name: `\User`; * */ public static function ns($namespace) { Registry::setNamespace($namespace); }
public function getCallsForMethod($func) { $calls = Registry::getFuncCallsFor($this->ns . '\\' . $func); return $calls; }
public function testMagicStatic() { double::registerClass('\\demo\\UserModel', ['defaultRole' => 'admin']); $this->assertEquals('admin', UserModel::defaultRole()); }
protected function userProxy() { $userProxy = new ClassProxy('demo\\UserModel'); double::registerClass('demo\\UserModel'); return $userProxy; }
public function getCallsForMethod($method) { $calls = Registry::getInstanceCallsFor($this->instance); return isset($calls[$method]) ? $calls[$method] : []; }
/** * Replaces all methods in a class with dummies, except those specified in the `$only` param. * * ``` php * <?php * $user = new User(['name' => 'jon']); * test::methods($user, ['getName']); * $user->setName('davert'); // not invoked * $user->getName(); // jon * ?> * ``` * * You can create a dummy without a constructor with all methods disabled: * * ``` php * <?php * $user = test::double('User')->make(); * test::methods($user, []); * ?> * ``` * * @api * @param string|object $classOrObject * @param string[] $only * @return Verifier Usually Proxy\ClassProxy|Proxy\InstanceProxy * @throws \Exception */ public static function methods($classOrObject, array $only = array()) { $classOrObject = Registry::getRealClassOrObject($classOrObject); if (is_object($classOrObject)) { $reflected = new \ReflectionClass(get_class($classOrObject)); } else { if (!class_exists($classOrObject)) { throw new \Exception("Class {$classOrObject} not defined."); } $reflected = new \ReflectionClass($classOrObject); } $methods = $reflected->getMethods(\ReflectionMethod::IS_PUBLIC); $params = array(); foreach ($methods as $m) { if ($m->isConstructor()) { continue; } if ($m->isDestructor()) { continue; } if (in_array($m->name, $only)) { continue; } $params[$m->name] = null; } return self::double($classOrObject, $params); }