1414use Psr \SimpleCache \InvalidArgumentException ;
1515use ReflectionException ;
1616use stdClass ;
17+ use Yiisoft \Cache \File \CacheException ;
1718use Yiisoft \Cache \File \FileCache ;
1819use Yiisoft \Cache \File \MockHelper ;
1920
2021use function array_keys ;
2122use function array_map ;
2223use function dirname ;
2324use function fileperms ;
25+ use function file_put_contents ;
2426use function function_exists ;
2527use function glob ;
2628use function is_dir ;
@@ -39,20 +41,22 @@ final class FileCacheTest extends TestCase
3941{
4042 use PHPMock;
4143
44+ private const RUNTIME_DIRECTORY = __DIR__ . '/runtime ' ;
45+
4246 private FileCache $ cache ;
4347 private string $ tmpDir ;
4448
4549 public function setUp (): void
4650 {
47- $ this ->cache = new FileCache (__DIR__ . '/runtime /cache ' );
51+ $ this ->cache = new FileCache (self :: RUNTIME_DIRECTORY . '/cache ' );
4852 $ this ->tmpDir = sys_get_temp_dir () . '/yiisoft-test-file-cache ' ;
4953 }
5054
5155 public function tearDown (): void
5256 {
5357 MockHelper::$ time = null ;
5458 $ this ->removeDirectory ($ this ->tmpDir );
55- $ this ->removeDirectory (__DIR__ . ' /runtime ' );
59+ $ this ->removeDirectory (self :: RUNTIME_DIRECTORY );
5660 }
5761
5862 /**
@@ -483,6 +487,33 @@ public function testGcProbability(): void
483487 $ this ->assertFileDoesNotExist ($ cacheFile );
484488 }
485489
490+ public function testDeleteForCacheItemNotExist (): void
491+ {
492+ $ this ->assertNull ($ this ->cache ->get ('key ' ));
493+ $ this ->assertTrue ($ this ->cache ->delete ('key ' ));
494+ $ this ->assertNull ($ this ->cache ->get ('key ' ));
495+ }
496+
497+ public function testSetThrowExceptionForInvalidCacheDirectory (): void
498+ {
499+ $ directory = self ::RUNTIME_DIRECTORY . '/cache/fail ' ;
500+ $ cache = new FileCache ($ directory );
501+
502+ $ this ->removeDirectory ($ directory );
503+ file_put_contents ($ directory , 'fail ' );
504+
505+ $ this ->expectException (CacheException::class);
506+ $ cache ->set ('key ' , 'value ' );
507+ }
508+
509+ public function testConstructorThrowExceptionForInvalidCacheDirectory (): void
510+ {
511+ $ file = self ::RUNTIME_DIRECTORY . '/fail ' ;
512+ file_put_contents ($ file , 'fail ' );
513+ $ this ->expectException (CacheException::class);
514+ new FileCache ($ file );
515+ }
516+
486517 public function invalidKeyProvider (): array
487518 {
488519 return [
@@ -502,7 +533,7 @@ public function invalidKeyProvider(): array
502533 *
503534 * @param mixed $key
504535 */
505- public function testGetInvalidKey ($ key ): void
536+ public function testGetThrowExceptionForInvalidKey ($ key ): void
506537 {
507538 $ this ->expectException (InvalidArgumentException::class);
508539 $ this ->cache ->get ($ key );
@@ -513,7 +544,7 @@ public function testGetInvalidKey($key): void
513544 *
514545 * @param mixed $key
515546 */
516- public function testSetInvalidKey ($ key ): void
547+ public function testSetThrowExceptionForInvalidKey ($ key ): void
517548 {
518549 $ this ->expectException (InvalidArgumentException::class);
519550 $ this ->cache ->set ($ key , 'value ' );
@@ -524,7 +555,7 @@ public function testSetInvalidKey($key): void
524555 *
525556 * @param mixed $key
526557 */
527- public function testDeleteInvalidKey ($ key ): void
558+ public function testDeleteThrowExceptionForInvalidKey ($ key ): void
528559 {
529560 $ this ->expectException (InvalidArgumentException::class);
530561 $ this ->cache ->delete ($ key );
@@ -535,7 +566,7 @@ public function testDeleteInvalidKey($key): void
535566 *
536567 * @param mixed $key
537568 */
538- public function testGetMultipleInvalidKeys ($ key ): void
569+ public function testGetMultipleThrowExceptionForInvalidKeys ($ key ): void
539570 {
540571 $ this ->expectException (InvalidArgumentException::class);
541572 $ this ->cache ->getMultiple ([$ key ]);
@@ -546,7 +577,7 @@ public function testGetMultipleInvalidKeys($key): void
546577 *
547578 * @param mixed $key
548579 */
549- public function testGetMultipleInvalidKeysNotIterable ($ key ): void
580+ public function testGetMultipleThrowExceptionForInvalidKeysNotIterable ($ key ): void
550581 {
551582 $ this ->expectException (InvalidArgumentException::class);
552583 $ this ->cache ->getMultiple ($ key );
@@ -557,7 +588,7 @@ public function testGetMultipleInvalidKeysNotIterable($key): void
557588 *
558589 * @param mixed $key
559590 */
560- public function testSetMultipleInvalidKeysNotIterable ($ key ): void
591+ public function testSetMultipleThrowExceptionForInvalidKeysNotIterable ($ key ): void
561592 {
562593 $ this ->expectException (InvalidArgumentException::class);
563594 $ this ->cache ->setMultiple ($ key );
@@ -568,7 +599,7 @@ public function testSetMultipleInvalidKeysNotIterable($key): void
568599 *
569600 * @param mixed $key
570601 */
571- public function testDeleteMultipleInvalidKeys ($ key ): void
602+ public function testDeleteMultipleThrowExceptionForInvalidKeys ($ key ): void
572603 {
573604 $ this ->expectException (InvalidArgumentException::class);
574605 $ this ->cache ->deleteMultiple ([$ key ]);
@@ -579,7 +610,7 @@ public function testDeleteMultipleInvalidKeys($key): void
579610 *
580611 * @param mixed $key
581612 */
582- public function testDeleteMultipleInvalidKeysNotIterable ($ key ): void
613+ public function testDeleteMultipleThrowExceptionForInvalidKeysNotIterable ($ key ): void
583614 {
584615 $ this ->expectException (InvalidArgumentException::class);
585616 $ this ->cache ->deleteMultiple ($ key );
@@ -590,7 +621,7 @@ public function testDeleteMultipleInvalidKeysNotIterable($key): void
590621 *
591622 * @param mixed $key
592623 */
593- public function testHasInvalidKey ($ key ): void
624+ public function testHasThrowExceptionForInvalidKey ($ key ): void
594625 {
595626 $ this ->expectException (InvalidArgumentException::class);
596627 $ this ->cache ->has ($ key );
0 commit comments