2016-01-22 27 views
0

如果你在谷歌寻找phalcon phpunits,你会发现phalcon的官方文章如何让它起作用。在这篇文章中是一个TestHelper的例子。例子很好 - 但他们需要详细描述。phalcon中没有作曲家的单元测试

首先使用FactoryDe​​faults创建一个DI。我不使用FactoryDe​​faults。其次是创建路径常量 - 为什么和我需要什么(我甚至没有找到项目树)。

我thinl接下来会引发错误:

包括DIR。 “/../vendor/autoload.php”;

我不想使用composer,npm,grunt,gem或其他包装助手,因为我不喜欢外部应用程序在我的项目中做一些“魔术”的想法。下载一个文件并加载它我可以自己做,并安装一个应用程序(如phpunits)我宁愿使用我的操作系统(pacman)中的包装管理器。

但是,当我看着phalcon /孵化器没有这样的目录供应商和没有这样的文件autoload ..怎么办?这是什么文件?从哪里得到它?

具体问题:如何让单元测试在没有安装composer的情况下运行?

+0

作曲呢没有魔法。你在JSON中配置你想要的包。然后下载它们并生成自动加载器。包含自动加载器,以便在实例化时自动加载类。你有没有在一个真实的项目中使用它?如果没有,我会建议在放弃之前尝试。 – gontrollez

+0

感谢您的帮助。创建自动加载器,将文件存储在预定义的结构中 - 这很神奇,与我的项目无关。因此,我可能必须在测试中创建一个自动加载器,并在那里添加孵化器 - 以了解我必须安装composer,创建新项目,创建composer.json并在其中安装孵化器 – iRaS

回答

0

要使用它,而作曲家,你可以下载或克隆所需的项目:

Phalcon Incubator - https://github.com/phalcon/incubator
Swiftmailer - https://github.com/swiftmailer/swiftmailer
Fork-Helper - https://github.com/duncan3dc/fork-helper

商店他们到一个单独的目录,byself这样一个创建autoload.php:

class UnitTestAutoload 
{ 
    protected $prefixLength = []; 
    protected $prefixDirs = []; 
    protected $fallbackDirs = []; 
    protected $classMap = []; 

    public function loadClass($class) 
    { 
     if ($file = $this->findFile($class)) { 
      includeFile($file); 
      return true; 
     } 
    } 

    public function setNamespaceMapping($prefix, $paths) 
    { 
     if (!$prefix) { 
      $this->fallbackDirs = (array)$paths; 
     } else { 
      $length = strlen($prefix); 
      if ('\\' !== $prefix[$length - 1]) { 
       throw new InvalidArgumentException("A non-empty prefix must end with a namespace separator."); 
      } 
      $this->prefixLength[$prefix[0]][$prefix] = $length; 
      $this->prefixDirs[$prefix] = (array)$paths; 
     } 
    } 

    protected function findFile($class) 
    { 
     if ('\\' == $class[0]) { 
      $class = substr($class, 1); 
     } 

     if (isset($this->classMap[$class])) { 
      return $this->classMap[$class]; 
     } 

     $file = $this->searchFile($class); 

     if ($file === null) { 
      return $this->classMap[$class] = false; 
     } 

     return $this->classMap[$class] = $file; 
    } 

    protected function searchFile($class) 
    { 
     $logicalPath = strtr($class, '\\', DIRECTORY_SEPARATOR) . '.php'; 

     $first = $class[0]; 
     if (isset($this->prefixLength[$first])) { 
      foreach ($this->prefixLength[$first] as $prefix => $length) { 
       if (0 === strpos($class, $prefix)) { 
        foreach ($this->prefixDirs[$prefix] as $dir) { 
         if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPath, $length))) { 
          return $file; 
         } 
        } 
       } 
      } 
     } 

     foreach ($this->fallbackDirs as $dir) { 
      if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPath)) { 
       return $file; 
      } 
     } 

     return null; 
    } 
} 

function includeFile($file) 
{ 
    include $file; 
} 

function requireFile($file) 
{ 
    require $file; 
} 

$_unitTestAutoload = new UnitTestAutoload(); 
$_unitTestAutoload->setNamespaceMapping('duncan3dc\\Helpers\\', array(__DIR__ . '/duncan3dc/fork-helper/src')); 
$_unitTestAutoload->setNamespaceMapping('Phalcon\\', array(__DIR__ . '/phalcon/incubator/Library/Phalcon')); 
spl_autoload_register([$_unitTestAutoload, 'loadClass'], true, true); 

requireFile(__DIR__ . '/swiftmailer/swiftmailer/lib/swift_required.php'); 
1

如果我的问题得到解决,您正在寻找一种方法来为您的Phalcon应用程序编写测试,而不依赖于只能与Composer一起安装的解决方案。

坏消息是,您肯定需要某种测试框架,您将不得不将其整合到您的项目中。 然而好消息是,有一个很好的解决方案: Codeception。它是一个测试框架,为几个PHP框架提供专用模块,包括Phalcon。

您可以通过Composer安装Codeception(您不需要这样,我明白了),或者您可以简单地抓住Phar文件。

基本上,你接下来要怎么做是这样的:从你的项目的根目录

  • 运行codecept bootstrap --empty
  • 创建一个测试套件
  • 有它使用Codeception的Phalcon2模块,并创建一个引导文件您的应用程序
  • 也许整合PhantomJS并让Codeception也测试您的GUI
  • 做很多其他很酷的东西

这只是一个粗略的指导,因为您的问题有点过于宽泛,无法深入细节。