コード例 #1
0
ファイル: Container.php プロジェクト: radeksimko/mockery
 /**
  * Generates a new mock object for this container
  *
  * I apologies in advance for this. A God Method just fits the API which
  * doesn't require differentiating between classes, interfaces, abstracts,
  * names or partials - just so long as it's something that can be mocked.
  * I'll refactor it one day so it's easier to follow.
  *
  * @return \Mockery\Mock
  */
 public function mock()
 {
     $class = null;
     $name = null;
     $partial = null;
     $expectationClosure = null;
     $quickdefs = array();
     $blocks = array();
     $makeInstanceMock = false;
     $args = func_get_args();
     $partialMethods = array();
     if (count($args) > 1) {
         $finalArg = end($args);
         reset($args);
         if (is_callable($finalArg)) {
             $expectationClosure = array_pop($args);
         }
     }
     while (count($args) > 0) {
         $arg = current($args);
         // check for multiple interfaces
         if (is_string($arg) && strpos($arg, ',') && !strpos($arg, ']')) {
             $interfaces = explode(',', str_replace(' ', '', $arg));
             foreach ($interfaces as $i) {
                 if (!interface_exists($i, true) && !class_exists($i, true)) {
                     throw new \Mockery\Exception('Class name follows the format for defining multiple' . ' interfaces, however one or more of the interfaces' . ' do not exist or are not included, or the base class' . ' (optional) does not exist');
                 }
             }
             $class = $interfaces;
             array_shift($args);
         } elseif (is_string($arg) && substr($arg, 0, 6) == 'alias:') {
             $class = 'stdClass';
             $name = array_shift($args);
             $name = str_replace('alias:', '', $name);
         } elseif (is_string($arg) && substr($arg, 0, 9) == 'overload:') {
             $class = 'stdClass';
             $name = array_shift($args);
             $name = str_replace('overload:', '', $name);
             $makeInstanceMock = true;
         } elseif (is_string($arg) && substr($arg, strlen($arg) - 1, 1) == ']') {
             $parts = explode('[', $arg);
             if (!class_exists($parts[0], true) && !interface_exists($parts[0], true)) {
                 throw new \Mockery\Exception('Can only create a partial mock from' . ' an existing class or interface');
             }
             $class = $parts[0];
             $parts[1] = str_replace(' ', '', $parts[1]);
             $partialMethods = explode(',', strtolower(rtrim($parts[1], ']')));
             array_shift($args);
         } elseif (is_string($arg) && (class_exists($arg, true) || interface_exists($arg, true))) {
             $class = array_shift($args);
         } elseif (is_string($arg)) {
             $name = array_shift($args);
         } elseif (is_object($arg)) {
             $partial = array_shift($args);
         } elseif (is_array($arg)) {
             if (array_key_exists(self::BLOCKS, $arg)) {
                 $blocks = $arg[self::BLOCKS];
             }
             unset($arg[self::BLOCKS]);
             $quickdefs = array_shift($args);
         } else {
             throw new \Mockery\Exception('Unable to parse arguments sent to ' . get_class($this) . '::mock()');
         }
     }
     if (!is_null($name) && !is_null($class)) {
         if (!$makeInstanceMock) {
             $mockName = \Mockery\Generator::createClassMock($class);
         } else {
             $mockName = \Mockery\Generator::createClassMock($class, null, null, array(), true);
         }
         $result = class_alias($mockName, $name);
         $mock = $this->_getInstance($name);
         $mock->mockery_init($class, $this);
     } elseif (!is_null($name)) {
         $mock = new \Mockery\Mock();
         $mock->mockery_init($name, $this);
     } elseif (!is_null($class)) {
         $mockName = \Mockery\Generator::createClassMock($class, null, null, array(), false, $partialMethods);
         $mock = $this->_getInstance($mockName);
         $mock->mockery_init($class, $this);
     } elseif (!is_null($partial)) {
         $mockName = \Mockery\Generator::createClassMock(get_class($partial), null, true, $blocks);
         $mock = $this->_getInstance($mockName);
         $mock->mockery_init(get_class($partial), $this, $partial);
     } else {
         $mock = new \Mockery\Mock();
         $mock->mockery_init('unknown', $this);
     }
     if (!empty($quickdefs)) {
         $mock->shouldReceive($quickdefs);
     }
     if (!empty($expectationClosure)) {
         $expectationClosure($mock);
     }
     $this->rememberMock($mock);
     return $mock;
 }
コード例 #2
0
ファイル: Container.php プロジェクト: rdohms/mockery
 /**
  * Generates a new mock object for this container
  *
  * @return \Mockery\Mock
  */
 public function mock()
 {
     $class = null;
     $name = null;
     $partial = null;
     $expectationClosure = null;
     $quickdefs = array();
     $args = func_get_args();
     if (count($args) > 1) {
         $finalArg = end($args);
         reset($args);
         if (is_callable($finalArg)) {
             $expectationClosure = array_pop($args);
         }
     }
     while (count($args) > 0) {
         $arg = current($args);
         if (is_string($arg) && (class_exists($arg, true) || interface_exists($arg, true))) {
             $class = array_shift($args);
         } elseif (is_string($arg)) {
             $name = array_shift($args);
         } elseif (is_object($arg)) {
             $partial = array_shift($args);
         } elseif (is_array($arg)) {
             $quickdefs = array_shift($args);
         } else {
             throw new \Mockery\Exception('Unable to parse arguments sent to ' . get_class($this) . '::mock()');
         }
     }
     if (!is_null($name)) {
         $mock = new \Mockery\Mock();
         $mock->mockery_init($name, $this);
     } elseif (!is_null($class)) {
         $mockName = \Mockery\Generator::createClassMock($class);
         $mock = $this->_getInstance($mockName);
         $mock->mockery_init($class, $this);
     } elseif (!is_null($partial)) {
         $mockName = \Mockery\Generator::createClassMock(get_class($partial), null, true);
         $mock = $this->_getInstance($mockName);
         $mock->mockery_init(get_class($partial), $this, $partial);
     } else {
         $mock = new \Mockery\Mock();
         $mock->mockery_init('unknown', $this);
     }
     if (!empty($quickdefs)) {
         $mock->shouldReceive($quickdefs);
     }
     if (!empty($expectationClosure)) {
         $expectationClosure($mock);
     }
     $this->rememberMock($mock);
     return $mock;
 }