简体   繁体   中英

Laravel package unit testing failing for helper function in config file (call to undefined method 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.

For anyone else googling this, the solution I found was to use the CreatesApplication trait and call the createApplication() method in your test class.

class MyTest extends \PHPUnit\Framework\TestCase
{
   use Tests\CreatesApplication;

    protected function setUp(): void
    {
        $this->createApplication();
    }
}

After adding this to your class, you can then access helper classes like app() and base_path() , etc.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM