Ejemplo n.º 1
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');
 }
Ejemplo n.º 2
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');
     });
 }
Ejemplo n.º 3
0
 /**
  * test::double registers class or object to track its calls.
  * In second argument you may pass values that mocked mathods should return.
  *
  * Returns either of [**ClassProxy**](https://github.com/Codeception/AspectMock/blob/master/docs/ClassProxy.md)
  * or [**InstanceProxy**](https://github.com/Codeception/AspectMock/blob/master/docs/InstanceProxy.md).
  * Proxies are used to verify method invocations, and some other useful things.
  *
  * Example:
  *
  * ``` php
  * <?php
  *
  * # simple
  * $user = test::double(new User, ['getName' => 'davert']);
  * $user->getName() // => davert
  * $user->verifyInvoked('getName'); // => success
  *
  * # with closure
  * $user = test::double(new User, ['getName' => function() { return $this->login; }]);
  * $user->login = '******';
  * $user->getName(); // => davert
  *
  * # on a class
  * $ar = test::double('ActiveRecord', ['save' => null]);
  * $user = new User;
  * $user->name = 'davert';
  * $user->save(); // passes to ActiveRecord->save() and does not insert any SQL.
  * $ar->verifyInvoked('save'); // true
  *
  * # on static method call
  * User::tableName(); // 'users'
  * $user = test::double('User', ['tableName' => 'fake_users']);
  * User::tableName(); // 'fake_users'
  * $user->verifyInvoked('tableName'); // success
  *
  * # append declaration
  * $user = new User;
  * test::double($user, ['getName' => 'davert']);
  * test::double($user, ['getEmail' => '*****@*****.**']);
  * $user->getName(); // => 'davert'
  * $user->getEmail(); => '*****@*****.**'
  *
  * # create an instance of mocked class
  * test::double('User')->construct(['name' => 'davert']); // via constructir
  * test::double('User')->make(); // without calling constructor
  * 
  * # stub for magic method
  * test::double('User', ['findByUsernameAndPasswordAndEmail' => false]);
  * User::findByUsernameAndPasswordAndEmail; // null
  * 
  * # stub for method of parent class
  * # if User extends ActiveRecord
  *
  * test::double('ActiveRecord', ['save' => false]);
  * $user = new User(['name' => 'davert']);
  * $user->save(); // false
  *
  * ?>
  * ```
  *
  * @api
  * @param $classOrObject
  * @param array $params
  * @throws \Exception
  * @return Proxy\ClassProxy|null
  */
 public static function double($classOrObject, $params = array())
 {
     $classOrObject = Registry::getRealClassOrObject($classOrObject);
     if (is_string($classOrObject)) {
         if (!class_exists($classOrObject)) {
             throw new \Exception("Class {$classOrObject} not loaded.\nIf you want to test undefined class use 'test::spec' method");
         }
         if (!\class_exists($classOrObject)) {
             $classOrObject = Registry::getNamespace() . '\\' . $classOrObject;
         }
         Core\Registry::registerClass($classOrObject, $params);
         return new Proxy\ClassProxy($classOrObject);
     }
 }
Ejemplo n.º 4
0
 public function testMagicStatic()
 {
     double::registerClass('\\demo\\UserModel', ['defaultRole' => 'admin']);
     $this->assertEquals('admin', UserModel::defaultRole());
 }
Ejemplo n.º 5
0
 protected function userProxy()
 {
     $userProxy = new ClassProxy('demo\\UserModel');
     double::registerClass('demo\\UserModel');
     return $userProxy;
 }