Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kmnurunnabi's avatar

Expecting validation Error

I have a test:

test('quiz cannot be updated with invalid started_at', function () {
    $this->actingAs(User::factory()->create());

    $component = Livewire::test(Edit::class, ['quiz' => $this->quiz]);

	$title = 'Quiz title';
    $description = 'Quiz Description';
    $type = true;
	$started_at = 'random_string';
    $expired_at = 'random_string';

    // Set component data
    $component
        ->set('title', $title)
        ->set('description', $description)
        ->set('type', $type)
        ->set('started_at', $started_at)
		->set('expired_at', $expired_at);

    // Call save method
    $component->call('save');

    // Assert that validation error for started_at is present
    $component->assertHasErrors('started_at');
});

It should expect error but its throwing:

  FAILED  Tests\Feature\User\Quizzes\EditTest > quiz cannot be updated with invalid inputs with dataset "invalid started_date string"  
 Failed to parse time string (random_string) at position 0 (r): The timezone could not be found in the database                        
                                                                                                                                       
 at vendor\nesbot\carbon\src\Carbon\Traits\Creator.php:89                                                                              
    85▕             setlocale(LC_NUMERIC, 'C'); // @codeCoverageIgnore                                                                 
    86▕         }                                                                                                                      
    87▕                                                                                                                                
    88▕         try {                                                                                                                  
 ➜  89▕             parent::__construct($time ?: 'now', static::safeCreateDateTimeZone($tz) ?: null);                                  
    90▕         } catch (Exception $exception) {                                                                                       
    91▕             throw new InvalidFormatException($exception->getMessage(), 0, $exception);                                         
    92▕         }                                                                                                                      
    93▕                                                                                                                                
                                                                                                                                       
 1   vendor\nesbot\carbon\src\Carbon\Traits\Creator.php:89                                                                             
 2   vendor\livewire\livewire\src\Mechanisms\HandleComponents\Synthesizers\CarbonSynth.php:38                                          
                                                                                                                                       
 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  FAILED  Tests\Feature\User\Quizzes\EditTest > quiz cannot be updated with invalid inputs with dataset "invalid expired_date string"  
 Failed to parse time string (random_string) at position 0 (r): The timezone could not be found in the database                        
                                                                                                                                       
 at vendor\nesbot\carbon\src\Carbon\Traits\Creator.php:89                                                                              
    85▕             setlocale(LC_NUMERIC, 'C'); // @codeCoverageIgnore                                                                 
    86▕         }                                                                                                                      
    87▕                                                                                                                                
    88▕         try {                                                                                                                  
 ➜  89▕             parent::__construct($time ?: 'now', static::safeCreateDateTimeZone($tz) ?: null);                                  
    90▕         } catch (Exception $exception) {                                                                                       
    91▕             throw new InvalidFormatException($exception->getMessage(), 0, $exception);                                         
    92▕         }                                                                                                                      
    93▕                                                                                                                                
                                                                                                                                       
 1   vendor\nesbot\carbon\src\Carbon\Traits\Creator.php:89                                                                             
 2   vendor\livewire\livewire\src\Mechanisms\HandleComponents\Synthesizers\CarbonSynth.php:38                                          

How to avoid exception?

0 likes
5 replies
Tray2's avatar
Tray2
Best Answer
Level 73

That is because the now helper returns an object and not a string representation of a date. Try adding `->toString() on the

$started_at = now()->addDay()->toString();
$expired_at = now()->addDays(4)->toString();
kmnurunnabi's avatar

@Tray2 oops. Actually I did a mistake. Whenever I give a random string it should return validation error :

$started_at = 'random_string';
    $expired_at = 'random_string';
kmnurunnabi's avatar

@Tray2 Thanks . Your provided solution works. Actually I put the logic into livewire component inside mount(Quiz $quiz) lifecycle hook by simply adding $this->quiz = $quiz->started_at->toString().

Please or to participate in this conversation.