Phpunit 无编写器的phalcon单元测试

Phpunit 无编写器的phalcon单元测试,phpunit,phalcon,Phpunit,Phalcon,如果你在谷歌上寻找phalcon phpunits,你会发现phalcon的官方文章《如何让它工作》。本文中是一个示例TestHelper。示例很好,但需要详细描述 首先,使用FactoryDefaults创建一个DI。我不使用FactoryDefault。第二个是创建的路径常量——为什么以及我需要什么(我甚至没有找到项目树) 我想下一个会抛出一个错误: 包括目录。“/../vendor/autoload.php” 我不想使用composer、npm、grunt、gem或任何其他打包助手,因为我

如果你在谷歌上寻找phalcon phpunits,你会发现phalcon的官方文章《如何让它工作》。本文中是一个示例TestHelper。示例很好,但需要详细描述

首先,使用FactoryDefaults创建一个DI。我不使用FactoryDefault。第二个是创建的路径常量——为什么以及我需要什么(我甚至没有找到项目树)

我想下一个会抛出一个错误:

包括目录。“/../vendor/autoload.php”

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

但当我查看phalcon/Cutter时,没有这样的目录供应商,也没有这样的文件自动加载。。怎么做?这是什么文件?在哪里可以买到


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

如果我没有弄错你的问题,那么你正在寻找一种方法来为你的Phalcon应用程序编写测试,而不依赖于一个只能安装composer的解决方案

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

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

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

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

这只是一个粗略的指导,因为你的问题太宽泛了,无法深入细节。

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

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

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

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

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

这只是一个粗略的指南,因为您的问题太宽泛了,无法深入细节。

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

Phalcon孵化器-
Swiftmailer-
叉子助手-

将它们存储到单独的目录中,并创建一个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');

要在不使用composer的情况下使用它,您可以下载或克隆所需的项目:

Phalcon孵化器-
Swiftmailer-
叉子助手-

将它们存储到单独的目录中,并创建一个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');

这位作曲家不会变魔术。您可以在JSON中配置所需的包。然后下载它们并生成自动加载器。您包括自动加载程序,以便在实例化时可以自动加载类。你有没有在实际项目中使用过它?如果不是的话,我建议在扔掉它之前先试试。谢谢你的帮助。创建一个自动加载器,将文件存储在预定义的结构中——这很神奇,与我的项目无关。因此,我可能必须在测试中创建一个自动加载程序,并在其中添加孵化器-以了解我必须安装什么以及如何安装composer,创建一个新项目,创建一个composer.json并安装孵化器,而composer并没有什么魔力。您可以在JSON中配置所需的包。然后下载它们并生成自动加载器。您包括自动加载程序,以便在实例化时可以自动加载类。你有没有在实际项目中使用过它?如果不是的话,我建议在扔掉它之前先试试。谢谢你的帮助。创建一个自动加载器,将文件存储在预定义的结构中——这很神奇,与我的项目无关。因此,我可能必须在测试中创建一个自动加载程序,并在其中添加孵化器-以了解我必须安装什么以及如何安装composer,创建一个新项目,创建composer.json并在那里安装孵化器