如何检查 Laravel blade 组件是否加载?
How to check if Laravel blade component is loaded?
我正在为一个包提供一些 blade components。因此,此软件包的用户可以将 blade 模板上的组件用作:
<x-mypackage-component-a/>
组件位于我的包的 src/Components
文件夹下。这些组件使用 loadViewComponentsAs()
方法加载到包服务提供程序中,如 here:
所述
$this->loadViewComponentsAs('mypackage', [
Components\ComponentA::class,
...
]);
现在,我需要为 phpunit
做一些测试,检查组件是否由包服务提供商加载,如下所示:
public function testComponentsAreLoaded()
{
$this->assertTrue(/*code that check 'x-mypackage-component-a' exists*/);
}
是否有任何方法(使用 Laravel 框架)检查 blade 组件名称是否存在 and/or 已加载?
我已经设法用下一个代码对包提供的一组 blade 视图做了类似的事情:
// 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'));
...
}
提前致谢!
没有检查组件是否存在的方法或助手,但从那时起 blade 组件在 laravel 中 class,因此您可以检查您的特定组件 class存在与否:
// 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'));
...
}
终于找到了解决这个问题的方法,我将对此进行解释,因为它可能对其他读者有用。首先,您需要加载 component classes 使用的视图集(您通常在 render()
方法中使用的视图)。在我的特定情况下,组件视图位于 resources/components
文件夹中,因此我必须在我的包服务提供商的 boot()
方法中插入下一个代码:
// Load the blade views used by the components.
$viewsPath = $this->packagePath('resources/components');
$this->loadViewsFrom($viewsPath, 'mypackage');
其中 packagePath()
是 returns 到接收参数的完全限定路径(从包根文件夹)的方法。
接下来,再次在 boot()
方法中,我必须按照问题中的说明加载组件:
$this->loadViewComponentsAs('mypackage', [
Components\ComponentA::class,
Components\ComponentB::class,
...
]);
最后,为了进行断言服务提供商正确加载视图和组件的测试,我创建了下一个要与 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']));
...
}
作为附加信息,我必须说我的 phpunit
测试 classes 继承自 Orchestral/testbench TestCase
class,您可能需要在测试文件中包含 View
和 Blade
外观。我还使用下一个方法来确保我的包的服务提供商的 boot()
方法在 运行 测试之前在我的测试环境中执行:
protected function getPackageProviders($app)
{
return ['Namespace\To\MyPackageServiceProvider'];
}
我正在为一个包提供一些 blade components。因此,此软件包的用户可以将 blade 模板上的组件用作:
<x-mypackage-component-a/>
组件位于我的包的 src/Components
文件夹下。这些组件使用 loadViewComponentsAs()
方法加载到包服务提供程序中,如 here:
$this->loadViewComponentsAs('mypackage', [
Components\ComponentA::class,
...
]);
现在,我需要为 phpunit
做一些测试,检查组件是否由包服务提供商加载,如下所示:
public function testComponentsAreLoaded()
{
$this->assertTrue(/*code that check 'x-mypackage-component-a' exists*/);
}
是否有任何方法(使用 Laravel 框架)检查 blade 组件名称是否存在 and/or 已加载?
我已经设法用下一个代码对包提供的一组 blade 视图做了类似的事情:
// 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'));
...
}
提前致谢!
没有检查组件是否存在的方法或助手,但从那时起 blade 组件在 laravel 中 class,因此您可以检查您的特定组件 class存在与否:
// 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'));
...
}
终于找到了解决这个问题的方法,我将对此进行解释,因为它可能对其他读者有用。首先,您需要加载 component classes 使用的视图集(您通常在 render()
方法中使用的视图)。在我的特定情况下,组件视图位于 resources/components
文件夹中,因此我必须在我的包服务提供商的 boot()
方法中插入下一个代码:
// Load the blade views used by the components.
$viewsPath = $this->packagePath('resources/components');
$this->loadViewsFrom($viewsPath, 'mypackage');
其中 packagePath()
是 returns 到接收参数的完全限定路径(从包根文件夹)的方法。
接下来,再次在 boot()
方法中,我必须按照问题中的说明加载组件:
$this->loadViewComponentsAs('mypackage', [
Components\ComponentA::class,
Components\ComponentB::class,
...
]);
最后,为了进行断言服务提供商正确加载视图和组件的测试,我创建了下一个要与 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']));
...
}
作为附加信息,我必须说我的 phpunit
测试 classes 继承自 Orchestral/testbench TestCase
class,您可能需要在测试文件中包含 View
和 Blade
外观。我还使用下一个方法来确保我的包的服务提供商的 boot()
方法在 运行 测试之前在我的测试环境中执行:
protected function getPackageProviders($app)
{
return ['Namespace\To\MyPackageServiceProvider'];
}