【问题标题】:How to check if Laravel blade component is loaded?如何检查 Laravel 刀片组件是否已加载?
【发布时间】:2021-01-13 15:49:22
【问题描述】:

我正在为一个将提供一些 blade components 的软件包做出贡献。因此,此包的用户可以将刀片模板上的组件用作:

<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 框架)来检查刀片组件名称是否存在和/或已加载?

我已经设法为包提供的一组刀片视图和下一个代码做类似的事情:

// 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'));
    ...
}

提前致谢!

【问题讨论】:

    标签: phpunit laravel-blade laravel-7


    【解决方案1】:

    终于设法找到解决这个问题的方法,我将对此进行解释,因为它可能对其他读者有用。首先,您需要加载component classes 使用的视图集(您通常在render() 方法中使用的视图)。在我的特定情况下,组件视图位于 resources/components 文件夹中,因此我必须在我的包服务提供者的 boot() 方法中插入下一个代码:

    // Load the blade views used by the components.
    
    $viewsPath = $this->packagePath('resources/components');
    $this->loadViewsFrom($viewsPath, 'mypackage');
    

    其中packagePath() 是一种将完全限定路径(从包根文件夹)返回到接收到的参数的方法。

    接下来,再次在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 测试类继承自Orchestral/testbench TestCase 类,您可能需要在测试文件中包含ViewBlade 外观。我还使用下一个方法来确保我的包服务提供者的boot() 方法在运行测试之前在我的测试环境中执行:

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

    【讨论】:

      【解决方案2】:

      没有用于检查组件是否存在的方法或助手,但从那时起刀片组件是 laravel 中的类,因此您可以检查您的特定组件类是否存在:

      // 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'));
          ...
      }
      

      【讨论】:

      • 您好,感谢您的回答,但我还没有想出如何解决它。我还找到了使用Blade::getClassComponentAliases() 方法的解决方案,并在解释中添加了答案。
      猜你喜欢
      • 2021-11-02
      • 2020-03-28
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 2019-01-20
      • 1970-01-01
      • 2019-01-10
      相关资源
      最近更新 更多