Example #1
0
 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;
 }
Example #2
0
 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');
 }
Example #3
0
 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;
 }
Example #4
0
 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);
     });
 }
Example #5
0
 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');
     });
 }
Example #6
0
 protected function configureAop(AspectContainer $container)
 {
     Registry::setMocker(new Core\Mocker());
 }
Example #7
0
 public function getCallsForMethod($method)
 {
     $calls = Registry::getClassCallsFor($this->getClassName());
     return isset($calls[$method]) ? $calls[$method] : [];
 }
Example #8
0
 public static function cleanInvocations()
 {
     Core\Registry::cleanInvocations();
 }
Example #9
0
 /**
  * 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);
 }
Example #10
0
 public function getCallsForMethod($func)
 {
     $calls = Registry::getFuncCallsFor($this->ns . '\\' . $func);
     return $calls;
 }
Example #11
0
 public function testMagicStatic()
 {
     double::registerClass('\\demo\\UserModel', ['defaultRole' => 'admin']);
     $this->assertEquals('admin', UserModel::defaultRole());
 }
Example #12
0
 protected function userProxy()
 {
     $userProxy = new ClassProxy('demo\\UserModel');
     double::registerClass('demo\\UserModel');
     return $userProxy;
 }
Example #13
0
 public function getCallsForMethod($method)
 {
     $calls = Registry::getInstanceCallsFor($this->instance);
     return isset($calls[$method]) ? $calls[$method] : [];
 }
Example #14
0
 /**
  * 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);
 }