我得到了未定义的索引:livewire 测试中的指纹

I got Undefined index: fingerprint in livewire tests

我使用文件 tests/Feature/AdminFacilitiesCrud.php 对 Laravel 8 / livewire 2.7.2 应用程序进行测试:

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Livewire\Livewire;
use App\Models\User;
use App\Models\Facility;
use Livewire\Component;
use Illuminate\Database\QueryException;
//use App\Models\Settings;
//use App\Library\CheckValueType;
//use Livewire\WithPagination;
use App\Http\Livewire\Admin\CrudFacilities;
use DB;

class AdminFacilitiesCrud extends TestCase
{

    // Adding the property to the params array of the Livewire::test method solved it.
    // public $fingerprint= ''; // uncommenting this does not help

    /** @test  */
    // ./vendor/bin/phpunit   tests/Feature/AdminFacilitiesCrud.php
    public function can_create_facility_under_admin_crud()
    {

        $newUser= User::factory()->create();
        $this->actingAs($newUser);
        Livewire::test(CrudFacilities::class)
                ->set('name', 'New Facility name ')
                ->set('descr', 'New Facility description text...  ')
//                ->set('fingerprint', 'fingerprint value') // uncommenting this does not help
                ->call('store');
        $this->assertTrue(Facility::getByName('foo')->exists());
    }
}

但是我得到错误 运行 测试:

master@master-laptop:/mnt/_work_sdb8/wwwroot/lar/hostels4j$ ./vendor/bin/phpunit   tests/Feature/AdminFacilitiesCrud.php
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.4.25
Configuration: /mnt/_work_sdb8/wwwroot/lar/hostels4j/phpunit.xml

E                                                                   1 / 1 (100%)

Time: 00:00.269, Memory: 28.00 MB

There was 1 error:

1) Tests\Feature\AdminFacilitiesCrud::can_create_facility_under_admin_crud
ErrorException: Undefined index: fingerprint

/mnt/_work_sdb8/wwwroot/lar/hostels4j/vendor/livewire/livewire/src/Testing/TestableLivewire.php:179
/mnt/_work_sdb8/wwwroot/lar/hostels4j/vendor/livewire/livewire/src/Testing/Concerns/MakesCallsToComponent.php:142
/mnt/_work_sdb8/wwwroot/lar/hostels4j/vendor/livewire/livewire/src/Testing/Concerns/MakesCallsToComponent.php:87
/mnt/_work_sdb8/wwwroot/lar/hostels4j/vendor/livewire/livewire/src/Testing/Concerns/MakesCallsToComponent.php:74
/mnt/_work_sdb8/wwwroot/lar/hostels4j/vendor/livewire/livewire/src/Testing/Concerns/MakesCallsToComponent.php:56
/mnt/_work_sdb8/wwwroot/lar/hostels4j/tests/Feature/AdminFacilitiesCrud.php:33

在网上搜索我尝试了一些方法来修复它,比如在上面的测试文件中声明指纹 var

或清除缓存:

composer update livewire/livewire
php artisan view:clear
php artisan livewire:publish --assets

在组件 app/Http/Livewire/Admin/CrudFacilities 中。php我有:

<?php

namespace App\Http\Livewire\Admin;

use App\Models\Facility;
use Livewire\Component;
use Illuminate\Database\QueryException;
use App\Models\Settings;
use App\Library\CheckValueType;
use Livewire\WithPagination;
use DB;


class CrudFacilities extends Component
{
    use WithPagination;

    public $form= [
        'name'=>'',
        'descr'=> '',
        'created_at'=> '',
        'is_reopen'       => false,
    ];

    public $current_facility_id;
    public $filter_name= '';
    public $filter_extended_search= '';
    public $updateMode = 'browse';

    protected $listeners = ['openFacilityInEditor' => 'edit', 'addNewItem' => 'add'];

    public function render()
    {

       ...
       

    public function store()
    {
        // I do not see logging lines below - so it is not store method error
        \Log::info( '-1 store ::' . print_r(  -1, true  ) );
        $this->validate( Facility::getFacilityValidationRulesArray($this->current_facility_id), Facility::getValidationMessagesArray() );

        \Log::info( '-2 store $this->form ::' . print_r(  $this->form, true  ) );

修改块 1 : 我尝试在文件

中调试此错误
vendor/livewire/livewire/src/Testing/TestableLivewire.php:

    public function pretendWereSendingAComponentUpdateRequest($message, $payload)
    {
        $result = $this->callEndpoint('POST', '/livewire/message/'.$this->componentName, [
            'fingerprint' => $this->payload['fingerprint'], // THIS LINE RAISE ERROR !
            'serverMemo' => $this->payload['serverMemo'],
            'updates' => [['type' => $message, 'payload' => $payload]],
        ]);

        LivewireManager::$isLivewireRequestTestingOverride = true;

        return $result;
    }

从 vendor/livewire/livewire/src/Testing/Concerns/MakesCallsToComponent.php 加注:

public function set($name, $value = null)
{
    return $this->updateProperty($name, $value);
}

public function toggle($name)
{
    return $this->set($name, ! $this->get($name));
}

public function updateProperty($name, $value = null)
{
    if (is_array($name)) {
        foreach ($name as $key => $value) {  // I SUPPOSE I COULD SET 'fingerprint' value calling updateProperty method, but where?
            $this->syncInput($key, $value);
        }

        return $this;
    }

    return $this->syncInput($name, $value);
}

public function syncInput($name, $value)
{
    if ($value instanceof UploadedFile) {
        return $this->syncUploadedFiles($name, [$value]);
    } elseif (is_array($value) && isset($value[0]) && $value[0] instanceof UploadedFile) {
        return $this->syncUploadedFiles($name, $value, $isMultiple = true);
    }
    return $this->sendMessage('syncInput', [
        'name' => $name,
        'value' => $value,
    ]);
}

所以 payload 数组没有任何 'fingerprint' 值。我无法在 verdor 下修改文件,但可以复制此文件 在我的项目中?如果是,以何种方式?什么是 updateProperty 方法?我可以在我的测试脚本中应用它吗?

如何修复此错误?

谢谢!

我发现问题是我在 .env

的 APP_URL 值中遗漏了“http://”

修复

APP_URL=http://localhost-site

修正了这个错误!