How to check if Laravel blade component is loaded?

3.1k views Asked by At

I'm contributing to a package that will provide some blade components. So, the users of this package may use the components on a blade template as:

<x-mypackage-component-a/>

The components are located under the src/Components folder of my package. These components are loaded in the package service provider using the loadViewComponentsAs() method as explained here:

$this->loadViewComponentsAs('mypackage', [
    Components\ComponentA::class,
    ...
]);

Now, I need to make some tests for phpunit that should check that the components are loaded by the package service provider, something like next:

public function testComponentsAreLoaded()
{
    $this->assertTrue(/*code that check 'x-mypackage-component-a' exists*/);
}

Is there any way (using the Laravel framework) to check a blade component name exists and/or is loaded?

I have manage to do something similar for a set of blade views provided by the package with next code:

// Views are loaded on the package service provider as:

$this->loadViewsFrom($viewsPath, 'mypackage');

// The phpunit test method is:

public function testViewsAreLoaded()
{
    $this->assertTrue(View::exists('mypackage::view-a'));
    $this->assertTrue(View::exists('mypackage::view-b'));
    ...
}

Thanks in advance!

2

There are 2 answers

0
Shidersz On BEST ANSWER

Finally managed to find a way to solve this, I'm going to explain this because it may be useful for other readers. First, you need to load the set of views that are used by the component classes (the ones you usually use on the render() method). In my particular case the component views are located on the resources/components folder, so I had to insert next code on the boot() method of my package's service provider:

// Load the blade views used by the components.

$viewsPath = $this->packagePath('resources/components');
$this->loadViewsFrom($viewsPath, 'mypackage');

Where packagePath() is a method that returns the fully qualified path (from the package root folder) to the received argument.

Next, again in the boot() method, I had to load the components as explained on the question:

$this->loadViewComponentsAs('mypackage', [
    Components\ComponentA::class,
    Components\ComponentB::class,
    ...
]);

Finally, in order to make a test that asserts the views and the components are loaded correctly by the service provider, I have created the next method to be used with phpunit:

public function testComponentsAreLoaded()
{
    // Check that the blade component views are loaded.

    $this->assertTrue(View::exists('mypackage::component-a'));
    $this->assertTrue(View::exists('mypackage::component-b'));
    ...

    // Now, check that the class components aliases are registered.

    $aliases = Blade::getClassComponentAliases();

    $this->assertTrue(isset($aliases['mypackage-component-a']));
    $this->assertTrue(isset($aliases['mypackage-component-b']));
    ...
}

As an additional information, I must say that my phpunit test classes inherits from Orchestral/testbench TestCase class, and you may need to include the View and Blade facades on your test file. I'm also using the next method to ensure the boot() method of my package's service provider executes on my test environment before running the tests:

protected function getPackageProviders($app)
{
    return ['Namespace\To\MyPackageServiceProvider'];
}
1
AmirAli Esteki On

there is no method or helper for checking that a component exists or not but from then blade components are class in laravel, so you can check that your specific component class exists or not:

// application namespaces

namespace App\View\Components;

use Illuminate\View\Component;

// define component
class mypackage extends Component { ... }


// check component
public function testViewsAreLoaded(){
    $this->assertTrue(class_exists('\Illuminate\View\Component\mypackage'));
    ...
}