Summer Sale! All accounts are 50% off this week.

bhosted's avatar

Target class [translator] does not exist.

Hi,

I'm trying to setup my first phpunit test in Laravel 8 and phpStorm. The first test already results in an error I can't fix:

Illuminate\Contracts\Container\BindingResolutionException : Target class [translator] does not exist.

The class being tested is a small simple "enum" class, that's calling __( ... ) for translation. When using the class in the normal situation, it's running just fine. But the phpunit test is giving problems.

Any tips to solve this? Or do you need more information to help me solve this issue?

Kind regards, Marc

0 likes
10 replies
bhosted's avatar

Thansk, but I already found that thread and it did not fix the problem.

AlexElementarteilchen's avatar

Hi Marc,

sometimes it helps to do composer dump-autoload.

(also I routinely forget to import stuff and then end up staring at things for 20 minutes before I finally realize that a simple use ...is missing - so maybe it's just that :-))

bhosted's avatar

Do you know which class I shoud add as a use for fixing the translator missing error? I've tried sereveral, but none of them fixed the error.

bhosted's avatar

As already mentioned: it's a first for me to create PHPUnit test classes, so it's a very simple class I'm testing.

The class I want to test:

<?php

namespace App\Tools;

/**
 * Class Gender
 * @package App\Tools
 */
class Gender
{
    public const UNKNOWN = "U";
    public const MALE    = "M";
    public const FEMALE  = "F";

    /**
     * Get the long text for the gender
     *
     * @param $gender
     * @return mixed
     */
    public static function get( $gender )
    {
        if( self::is( $gender ) )
            return self::all()[$gender];
        else
            return false;
    }

    /**
     * Check if it's a valid gender
     *
     * @param $gender
     * @return bool
     */
    public static function is( $gender )
    {
        return( ! empty( self::all()[$gender] ) );
    }

    /**
     * Get all genders with text
     *
     * @return array
     */
    public static function all()
    {
        return [
            self::UNKNOWN => trans('Unknown'),
            self::MALE => trans('Male'),
            self::FEMALE => trans('Female')
        ];
    }

    /**
     * Get array of short gender codes
     *
     * @return string[]
     */
    public static function keys()
    {
        return [
            self::UNKNOWN,
            self::MALE,
            self::FEMALE
        ];
    }
}

and the PHPUnit test class:

<?php

namespace Tools;

use App\Tools\Gender;
use PHPUnit\Framework\TestCase;

/**
 * Class GenderTest
 * @package Tools
 */
class GenderTest extends TestCase
{
    /**
     * test the Gender class
     */
    public function testGender(): void
    {
        $gender = new Gender();

        $this->assertTrue( $gender->is( "U" ) );
        $this->assertTrue( $gender->is("M") );
        $this->assertTrue( $gender->is("F") );
        $this->assertFalse( $gender->is( "A") );
        $this->assertFalse( $gender->is( 1 ) );
    }
}
mabdullahsari's avatar
Level 16

You are extending the wrong TestCase which doesn't do any application bootstrapping. Extend the one in your root "tests" folder, not PHPUnit.

2 likes
bhosted's avatar

Thanks a lot! Fixed. The phpStorm / Laraval Idea plugin doesn't take that classs into account by default.

Please or to participate in this conversation.