douxun3496 2017-01-17 20:13
浏览 88
已采纳

Laravel包单元测试配置文件中的辅助函数失败(调用未定义的方法base_path())

Okay, So I'm trying to update a package that I used a good deal with Laravel 4 to be compatible with Laravel 5, since the original repository hasn't had any updates for a while and it needed a few tweaks to work with Laravel 5.

I have made the necessary changes, and created a pull request to the original repository.

I noticed the unit tests had failed, though, so I tried digging into it. I haven't done anything with PHPUnit so far, so I figured this would be a good learning experience.

I'm not asking for help with the error displayed on github. I managed to get past that error, but I achieved this by removing some tests, which of course isn't an actual solution, thus these changes are not committed. I will fix that once I have a better understanding of how unit testing works.

The problem I seem to be having is that one of Laravel's helper functions is being used in the default config file of the package, it is saying the function base_path() is undefined.

As I understand, PHPUnit works with classes and I would need to mock the function in order for it to be used. Can't seem to find a straight answer HOW to do that.

Since it's pointing a config variable to a folder which doesn't exist in my repository hierarchy, I'm not even sure what it should return.

The file that's causing the issue is the default config located in polyglot/config/polyglot.php

return [

// Whether to swap out the facades (Router, Lang, etc) with
// Polyglot's, disable this if you need other packages to do the same
// Can specify an array of facades to swap, eg. ['Lang', 'URL']
'facades' => true,

// Locales
////////////////////////////////////////////////////////////////////

// The default locale if none is provided in the URL
// Leave empty to force the use of locales prefixes in URLs
'default' => 'en',

// The fallback locale for translations
// If null, the default locale is used
'fallback' => null,

// The available locales
'locales' => [],

// Gettext
////////////////////////////////////////////////////////////////////

// The domain of your translations, for gettext use
'domain' => 'messages',

// Where the PO/MO files reside

'folder' => base_path('resources/lang'),
// Format of the compiled files
'file' => '{domain}.po',

// Database
////////////////////////////////////////////////////////////////////

// The pattern Polyglot should follow to find the Lang classes
// Examples are "Lang\{model}", "{model}Lang", where {model}
// will be replaced by the model's name
'model_pattern' => '{model}Lang',

];

So, what is typical to do in this situation? Define the function manually? Include the helper file somehow? Or discard the use of the function and go with a solution using basic php directory references? Any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • drl37530 2017-01-17 21:37
    关注

    base_path is a standard Laravel helper. It's available every time Laravel boots. The proper approach is booting Laravel in your tests via the TestCase base class included in Laravel. Refer to the official testing guide and this laracast.

    You should not mock the method. You should not replace it with file references and string assembly. Doing either of these moves the code away from Laravel integration, not toward, as you wanted to do with the L5 upgrade.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?