Skip to content

Commit 4ee9b53

Browse files
authored
Fix flaky test (#51022)
1 parent 2c4746a commit 4ee9b53

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

tests/Database/QueryDurationThresholdTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66
use Illuminate\Database\Connection;
77
use Illuminate\Events\Dispatcher;
88
use Illuminate\Support\Arr;
9+
use Illuminate\Support\Carbon;
910
use PDO;
1011
use PHPUnit\Framework\TestCase;
1112

1213
class QueryDurationThresholdTest extends TestCase
1314
{
15+
/**
16+
* @var \Illuminate\Support\Carbon
17+
*/
18+
protected $now;
19+
1420
public function testItCanHandleReachingADurationThresholdInTheDb()
1521
{
1622
$connection = new Connection(new PDO('sqlite::memory:'));
@@ -46,10 +52,12 @@ public function testItIsOnlyCalledOnce()
4652

4753
public function testItIsOnlyCalledOnceWhenGivenDateTime()
4854
{
55+
Carbon::setTestNow($this->now = Carbon::create(2017, 6, 27, 13, 14, 15, 'UTC'));
56+
4957
$connection = new Connection(new PDO('sqlite::memory:'));
5058
$connection->setEventDispatcher(new Dispatcher());
5159
$called = 0;
52-
$connection->whenQueryingForLongerThan(now()->addMilliseconds(1), function () use (&$called) {
60+
$connection->whenQueryingForLongerThan($this->now->addMilliseconds(1), function () use (&$called) {
5361
$called++;
5462
});
5563

@@ -58,6 +66,8 @@ public function testItIsOnlyCalledOnceWhenGivenDateTime()
5866
$connection->logQuery('xxxx', [], 1);
5967

6068
$this->assertSame(1, $called);
69+
70+
Carbon::setTestNow(null);
6171
}
6272

6373
public function testItCanSpecifyMultipleHandlersWithTheSameIntervals()

0 commit comments

Comments
 (0)