I wanna test my livewire store functionality.
public function store()
{
$validated = $this->validate(
[
'service_id' => 'required',
'price' => 'required|regex:/^[0-9]{1,5}([,.][0-9]{1,2})?$/',
'option_available.*' => 'boolean',
'option_price.*' => 'regex:/^[0-9]{1,5}([,.][0-9]{1,2})?$/'
]
);
// formatting price
$validated['price'] = (float) str_replace(',' , '.', $validated['price']);
$service = $this->services->find($validated['service_id']);
$options = collect([]);
if ( $this->serviceOptions ) {
foreach ($this->serviceOptions as $key => $option) {
$options->push(
[
'name' => $option['name'],
'price' => ((float) str_replace(',', '.', $this->option_price[$key])) * 100 ?? '',
'available' => $this->option_available[$key] ?? false
]
);
}
}
$this->garage->addService($service, $options, $validated['price']);
}
This is my test function:
/** @test */
function user_can_edit_garage_services_information() {
Role::create(['name' => 'Garage Owner']);
$garage = factory(Garage::class)->create();
// create and attach services
$service = Service::create(
[
'name' => 'Reifenwechsel',
'slug' => 'reifenwechsel'
]
);
// create and attach service options
$serviceOptions = collect(
[
'name' => 'Wuchten',
'available' => false,
'price' => 3
]
);
$garage->addService($service, $serviceOptions, 20);
$this->assertEquals(20, $garage->services->first()->pivot->price);
$this->assertEquals(false, $garage->services->first()->pivot->options['available']);
$this->assertEquals(3, $garage->services->first()->pivot->options['price']);
Livewire::test(
GarageServiceListItem::class,
[
'garage' => $garage
]
)
->set('service_id', $service->id )
->set('price', 25 )
->set('option_available.0', true)
->set('option_price.0', 5)
->call('store');
$garage->refresh();
$this->assertEquals(25, $garage->services->first()->pivot->price);
$this->assertEquals(true, $garage->services->first()->pivot->options['available']);
$this->assertEquals(5, $garage->services->first()->pivot->options['price']);
}
I get this error exception when trying to test this function:
ErrorException : Undefined index: available
I need to pass the ‘option_available’ and ‘option_price’ values but don’t have the clue how I can do this.
Does anyone know the reason?
Thanks in advance.
Source: Laravel