Skip to content

Commit 21b0af9

Browse files
committed
Add tests
1 parent f6e323c commit 21b0af9

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

tests/Cache/CacheFileStoreTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Support\Str;
1111
use Mockery as m;
1212
use PHPUnit\Framework\TestCase;
13+
use RuntimeException;
1314

1415
class CacheFileStoreTest extends TestCase
1516
{
@@ -402,6 +403,40 @@ public function testFlushingLocksIgnoreNonExistingDirectory()
402403
$this->assertFalse($result, 'Flushing locks should not clean locks directory');
403404
}
404405

406+
public function testHasSeparateLockStoreReturnsTrueWhenLockDirectoryDiffers()
407+
{
408+
$store = new FileStore(new Filesystem, __DIR__);
409+
$store->setLockDirectory('/locks');
410+
411+
$this->assertTrue($store->hasSeparateLockStore());
412+
}
413+
414+
public function testHasSeparateLockStoreReturnsFalseWhenLockDirectoryIsSame()
415+
{
416+
$store = new FileStore(new Filesystem, __DIR__);
417+
$store->setLockDirectory(__DIR__);
418+
419+
$this->assertFalse($store->hasSeparateLockStore());
420+
}
421+
422+
public function testHasSeparateLockStoreReturnsFalseWhenLockDirectoryIsNull()
423+
{
424+
$store = new FileStore(new Filesystem, __DIR__);
425+
$store->setLockDirectory(null);
426+
427+
$this->assertFalse($store->hasSeparateLockStore());
428+
}
429+
430+
public function testFlushLocksThrowsExceptionWhenLockDirectoryIsSame()
431+
{
432+
$store = new FileStore(new Filesystem, __DIR__);
433+
$store->setLockDirectory(__DIR__);
434+
435+
$this->expectException(RuntimeException::class);
436+
437+
$store->flushLocks();
438+
}
439+
405440
public function testItHandlesForgettingNonFlexibleKeys()
406441
{
407442
$store = new FileStore(new Filesystem, __DIR__);

tests/Cache/CacheRepositoryTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,16 @@ public function testNonFlushableLockRepositoryDoesNotSupportFlushingLocks()
464464
$this->assertFalse($nonFlushableRepo->supportsFlushingLocks());
465465
}
466466

467+
public function testItThrowsExceptionWhenStoreDoesNotSupportFlushingLocks()
468+
{
469+
$this->expectException(BadMethodCallException::class);
470+
471+
$nonFlushable = m::mock(MemcachedStore::class);
472+
$nonFlushableRepo = new Repository($nonFlushable);
473+
474+
$nonFlushableRepo->flushLocks();
475+
}
476+
467477
public function testTouchWithNullTTLRemembersItemForever(): void
468478
{
469479
$key = 'key';

tests/Integration/Cache/RedisStoreTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Orchestra\Testbench\TestCase;
1313
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
1414
use PHPUnit\Framework\Attributes\TestWith;
15+
use RuntimeException;
1516

1617
#[RequiresPhpExtension('redis')]
1718
class RedisStoreTest extends TestCase
@@ -314,4 +315,36 @@ public function testLocksCanBeFlushed()
314315
$this->assertTrue($store->lock('lock-2', 60)->acquire());
315316
$this->assertTrue($store->lock('lock-3', 60)->acquire());
316317
}
318+
319+
public function testHasSeparateLockStoreReturnsTrueWhenLockConnectionDiffers()
320+
{
321+
/** @var \Illuminate\Cache\RedisStore $store */
322+
$store = Cache::store('redis');
323+
$store->setConnection('default');
324+
$store->setLockConnection('locks');
325+
326+
$this->assertTrue($store->hasSeparateLockStore());
327+
}
328+
329+
public function testHasSeparateLockStoreReturnsFalseWhenLockConnectionIsSame()
330+
{
331+
/** @var \Illuminate\Cache\RedisStore $store */
332+
$store = Cache::store('redis');
333+
$store->setConnection('default');
334+
$store->setLockConnection('default');
335+
336+
$this->assertFalse($store->hasSeparateLockStore());
337+
}
338+
339+
public function testFlushLocksThrowsExceptionWhenLockConnectionIsSame()
340+
{
341+
/** @var \Illuminate\Cache\RedisStore $store */
342+
$store = Cache::store('redis');
343+
$store->setConnection('default');
344+
$store->setLockConnection('default');
345+
346+
$this->expectException(RuntimeException::class);
347+
348+
$store->flushLocks();
349+
}
317350
}

tests/Integration/Database/DatabaseCacheStoreTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Illuminate\Tests\Integration\Database;
44

5+
use Illuminate\Cache\DatabaseStore;
56
use Illuminate\Database\SQLiteConnection;
67
use Illuminate\Support\Carbon;
78
use Illuminate\Support\Facades\Cache;
89
use Illuminate\Support\Facades\DB;
910
use Orchestra\Testbench\Attributes\WithMigration;
11+
use RuntimeException;
1012

1113
#[WithMigration('cache')]
1214
class DatabaseCacheStoreTest extends DatabaseTestCase
@@ -291,6 +293,41 @@ public function testFlushLocksRemovesExpiredLocksToo()
291293
$this->assertDatabaseMissing($this->getLocksTableName(), ['key' => $this->withCachePrefix('stale-lock')]);
292294
}
293295

296+
public function testHasSeparateLockStoreReturnsTrueWhenTablesAreDifferent()
297+
{
298+
$store = new DatabaseStore(
299+
connection: DB::connection(),
300+
table: $this->getCacheTableName(),
301+
lockTable: $this->getLocksTableName(),
302+
);
303+
304+
$this->assertTrue($store->hasSeparateLockStore());
305+
}
306+
307+
public function testHasSeparateLockStoreReturnsFalseWhenTablesAreTheSame()
308+
{
309+
$store = new DatabaseStore(
310+
connection: DB::connection(),
311+
table: $this->getCacheTableName(),
312+
lockTable: $this->getCacheTableName(),
313+
);
314+
315+
$this->assertFalse($store->hasSeparateLockStore());
316+
}
317+
318+
public function testFlushLocksThrowsExceptionWhenTablesAreTheSame()
319+
{
320+
$store = new DatabaseStore(
321+
connection: DB::connection(),
322+
table: $this->getCacheTableName(),
323+
lockTable: $this->getCacheTableName(),
324+
);
325+
326+
$this->expectException(RuntimeException::class);
327+
328+
$store->flushLocks();
329+
}
330+
294331
/**
295332
* @return \Illuminate\Cache\DatabaseStore
296333
*/

0 commit comments

Comments
 (0)