没有作曲家的Phalcon中的单元测试

Unit tests in phalcon without composer

如果您在 Google 中查找 phalcon phpunits,您会找到来自 phalcon 的官方文章 how to get it to work。在本文中是一个示例 TestHelper。例子很好——但需要详细描述。

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

而且我认为下一个会抛出错误:

包括 DIR 。 "/../vendor/autoload.php";

我不想使用 composer、npm、g运行t、gem 或任何其他打包助手,因为我不喜欢这样的想法外部应用程序正在我的项目中做一些 "magic"。下载一个文件并加载它我可以自己完成并安装一个应用程序(比如 phpunits)我宁愿使用我的 os (pacman) 中的打包管理器。

但是当我查看 phalcon/incubator 时,没有这样的目录供应商,也没有这样的文件自动加载。怎么办?这个文件是干什么用的?从哪里获取?

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

如果我答对了你的问题,那么你正在寻找一种方法来为你的 Phalcon 应用程序编写测试,它不依赖于只能使用 Composer 安装的解决方案。

坏消息是,您肯定需要某种测试框架,并且必须将其集成到您的项目中。 然而,好消息是有一个好的解决方案: 代码欺骗。它是一个测试框架,具有多个 PHP 框架的专用模块,包括 Phalcon。

您可以通过 Composer 安装 Codeception(您不需要那个,我明白了),或者您可以简单地获取 Phar 文件。

基本上,您接下来要做的是:

  • 运行 codecept bootstrap --empty 来自项目的根目录
  • 创建测试套件
  • 让它使用 Codeception 的 Phalcon2 模块并为您的应用程序创建一个 bootstrap 文件
  • 也许集成 PhantomJS 并让 Codeception 也测试您的 GUI
  • 做很多其他很酷的事情

这只是一个粗略的指南,因为你的问题太宽泛了,无法详细讨论。

要在没有作曲家的情况下使用它,您可以下载或克隆所需的项目:

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

将它们存储到一个单独的目录并像这样自己创建一个 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');