In the previous article "Test the Interim URL in Laravel Storage", we discussed the technique of two
methods to test Laravel. Although it does not support Storage::temporaryUrl()
, we demonstrate how to use simulation to effectively simulate its behavior. If you haven't read it yet, it is recommended that you read it first to understand the basic knowledge of testing temporary URL in Laravel. Storage::fake
temporaryUrl
This article will in -depth discussions on how to pass the freezing time to make the temporary URL test more reliable, especially in terms of time -sensitive functions. We will use Laravel's built -in test auxiliary functions and Carbon's time operation function to solve the potential inconsistency in the test.
Why frozen time is important
When the expiration time stamp generated during the test execution is slightly different due to the shift of time, this will happen. Frost time ensure that all operations related to time return to consistent values to eliminate this difference.
<code>断言两个字符串相等失败。 预期值:'http://localhost/test/image?expiration=1737729799' 实际值:'http://localhost/test/image?expiration=1737729800'</code>
Laravel's time frozen auxiliary function
$this->freezeTime()
$this->travelTo(Carbon::now())
: Set the current time for all Carbon operations. Carbon::setTestNow(Carbon::now())
For more details, please refer to the following resources: Frozen time in the Laravel test
Frozen time in the test
The following is how to use time frozen to test the above functions:
<code>断言两个字符串相等失败。 预期值:'http://localhost/test/image?expiration=1737729799' 实际值:'http://localhost/test/image?expiration=1737729800'</code>
$this->freezeTime()
Storage assertions assertMissing
assertExists
: Simulation external API calls to obtain images. By adopting these practices, you will have tests that can predict and have more powerful temporary URLs and other time sensitive functions.
The above is the detailed content of Freezing Time: Testing Laravel Temporary Storage URLs. For more information, please follow other related articles on the PHP Chinese website!