Exemplo n.º 1
0
 public function apply($code, MockConfiguration $config)
 {
     $target = $config->getTargetClass();
     if (!$target) {
         return $code;
     }
     if ($target->isFinal()) {
         return $code;
     }
     $className = ltrim($target->getName(), "\\");
     if (!class_exists($className)) {
         $targetCode = '<?php ';
         if ($target->inNamespace()) {
             $targetCode .= 'namespace ' . $target->getNamespaceName() . '; ';
         }
         $targetCode .= 'class ' . $target->getShortName() . ' {} ';
         /*
          * We could eval here, but it doesn't play well with the way
          * PHPUnit tries to backup global state and the require definition
          * loader
          */
         $tmpfname = tempnam(sys_get_temp_dir(), "Mockery");
         file_put_contents($tmpfname, $targetCode);
         require $tmpfname;
         \Mockery::registerFileForCleanUp($tmpfname);
     }
     $code = str_replace("implements MockInterface", "extends \\" . $className . " implements MockInterface", $code);
     return $code;
 }
Exemplo n.º 2
0
 public function apply($code, MockConfiguration $config)
 {
     if ($config->isInstanceMock()) {
         $code = $this->appendToClass($code, static::INSTANCE_MOCK_CODE);
     }
     return $code;
 }
 public function apply($code, MockConfiguration $config)
 {
     $interfaces = array_reduce((array) $config->getTargetInterfaces(), function ($code, $i) {
         return $code . ", \\" . $i->getName();
     }, "");
     $code = str_replace("implements MockInterface", "implements MockInterface" . $interfaces, $code);
     return $code;
 }
Exemplo n.º 4
0
 public function apply($code, MockConfiguration $config)
 {
     $namespace = $config->getNamespaceName();
     $className = $config->getShortName();
     $code = str_replace('namespace Mockery;', $namespace ? 'namespace ' . $namespace . ';' : '', $code);
     $code = str_replace('class Mock', 'class ' . $className, $code);
     return $code;
 }
Exemplo n.º 5
0
 /**
  * Apply implementation.
  *
  * @param $code
  * @param MockConfiguration $config
  * @return string
  */
 public function apply($code, MockConfiguration $config)
 {
     $magicMethods = $this->getMagicMethods($config->getTargetClass());
     foreach ($magicMethods as $method) {
         $code = $this->applyMagicTypeHints($code, $method);
     }
     return $code;
 }
 public function generate(MockConfiguration $config)
 {
     $code = file_get_contents(__DIR__ . '/../Mock.php');
     $className = $config->getName() ?: $config->generateName();
     $namedConfig = $config->rename($className);
     foreach ($this->passes as $pass) {
         $code = $pass->apply($code, $namedConfig);
     }
     return new MockDefinition($namedConfig, $code);
 }
 public function apply($code, MockConfiguration $config)
 {
     if ($config->requiresCallTypeHintRemoval()) {
         $code = str_replace('public function __call($method, array $args)', 'public function __call($method, $args)', $code);
     }
     if ($config->requiresCallStaticTypeHintRemoval()) {
         $code = str_replace('public static function __callStatic($method, array $args)', 'public static function __callStatic($method, $args)', $code);
     }
     return $code;
 }
Exemplo n.º 8
0
 public function apply($code, MockConfiguration $config)
 {
     $target = $config->getTargetClass();
     if (!$target) {
         return $code;
     }
     if (!$config->isMockOriginalDestructor()) {
         $code = preg_replace('/public function __destruct\\(\\)\\s+\\{.*?\\}/sm', '', $code);
     }
     return $code;
 }
 public function apply($code, MockConfiguration $config)
 {
     $target = $config->getTargetClass();
     if (!$target) {
         return $code;
     }
     if (!$target->hasInternalAncestor() || !$target->implementsInterface("Serializable")) {
         return $code;
     }
     $code = $this->appendToClass($code, self::DUMMY_METHOD_DEFINITION);
     return $code;
 }
 public function apply($code, MockConfiguration $config)
 {
     $target = $config->getTargetClass();
     if (!$target) {
         return $code;
     }
     foreach ($target->getMethods() as $method) {
         if ($method->isFinal() && isset($this->methods[$method->getName()])) {
             $code = preg_replace($this->methods[$method->getName()], '', $code);
         }
     }
     return $code;
 }
Exemplo n.º 11
0
 public function apply($code, MockConfiguration $config)
 {
     foreach ($config->getMethodsToMock() as $method) {
         if ($method->isPublic()) {
             $methodDef = 'public';
         } elseif ($method->isProtected()) {
             $methodDef = 'protected';
         } else {
             $methodDef = 'private';
         }
         if ($method->isStatic()) {
             $methodDef .= ' static';
         }
         $methodDef .= ' function ';
         $methodDef .= $method->returnsReference() ? ' & ' : '';
         $methodDef .= $method->getName();
         $methodDef .= $this->renderParams($method, $config);
         $methodDef .= $this->renderMethodBody($method, $config);
         $code = $this->appendToClass($code, $methodDef);
     }
     return $code;
 }