【问题标题】:Simpletest: Testing echo statements?最简单的:测试 echo 语句?
【发布时间】:2010-11-17 19:11:16
【问题描述】:

我刚刚用 simpletest 测试了一些 PHP 文件,发现它不适用于实际输出(回显)任何内容的函数。

那么我有什么办法可以在 PHP 中不使用 ob_buffer() 的情况下测试回显内容的函数吗?

谢谢

【问题讨论】:

  • :-) 为什么要使用函数来回显?
  • @zod 如果就这么简单,那为什么不直接使用?>Hello World<?php 而根本不使用echo。我假设 OP 需要从各种来源或有条件地组装字符串,并且您不想在模板中乱扔该代码。问题应该是,为什么 OP 没有在需要的地方返回字符串和 echo 它们。这将使测试功能更容易(并解决问题)。
  • 没有一个基于 JUnit API 的测试框架(PHPUnit、SimpleTest、SnapTest)可以做到这一点(没有变通方法)。对于原始 PHP 功能测试,请尝试 .phpt 脚本。

标签: php simpletest


【解决方案1】:

如果您正在测试输出本身的有效性,那么不会。不是没有输出缓冲区。但是,您可以使用 JavaScript 对其进行测试。您甚至可以通过 ajax 将输出传回另一个线程,使用 simpletest 对其进行测试。

绕行?哦,是的,宝贝。

【讨论】:

    【解决方案2】:

    使用以下完全愚蠢的方法应该可以满足您的需求。嘿...写起来很有趣:)

    <?php
    /**
     * The function which output you want to test.
     *
     * @param string $arg1
     * @param string $arg2
     */
    function function_with_echo($arg1, $arg2) {
        $c = array();
        echo "X";
        foreach (range(0,2) as $i) {
            print $i * 2 . $arg2;
        }
        echo "Yir $arg1";
    }
    
    /**
     * Stupid, too big, ugly function that takes a function and creates a new
     * function with two underscores prefixed (__) where all echo and print
     * statements instead are collected into a temporary variable and returned. 
     * Does not work for functions that already returns something, although 
     * that could be fixed too!
     *
     * @param string $function_name
     */
    function change_output_to_return($function_name) {
        $r = new ReflectionFunction($function_name);
        $lines = array_slice(
            file($r->getFileName()),
            $r->getStartLine() - 1,
            $r->getEndLine() - $r->getStartLine() + 1
        );
        $first = array_shift($lines);
        array_unshift($lines, $first, '$__temp = "";' . "\n");
        $last = array_pop($lines);
        array_push($lines, 'return $__temp;' . "\n", $last);
        $code = "<?php " . implode("", $lines);
        $echo_free_code = '';
        foreach(token_get_all($code) as $token) {
            if(is_array($token)) {
               if (in_array(token_name($token[0]), array('T_ECHO', 'T_PRINT'))) {
                   $echo_free_code .= '$__temp .= ';
               } else {
                   $echo_free_code .= $token[1];
               }
            } else {
                $echo_free_code .= $token;
            }
        }
        $echo_free_code = str_replace($function_name, "__$function_name", $echo_free_code);
        eval("?>$echo_free_code");
    }
    
    // Creates a function called "__function_with_echo" that returns a string
    // instead of outputting it using "print" and "echo".
    change_output_to_return('function_with_echo');
    
    // Stuff contains the outputted data from "function_with_echo".
    $stuff = __function_with_echo('fun', 'stuff');
    var_dump($stuff);
    

    【讨论】:

    • 虽然不处理所有 PHP 格式。可以通过使用更多令牌来修复。
    • 哇 - 代码完全疯了 - 我在中途完全迷路了 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    相关资源
    最近更新 更多