Will handle annotations on each method before running it Bespin:: up( $ this) If no attributes have been used, Bespin::up() will use: // Carbonite::freeze('now') // But you can still use to get a test with // real time self:: assertSame( 10.0, Carbonite:: speed())
Here we start from the real date-time, but during // the test, time elapse 10 times faster.
Self:: assertSame( 1.0, Carbonite:: speed()) Here we are the, but time is NOT frozen. Self:: assertSame( 0.0, Carbonite:: speed()) Release the time after each test Bespin:: down() Will handle attributes on each method before running it Bespin:: up( $ this) Use Carbon\ Carbonite\ Attribute\ JumpTo Use Carbon\ Carbonite\ Attribute\ Freeze Enable it using Bespin::up() on a given test suite: PHP 8 PHP 8 attributes (or PHPDoc annotations for PHP 7) can also be used forĬonvenience. $ this-> assertTrue( $holidays-> isEnded()) $ this-> assertFalse( $holidays-> isEnded()) $ this-> assertTrue( $holidays-> isInProgress()) $ this-> assertFalse( $holidays-> isStarted()) $holidays = CarbonPeriod:: create( '', '', CarbonPeriod:: EXCLUDE_END_DATE) Release after each test to isolate the timeline of each one. Working with frozen time in unit tests is highly recommended.
Returns the value returned by the given $action. Speed once it's done, rather it succeeded or threw an error or an exception. Trigger a given $action in the frozen current instant. If there is no previousĬarbonite state (if you didn't do any freeze, jump, speed, etc.) then Carbon::now() Now the speed is 1.5 on again echo Carbon:: now()-> format( 'Y-m-d') // output: echo Carbonite:: speed() // output: 1.5Ĭarbonite::do() is a good way to isolate a test and use a particular dateĪs "now" then be sure to restore the previous state. Still the same output as time is frozen inside the callback echo Carbon:: now()-> format( 'Y-m-d H:i:s.u') // output: 00:00:00.000000 echo Carbonite:: speed() // output: 0 You can also use CarbonImmutable, both will be synchronized.Īnd as Carbonite directly handle any date created with Carbon, it will work just fine for properties likeĬreated_at, updated_at or any custom date field in your Laravel models or any framework using Carbon.Įxample of raw PHPUnit test Available methods freezeĬarbonite::freeze($toMoment = 'now', float $speed = 0.0): voidįreeze the time to a given moment (now by default).Įcho Carbon:: now()-> format( 'Y-m-d H:i:s.u') // output: 00:00:00.000000 Sleep( 15) // If 15 seconds passes in the real time // Then 45 seconds passed in our fake timeline: echo Carbon:: now() // output: 00:00:45 Echo scanEpoch() // output: WW2 Carbonite:: elapse( '2 years') Įcho scanEpoch() // output: War is over Carbonite:: jumpTo( ' 00:00:00') Įcho scanEpoch() // output: The Discovery is flying! Carbonite:: speed( 3) // Times passes now thrice as fast