Skip to content

Commit 2667e69

Browse files
authored
[9.x] Fix Carbon::setTestNow usage. (#40790)
* Fix Carbon::setTestNow usage. * Fix CS.
1 parent 870bc7d commit 2667e69

29 files changed

+66
-106
lines changed

tests/Auth/AuthDatabaseTokenRepositoryTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,11 @@
1313

1414
class AuthDatabaseTokenRepositoryTest extends TestCase
1515
{
16-
protected function setUp(): void
17-
{
18-
parent::setUp();
19-
20-
Carbon::setTestNow(Carbon::now());
21-
}
22-
2316
protected function tearDown(): void
2417
{
2518
parent::tearDown();
2619

2720
m::close();
28-
Carbon::setTestNow(null);
2921
}
3022

3123
public function testCreateInsertsNewRecordIntoTable()

tests/Cache/CacheArrayStoreTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99

1010
class CacheArrayStoreTest extends TestCase
1111
{
12+
protected function tearDown(): void
13+
{
14+
parent::tearDown();
15+
16+
Carbon::setTestNow(null);
17+
}
18+
1219
public function testItemsCanBeSetAndRetrieved()
1320
{
1421
$store = new ArrayStore;
@@ -37,15 +44,13 @@ public function testMultipleItemsCanBeSetAndRetrieved()
3744

3845
public function testItemsCanExpire()
3946
{
40-
Carbon::setTestNow(Carbon::now());
4147
$store = new ArrayStore;
4248

4349
$store->put('foo', 'bar', 10);
4450
Carbon::setTestNow(Carbon::now()->addSeconds(10)->addSecond());
4551
$result = $store->get('foo');
4652

4753
$this->assertNull($result);
48-
Carbon::setTestNow(null);
4954
}
5055

5156
public function testStoreItemForeverProperlyStoresInArray()
@@ -77,15 +82,13 @@ public function testNonExistingKeysCanBeIncremented()
7782

7883
public function testExpiredKeysAreIncrementedLikeNonExistingKeys()
7984
{
80-
Carbon::setTestNow(Carbon::now());
8185
$store = new ArrayStore;
8286

8387
$store->put('foo', 999, 10);
8488
Carbon::setTestNow(Carbon::now()->addSeconds(10)->addSecond());
8589
$result = $store->increment('foo');
8690

8791
$this->assertEquals(1, $result);
88-
Carbon::setTestNow(null);
8992
}
9093

9194
public function testValuesCanBeDecremented()
@@ -134,7 +137,6 @@ public function testCannotAcquireLockTwice()
134137

135138
public function testCanAcquireLockAgainAfterExpiry()
136139
{
137-
Carbon::setTestNow(Carbon::now());
138140
$store = new ArrayStore;
139141
$lock = $store->lock('foo', 10);
140142
$lock->acquire();
@@ -146,6 +148,7 @@ public function testCanAcquireLockAgainAfterExpiry()
146148
public function testLockExpirationLowerBoundary()
147149
{
148150
Carbon::setTestNow(Carbon::now());
151+
149152
$store = new ArrayStore;
150153
$lock = $store->lock('foo', 10);
151154
$lock->acquire();
@@ -156,7 +159,6 @@ public function testLockExpirationLowerBoundary()
156159

157160
public function testLockWithNoExpirationNeverExpires()
158161
{
159-
Carbon::setTestNow(Carbon::now());
160162
$store = new ArrayStore;
161163
$lock = $store->lock('foo');
162164
$lock->acquire();

tests/Cache/CacheFileStoreTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,6 @@
1111

1212
class CacheFileStoreTest extends TestCase
1313
{
14-
protected function setUp(): void
15-
{
16-
parent::setUp();
17-
18-
Carbon::setTestNow(Carbon::now());
19-
}
20-
21-
protected function tearDown(): void
22-
{
23-
parent::tearDown();
24-
25-
Carbon::setTestNow(null);
26-
}
27-
2814
public function testNullIsReturnedIfFileDoesntExist()
2915
{
3016
$files = $this->mockFilesystem();

tests/Cache/CacheMemcachedStoreTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function testSetMethodProperlyCallsMemcache()
6666
$store = new MemcachedStore($memcache);
6767
$result = $store->put('foo', 'bar', 60);
6868
$this->assertTrue($result);
69-
Carbon::setTestNow();
69+
Carbon::setTestNow(null);
7070
}
7171

7272
public function testIncrementMethodProperlyCallsMemcache()

tests/Cache/CacheRepositoryTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class CacheRepositoryTest extends TestCase
2323
protected function tearDown(): void
2424
{
2525
m::close();
26-
Carbon::setTestNow();
26+
27+
Carbon::setTestNow(null);
2728
}
2829

2930
public function testGetReturnsValueFromCache()
@@ -96,11 +97,6 @@ public function testRememberMethodCallsPutAndReturnsDefault()
9697
});
9798
$this->assertSame('bar', $result);
9899

99-
/*
100-
* Use Carbon object...
101-
*/
102-
Carbon::setTestNow(Carbon::now());
103-
104100
$repo = $this->getRepository();
105101
$repo->getStore()->shouldReceive('get')->times(2)->andReturn(null);
106102
$repo->getStore()->shouldReceive('put')->once()->with('foo', 'bar', 602);

tests/Console/Scheduling/FrequencyTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ class FrequencyTest extends TestCase
1717

1818
protected function setUp(): void
1919
{
20-
Carbon::setTestNow();
21-
2220
$this->event = new Event(
2321
m::mock(EventMutex::class),
2422
'php foo'
@@ -102,6 +100,8 @@ public function testLastDayOfMonth()
102100
Carbon::setTestNow('2020-10-10 10:10:10');
103101

104102
$this->assertSame('0 0 31 * *', $this->event->lastDayOfMonth()->getExpression());
103+
104+
Carbon::setTestNow(null);
105105
}
106106

107107
public function testTwiceMonthly()

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class DatabaseEloquentBuilderTest extends TestCase
2626
{
2727
protected function tearDown(): void
2828
{
29+
parent::tearDown();
30+
31+
Carbon::setTestNow(null);
32+
2933
m::close();
3034
}
3135

@@ -1665,8 +1669,6 @@ public function testUpdate()
16651669

16661670
$result = $builder->update(['foo' => 'bar']);
16671671
$this->assertEquals(1, $result);
1668-
1669-
Carbon::setTestNow(null);
16701672
}
16711673

16721674
public function testUpdateWithTimestampValue()
@@ -1711,8 +1713,6 @@ public function testUpdateWithAlias()
17111713

17121714
$result = $builder->from('table as alias')->update(['foo' => 'bar']);
17131715
$this->assertEquals(1, $result);
1714-
1715-
Carbon::setTestNow(null);
17161716
}
17171717

17181718
public function testUpsert()
@@ -1736,8 +1736,6 @@ public function testUpsert()
17361736
$result = $builder->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], ['email']);
17371737

17381738
$this->assertEquals(2, $result);
1739-
1740-
Carbon::setTestNow(null);
17411739
}
17421740

17431741
public function testWithCastsMethod()

tests/Database/DatabaseEloquentIntegrationTest.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ protected function createSchema()
155155
*/
156156
protected function tearDown(): void
157157
{
158+
parent::tearDown();
159+
158160
foreach (['default', 'second_connection'] as $connection) {
159161
$this->schema($connection)->drop('users');
160162
$this->schema($connection)->drop('friends');
@@ -165,6 +167,8 @@ protected function tearDown(): void
165167

166168
Relation::morphMap([], false);
167169
Eloquent::unsetConnectionResolver();
170+
171+
Carbon::setTestNow(null);
168172
}
169173

170174
/**
@@ -1627,8 +1631,6 @@ public function testUpdatingChildModelTouchesParent()
16271631

16281632
$this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is not touching model own timestamps.');
16291633
$this->assertTrue($future->isSameDay($user->fresh()->updated_at), 'It is not touching models related timestamps.');
1630-
1631-
Carbon::setTestNow($before);
16321634
}
16331635

16341636
public function testMultiLevelTouchingWorks()
@@ -1647,8 +1649,6 @@ public function testMultiLevelTouchingWorks()
16471649

16481650
$this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is not touching models related timestamps.');
16491651
$this->assertTrue($future->isSameDay($user->fresh()->updated_at), 'It is not touching models related timestamps.');
1650-
1651-
Carbon::setTestNow($before);
16521652
}
16531653

16541654
public function testDeletingChildModelTouchesParentTimestamps()
@@ -1666,8 +1666,6 @@ public function testDeletingChildModelTouchesParentTimestamps()
16661666
$post->delete();
16671667

16681668
$this->assertTrue($future->isSameDay($user->fresh()->updated_at), 'It is not touching models related timestamps.');
1669-
1670-
Carbon::setTestNow($before);
16711669
}
16721670

16731671
public function testTouchingChildModelUpdatesParentsTimestamps()
@@ -1686,8 +1684,6 @@ public function testTouchingChildModelUpdatesParentsTimestamps()
16861684

16871685
$this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is not touching model own timestamps.');
16881686
$this->assertTrue($future->isSameDay($user->fresh()->updated_at), 'It is not touching models related timestamps.');
1689-
1690-
Carbon::setTestNow($before);
16911687
}
16921688

16931689
public function testTouchingChildModelRespectsParentNoTouching()
@@ -1715,8 +1711,6 @@ public function testTouchingChildModelRespectsParentNoTouching()
17151711
$before->isSameDay($user->fresh()->updated_at),
17161712
'It is touching model own timestamps in withoutTouching scope, when it should not.'
17171713
);
1718-
1719-
Carbon::setTestNow($before);
17201714
}
17211715

17221716
public function testUpdatingChildPostRespectsNoTouchingDefinition()
@@ -1737,8 +1731,6 @@ public function testUpdatingChildPostRespectsNoTouchingDefinition()
17371731

17381732
$this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is not touching model own timestamps when it should.');
17391733
$this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models relationships when it should be disabled.');
1740-
1741-
Carbon::setTestNow($before);
17421734
}
17431735

17441736
public function testUpdatingModelInTheDisabledScopeTouchesItsOwnTimestamps()
@@ -1759,8 +1751,6 @@ public function testUpdatingModelInTheDisabledScopeTouchesItsOwnTimestamps()
17591751

17601752
$this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is touching models when it should be disabled.');
17611753
$this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.');
1762-
1763-
Carbon::setTestNow($before);
17641754
}
17651755

17661756
public function testDeletingChildModelRespectsTheNoTouchingRule()
@@ -1780,8 +1770,6 @@ public function testDeletingChildModelRespectsTheNoTouchingRule()
17801770
});
17811771

17821772
$this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.');
1783-
1784-
Carbon::setTestNow($before);
17851773
}
17861774

17871775
public function testRespectedMultiLevelTouchingChain()
@@ -1802,8 +1790,6 @@ public function testRespectedMultiLevelTouchingChain()
18021790

18031791
$this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is touching models when it should be disabled.');
18041792
$this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.');
1805-
1806-
Carbon::setTestNow($before);
18071793
}
18081794

18091795
public function testTouchesGreatParentEvenWhenParentIsInNoTouchScope()
@@ -1824,8 +1810,6 @@ public function testTouchesGreatParentEvenWhenParentIsInNoTouchScope()
18241810

18251811
$this->assertTrue($before->isSameDay($post->fresh()->updated_at), 'It is touching models when it should be disabled.');
18261812
$this->assertTrue($future->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.');
1827-
1828-
Carbon::setTestNow($before);
18291813
}
18301814

18311815
public function testCanNestCallsOfNoTouching()
@@ -1848,8 +1832,6 @@ public function testCanNestCallsOfNoTouching()
18481832

18491833
$this->assertTrue($before->isSameDay($post->fresh()->updated_at), 'It is touching models when it should be disabled.');
18501834
$this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.');
1851-
1852-
Carbon::setTestNow($before);
18531835
}
18541836

18551837
public function testCanPassArrayOfModelsToIgnore()
@@ -1870,8 +1852,6 @@ public function testCanPassArrayOfModelsToIgnore()
18701852

18711853
$this->assertTrue($before->isSameDay($post->fresh()->updated_at), 'It is touching models when it should be disabled.');
18721854
$this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.');
1873-
1874-
Carbon::setTestNow($before);
18751855
}
18761856

18771857
public function testWhenBaseModelIsIgnoredAllChildModelsAreIgnored()

tests/Database/DatabaseEloquentIrregularPluralTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ public function createSchema()
5555

5656
protected function tearDown(): void
5757
{
58+
parent::tearDown();
59+
5860
$this->schema()->drop('irregular_plural_tokens');
5961
$this->schema()->drop('irregular_plural_humans');
6062
$this->schema()->drop('irregular_plural_human_irregular_plural_token');
63+
64+
Carbon::setTestNow(null);
6165
}
6266

6367
protected function schema()

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@ class DatabaseEloquentModelTest extends TestCase
4242
{
4343
use InteractsWithTime;
4444

45-
protected function setUp(): void
46-
{
47-
parent::setUp();
48-
49-
Carbon::setTestNow(Carbon::now());
50-
}
51-
5245
protected function tearDown(): void
5346
{
5447
parent::tearDown();
@@ -2058,8 +2051,6 @@ public function testScopesMethod()
20582051
$model = new EloquentModelStub;
20592052
$this->addMockConnection($model);
20602053

2061-
Carbon::setTestNow();
2062-
20632054
$scopes = [
20642055
'published',
20652056
'category' => 'Laravel',

0 commit comments

Comments
 (0)