douxun3496
douxun3496
2017-01-17 20:13

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

  • php
  • unit-testing
已采纳

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 drl37530 4年前

    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.

    点赞 评论 复制链接分享