在 phpunit 中创建 phpunit.xml 文件 [英] Creating phpunit.xml file in phpunit

查看:92
本文介绍了在 phpunit 中创建 phpunit.xml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目有这样的结构:

I have such structure of my project:

  • 根--/src--/tests

在 src 中是我的文件,例如 Task1.php.在测试文件夹中是我的测试文件.示例:

In src are my file such a Task1.php. In tests folder are my tests files. Example:

<?php
require __DIR__ . '/../src/Task1.php';
class Task1Test extends PHPUnit_Framework_TestCase
 {
public function testTask1(){
    $this->assertEquals([1,1,1,3,5,9],fib([1,1,1],6));
}
}

我使用 require __DIR__ 包含我的 src 文件.'/../src/Task1.php';我想添加 bootstrap.php 文件并在那里包含 src 文件.然后在phpunit.xml中添加bootstrap.php.

I include my src files using require __DIR__ . '/../src/Task1.php'; I want to add bootstrap.php file and include src files there. And then add bootstrap.php in phpunit.xml.

我的 phpunit.xml:

My phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" strict="true" bootstrap="bootstrap.php"></phpunit>

我应该在 bootstrap.php 文件中写什么?

What should I write in bootstrap.php file?

我尝试在测试文件夹中添加 Autoloader.php 文件:

I try add Autoloader.php file in tests folder:

<?php
class AutoLoader {

static private $classNames = array();

/**
 * Store the filename (sans extension) & full path of all ".php" files found
 */
public static function registerDirectory($dirName) {

    $di = new DirectoryIterator($dirName);
    foreach ($di as $file) {

        if ($file->isDir() && !$file->isLink() && !$file->isDot()) {
            // recurse into directories other than a few special ones
            self::registerDirectory($file->getPathname());
        } elseif (substr($file->getFilename(), -4) === '.php') {
            // save the class name / path of a .php file found
            $className = substr($file->getFilename(), 0, -4);
            AutoLoader::registerClass($className, $file->getPathname());
        }
    }
}

public static function registerClass($className, $fileName) {
    AutoLoader::$classNames[$className] = $fileName;
}

public static function loadClass($className) {
    if (isset(AutoLoader::$classNames[$className])) {
        require_once(AutoLoader::$classNames[$className]);
    }
  }

 }

spl_autoload_register(array('AutoLoader', 'loadClass'));

以及测试文件夹中的 bootsstrap.php:

And bootsstrap.php in tests folder:

<?php
include_once('AutoLoader.php');
// Register the directory to your include files
AutoLoader::registerDirectory(__DIR__ . '/../src/');

但它不起作用

推荐答案

我为你刻了两个版本.

参见 https://github.com/Grawer/starckoverflow-41881997

First (branch global-functions) 是一个带有全局声明函数的版本,非常基础的函数.所做的基本上是将 require 从包含测试的文件移动到 bootstrap.php,仅此而已.

First (branch global-functions) is a version with globally declared functions, the very basic one. What was done is basically the require is moved from file containing test to bootstrap.php, and that's all.

第二个版本(分支主),带自动加载器更复杂,我意识到你可能会坚持这样做.尽管如此,我还是坚持所以就在这里.你快到了.在您编写 prs-4 时,您的 composer.json 中有一个拼写错误,它应该是 psr-4.剩下的就是将 autoloader require 添加到 bootstrap.php 中,并且您还需要将 fib 函数放在一个类中.然后您就可以访问它,而无需在测试类文件中要求该类.

Second version (branch master), with autoloader is more complicated, and I realized that you might hold on with doing it. Still, I was insisting so here it is. You was almost there. There was a typo in your composer.json as you wrote prs-4 and it is supposed to be psr-4. What was left was to add autoloader require to bootstrap.php, and you would also need to put fib function inside a class. Then you could access it, without needing to require that class in the test class file.

这篇关于在 phpunit 中创建 phpunit.xml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆