【问题标题】:phpunit error with app helper in laravel 4laravel 4中应用程序助手的phpunit错误
【发布时间】:2014-06-27 08:38:03
【问题描述】:

我尝试在我的 laravel 项目中开始使用 phpunit。

我调用“phpunit”并得到这个错误:

$ phpunit 
PHPUnit 3.6.10 by Sebastian Bergmann.

Configuration read from /home/bee/www/postaler/phpunit.xml

EPHP Fatal error:  Cannot redeclare Helper\isMenuActive() (previously declared in /home/bee/www/postaler/app/helpers.php:4) in /home/bee/www/postaler/app/helpers.php on line 6
PHP Stack trace:
PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:130
PHP   4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:192
PHP   5. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/TextUI/TestRunner.php:325
PHP   6. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:705
PHP   7. PHPUnit_Framework_TestSuite->runTest() /usr/share/php/PHPUnit/Framework/TestSuite.php:745
PHP   8. PHPUnit_Framework_TestCase->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:772
PHP   9. PHPUnit_Framework_TestResult->run() /usr/share/php/PHPUnit/Framework/TestCase.php:751
PHP  10. PHPUnit_Framework_TestCase->runBare() /usr/share/php/PHPUnit/Framework/TestResult.php:649
PHP  11. Illuminate\Foundation\Testing\TestCase->setUp() /usr/share/php/PHPUnit/Framework/TestCase.php:801
PHP  12. Illuminate\Foundation\Testing\TestCase->refreshApplication() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:31
PHP  13. Illuminate\Foundation\Application->boot() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:48
PHP  14. Illuminate\Foundation\Application->bootApplication() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:552
PHP  15. Illuminate\Foundation\Application->fireAppCallbacks() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:569
PHP  16. call_user_func:{/home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:792}() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:792
PHP  17. TestCase->{closure:/home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/start.php:223-271}() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:792
PHP  18. require() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/start.php:239

我的助手代码

<?php namespace Helper;
//app/helpers.php

function isMenuActive($url_segment) {
    if (\Request::segment(1) == $url_segment) { return " active"; }
}

我在 app/start/global.php 中包含了帮助文件

require app_path().'/helpers.php'

我的测试脚本

<?php
// app/tests/FromTest.php
class FromTest extends TestCase {

    public function testFrom()
    {
        $response = $this->call('GET', 'user/profile');

        $this->assertTrue(true);
    }

}

【问题讨论】:

    标签: unit-testing laravel laravel-4


    【解决方案1】:

    app/start/global.php 对每个请求执行一次,不应用于类/函数/常量定义。相反,将 files 自动加载规则添加到您的 composer.json。

    "files": ["src/functions.php"],
    

    【讨论】:

      猜你喜欢
      • 2018-05-20
      • 1970-01-01
      • 2014-06-15
      • 2015-10-14
      • 2013-06-18
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多