Livewire class does not exists - package development

What seems to be the problem:

    Illuminate\Contracts\Container\BindingResolutionException : Target class [livewire] does not exist.
     E:\Programs\xampp\htdocs\lingua\vendor\laravel\framework\src\Illuminate\Container\Container.php:811
     E:\Programs\xampp\htdocs\lingua\vendor\laravel\framework\src\Illuminate\Container\Container.php:691
     E:\Programs\xampp\htdocs\lingua\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:796
     E:\Programs\xampp\htdocs\lingua\vendor\laravel\framework\src\Illuminate\Container\Container.php:637
     E:\Programs\xampp\htdocs\lingua\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:781
     E:\Programs\xampp\htdocs\lingua\vendor\laravel\framework\src\Illuminate\Container\Container.php:1284
     E:\Programs\xampp\htdocs\lingua\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:198
     E:\Programs\xampp\htdocs\lingua\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:166
     E:\Programs\xampp\htdocs\lingua\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:255
     E:\Programs\xampp\htdocs\lingua\tests\Feature\Http\Livewire\LinguaLivewireTest.php:21

Steps to Reproduce:
From a new scratch package development use the Livewire::test() facade and execute the test.

Are you using the latest version of Livewire: yes.

Do you have any screenshots or code examples:

    <?php

    namespace alessandrobelli\Lingua\Tests\Feature\Http\Livewire;

    use alessandrobelli\Lingua\Http\Livewire\TranslationInput;
    use alessandrobelli\Lingua\Tests\TestCase;
    use alessandrobelli\Lingua\Tests\User;
    use alessandrobelli\Lingua\Translation;
    use Livewire\Livewire;

    class LinguaLivewireTest extends TestCase
    {
        /** @test */
        public function can_update_locales()
        {
            $this->withoutExceptionHandling();
            $newTranslation = "a";
            $translation = Translation::create(['string' => 'test', 'locales' => '{}']);
            $this->actingAs(factory(User::class)->create());
            Livewire::test(TranslationInput::class,
                ['translation' => $translation->id, 'translationModel' => 'test', 'locale' => "de"])
                ->set('locales', "test")
                ->call('saveLocale');
            $this->assertTrue(Translation::whereLocale($newTranslation)->exists());
        }
    }

Have you included Livewire in the composer requirements? Another thing that in a “pure” package there is no app() kernel and Laravel facades will not work. So you have to also add “orchestra/testbench” otherwise you migh have troubles in a really simple test tasks.

Yes

    "require": {
        "php": "^7.4"
    },
    "require-dev": {
        "friendsofphp/php-cs-fixer": "^2.16",
        "orchestra/testbench": "^5.0",
        "phpunit/phpunit": "^9.0",
        "psalm/plugin-laravel": "^1.2",
        "vimeo/psalm": "^3.11",
        "ext-zip": "*",
        "livewire/livewire": "^1.3"
   }

I also added orchestra/testbench, I am following the Spatie course on package development (but I went further, Freek doesn’t use livewire there).
I think I’m doing something wrong, can the Livewire::test() be used in a package development? :thinking:

I haven’t tried yet… :roll_eyes: But make sure that your test extends Orchestra\Testbench\TestCase. App is initialized there.

My base test case:

    <?php

    namespace Tests;
    use Illuminate\Support\Facades\Artisan;
    use dimonka2\flatform\FlatformServiceProvider;
    use Orchestra\Testbench\TestCase as BaseTestCase;
    use Illuminate\Support\Facades\Facade as Facade;

    class TestCase extends BaseTestCase
    {

        public function setUp(): void
        {

            $this->afterApplicationCreated(function () {
                $this->makeACleanSlate();
            });

            $this->beforeApplicationDestroyed(function () {
                $this->makeACleanSlate();
            });

            parent::setUp();
            Facade::setFacadeApplication(app());
        }

        public function makeACleanSlate()
        {
            Artisan::call('view:clear');
        }

        protected function getPackageProviders($app)
        {
            return [
                FlatformServiceProvider::class,
            ];
        }

        protected function getEnvironmentSetUp($app)
        {
            $app['config']->set('view.paths', [
                __DIR__.'/../views',
                resource_path('views'),
            ]);

            $app['config']->set('flatform', require  __DIR__ . '\..\config\flatform.php');
            $app['config']->set('session.driver', 'file');
            $app['config']->set('app.key', 'base64:Hupx3yAySikrM2/edkZQNQHslgDWYfiBfCuSThJ5SK8=');
            $app['config']->set('flatform.test', 1);
        }

        protected function resolveApplicationHttpKernel($app)
        {
            $app->singleton('Illuminate\Contracts\Http\Kernel', 'Tests\HttpKernel');
        }

        public function test_test_envirement()
        {
            $this->assertNotNull(app('flatform'));
        }
    }
1 Like

Thanks a lot!.
This solved it:

$app['config']->set('view.paths', [
                __DIR__.'/../views',
                resource_path('views'),
            ]);
 $app['config']->set('app.key', 'base64:Hupx3yAySikrM2/edkZQNQHslgDWYfiBfCuSThJ5SK8=');

since I just successfully tested 1 component so far, I also added the other things:

        $this->afterApplicationCreated(function ()
        {
            $this->makeACleanSlate();
        });
        $this->beforeApplicationDestroyed(function ()
        {
            $this->makeACleanSlate();
        });
        Facade::setFacadeApplication(app());

 $app['config']->set('session.driver', 'file');

Is it me or something is missing from the docs, either from livewire or laravel documentation? What I want to say is, how I was supposed to know this? :thinking:

It was a real challenge for me to find out why this and that is not working. There is really too little information and I was just googling Laravel packages trying to understand what I am doing wrong…

this TestCase.php is of Project Root aur Package Root ?

please let me know i am suffering from same issue

TestCase.php is in the tests directory

Thanks for Your Reply

in this where i should place

and if possible please share some resources for livewire package testing

without livewire, tests are working properly

thanks in advance

i have Autoloaded package test cases in project root composer for running project and package test cases in single command with php artisan test