Skip to content

Commit 2246744

Browse files
add test for cache Repository class (#40817)
1 parent 536f5a8 commit 2246744

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

tests/Cache/CacheRepositoryTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,36 @@ public function testCacheAddCallsRedisStoreAdd()
205205
$this->assertTrue($repository->add('k', 'v', 60));
206206
}
207207

208+
public function testAddMethodCanAcceptDateIntervals()
209+
{
210+
$storeWithAdd = m::mock(RedisStore::class);
211+
$storeWithAdd->shouldReceive('add')->once()->with('k', 'v', 61)->andReturn(true);
212+
$repository = new Repository($storeWithAdd);
213+
$this->assertTrue($repository->add('k', 'v', DateInterval::createFromDateString('61 seconds')));
214+
215+
$storeWithoutAdd = m::mock(ArrayStore::class);
216+
$this->assertFalse(method_exists(ArrayStore::class, 'add'), 'This store should not have add method on it.');
217+
$storeWithoutAdd->shouldReceive('get')->once()->with('k')->andReturn(null);
218+
$storeWithoutAdd->shouldReceive('put')->once()->with('k', 'v', 60)->andReturn(true);
219+
$repository = new Repository($storeWithoutAdd);
220+
$this->assertTrue($repository->add('k', 'v', DateInterval::createFromDateString('60 seconds')));
221+
}
222+
223+
public function testAddMethodCanAcceptDateTimeInterface()
224+
{
225+
$withAddStore = m::mock(RedisStore::class);
226+
$withAddStore->shouldReceive('add')->once()->with('k', 'v', 61)->andReturn(true);
227+
$repository = new Repository($withAddStore);
228+
$this->assertTrue($repository->add('k', 'v', Carbon::now()->addSeconds(61)));
229+
230+
$noAddStore = m::mock(ArrayStore::class);
231+
$this->assertFalse(method_exists(ArrayStore::class, 'add'), 'This store should not have add method on it.');
232+
$noAddStore->shouldReceive('get')->once()->with('k')->andReturn(null);
233+
$noAddStore->shouldReceive('put')->once()->with('k', 'v', 62)->andReturn(true);
234+
$repository = new Repository($noAddStore);
235+
$this->assertTrue($repository->add('k', 'v', Carbon::now()->addSeconds(62)));
236+
}
237+
208238
public function testAddWithNullTTLRemembersItemForever()
209239
{
210240
$repo = $this->getRepository();
@@ -221,6 +251,8 @@ public function testAddWithDatetimeInPastOrZeroSecondsReturnsImmediately()
221251
$this->assertFalse($result);
222252
$result = $repo->add('foo', 'bar', Carbon::now());
223253
$this->assertFalse($result);
254+
$result = $repo->add('foo', 'bar', -1);
255+
$this->assertFalse($result);
224256
}
225257

226258
public function dataProviderTestGetSeconds()

0 commit comments

Comments
 (0)