isimmons's avatar

Strange error in phpunit when I add RefreshDatabase to any test

Hi all,

I was running tests with artisan test and they were working fine. Then I tried phpunit and got the following error

1) Tests\Feature\ExampleTest::test_the_application_returns_a_successful_response
Error: Call to undefined function Illuminate\Console\View\Components\Mutators\str()

I actually got that same error on all tests so then trying to narrow down the issue I decided to run only the example feature test that comes with laravel. If I add use RefreshDatabase to the test class and run phpunit I get the above error. But it works and passes if I run php artisan test.

Anyone else run into this? This is on Laravel 9.28

Thanks

0 likes
6 replies
isimmons's avatar

@sinnbeck Yes I just created a new laravel app with "laravel new footest", deleted the example unit test and left only the example feature test. artisan test works and phpunit works until I add "use RefreshDatabase"

Failing test

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    use RefreshDatabase;

    /**
     * A basic test example.
     *
     * @return void
     */
    public function test_the_application_returns_a_successful_response()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

Tests/TestCase;

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
}

The Error with stacktrace

There was 1 error:

1) Tests\Feature\ExampleTest::test_the_application_returns_a_successful_response
Error: Call to undefined function Illuminate\Console\View\Components\Mutators\str()

C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Console\View\Components\Mutators\EnsurePunctuation.php:15
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Console\View\Components\Component.php:89
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Console\View\Components\Line.php:44
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Console\View\Components\Info.php:18
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Console\View\Components\Factory.php:56
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Database\Console\Migrations\MigrateCommand.php:113
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Database\Console\Migrations\MigrateCommand.php:80
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:616
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Database\Console\Migrations\MigrateCommand.php:100
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:36
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Container\Util.php:41
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:93
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:37
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Container\Container.php:651
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Console\Command.php:144
C:\Users\isimm\code\php\footest\vendor\symfony\console\Command\Command.php:308
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Console\Command.php:126
C:\Users\isimm\code\php\footest\vendor\symfony\console\Application.php:1002
C:\Users\isimm\code\php\footest\vendor\symfony\console\Application.php:299
C:\Users\isimm\code\php\footest\vendor\symfony\console\Application.php:171
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Console\Application.php:102
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Console\Application.php:194
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php:263
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Testing\PendingCommand.php:296
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Testing\PendingCommand.php:475
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithConsole.php:80
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Foundation\Testing\RefreshDatabase.php:47
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Foundation\Testing\RefreshDatabase.php:22
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestCase.php:123
C:\Users\isimm\code\php\footest\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestCase.php:92
C:\Users\isimm\code\php\laracasts\blog\vendor\phpunit\phpunit\src\TextUI\Command.php:143
C:\Users\isimm\code\php\laracasts\blog\vendor\phpunit\phpunit\src\TextUI\Command.php:96

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

Thanks

isimmons's avatar
isimmons
OP
Best Answer
Level 17

I just noticed those last 2 lines in the stacktrace are paths to a completely different laravel project.

I could not find reference to that path anywhere in phpstorm settings, system env's , or composer files.

When I specifically ran a projects local vendor/bin/phpunit the problem disappeared.

Somehow some reference to that project got stored somewhere in my global install of phpunit so even though the "which" and "where" commands were showing phpunit as coming from the global composer directory, composer was then running phpunit out of that other project.

I ended up having to remove global phpunit and reinstall it to get the problem fixed.

isimmons's avatar

@Sinnbeck Yeah it's weird. Thanks for your help though. Marked answered but if I ever figure out what caused it I'll try to remember to post it here.

Thanks again

Sinnbeck's avatar

@isimmons think the lesson is "never use a global phpunit install". Always install locally and use php artisan test

Please or to participate in this conversation.