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!
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 theresources/components
folder, so I had to insert next code on theboot()
method of my package's service provider: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: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
:As an additional information, I must say that my
phpunit
test classes inherits from Orchestral/testbenchTestCase
class, and you may need to include theView
andBlade
facades on your test file. I'm also using the next method to ensure theboot()
method of my package's service provider executes on my test environment before running the tests: