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.

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

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊