Php Laravel测试定制刀片(如适用)

Php Laravel测试定制刀片(如适用),php,laravel,laravel-5,Php,Laravel,Laravel 5,我正在尝试对自定义刀片进行单元测试,如果在laravel中 <?php namespace Tests\Unit; use Tests\TestCase; use Illuminate\Filesystem\Filesystem; use Illuminate\View\Compilers\BladeCompiler; class BladeIfAdminStatementTest extends TestCase { public function testIfAdminS

我正在尝试对自定义刀片进行单元测试,如果在laravel中

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;

class BladeIfAdminStatementTest extends TestCase
{
    public function testIfAdminStatementAreCompiled()
    {
        $compiler = new BladeCompiler(\Mockery::mock(Filesystem::class), __DIR__);

        $string = '@admin test @endadmin';
        $expected = '<?php if(auth->check() && auth()->user()->isAdmin()): ?> test <?php endif; ?>';

        $this->assertEquals($expected, $compiler->compileString($string));
    }
}

请注意,它失败了,但
@if
语句编译正确,而我的自定义
@admin
语句编译不正确。

编译器字符串将编译字符串并为您返回代码结果

每个blade语句都将被赋值,然后返回


您是否尝试过断言结果而不是php代码

只需访问compileString方法,如下所示:

名称空间测试\单元;
使用Tests\TestCase;
使用照明\支持\立面\刀片;
类BladeIfAdminStatementTest扩展了TestCase
{
公共功能测试FADMINStatementAreCompiled()
{
$bladeSnippet='@admin test@endadmin';
$expectedCode='test';
$this->assertEquals($expectedCode,Blade::compileString($bladeSnippet));
}
}

您能给我一个示例断言吗?
<?php


...

public function boot()
{
    \Blade::if('admin', function () {
        return auth()->check() && auth()->user()->isAdmin();
    });
}
Failed asserting that two strings are equal.
Expected :'<?php if(auth->check() && auth()->user()->isAdmin()): ?> test <?php endif; ?>'
Actual   :'@admin  test @endadmin'
Failed asserting that two strings are equal.
Expected :'<?php if(auth->check() && auth()->user()->isAdmin()): ?> test <?php endif; ?>'
Actual   :'<?php if(true): ?> test <?php endif; ?>'
namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Support\Facades\Blade;


class BladeIfAdminStatementTest extends TestCase
{
    public function testIfAdminStatementAreCompiled()
    {

        $bladeSnippet = '@admin test @endadmin';
        $expectedCode = '<?php if(auth->check() && auth()->user()->isAdmin()): ?> test <?php endif; ?>';

        $this->assertEquals($expectedCode, Blade::compileString($bladeSnippet));
    }
}