FilesystemTest.php
121 lines
| 1 | <?php |
| 2 | /** |
| 3 | * FilesystemTest.php |
| 4 | * |
| 5 | * @package Tests |
| 6 | * @author Michael Pratt <yo@michael-pratt.com> |
| 7 | * @link http://www.michael-pratt.com/ |
| 8 | * |
| 9 | * For the full copyright and license information, please view the LICENSE |
| 10 | * file that was distributed with this source code. |
| 11 | */ |
| 12 | namespace Embera\Cache; |
| 13 | |
| 14 | use PHPUnit\Framework\TestCase; |
| 15 | |
| 16 | use DateTime; |
| 17 | use DateInterval; |
| 18 | |
| 19 | class FilesystemTest extends TestCase |
| 20 | { |
| 21 | public function testCanStoreStringsArraysAndObjects() |
| 22 | { |
| 23 | $cache = new Filesystem(sys_get_temp_dir(), mt_rand(1, 5)); |
| 24 | $cache->clear(); |
| 25 | |
| 26 | $data = [ md5(mt_rand(1000, 9000)), sha1(mt_rand(1000, 9000)), mt_rand(1000, 9000), 'This is a string! ? \' 345 345 sdf # @ $ % & *']; |
| 27 | |
| 28 | $this->assertTrue($cache->set('array', $data)); |
| 29 | $this->assertEquals($cache->get('array'), $data); |
| 30 | |
| 31 | $this->assertTrue($cache->set('object', (object) $data)); |
| 32 | $this->assertEquals($cache->get('object'), (object) $data); |
| 33 | |
| 34 | $this->assertTrue($cache->set('string', end($data))); |
| 35 | $this->assertEquals($cache->get('string'), end($data)); |
| 36 | } |
| 37 | |
| 38 | public function testBehaviourOnOverwrite() |
| 39 | { |
| 40 | $cache = new Filesystem(sys_get_temp_dir(), mt_rand(1, 5)); |
| 41 | $this->assertTrue($cache->set('twice', 'this is the first string', 5)); |
| 42 | $this->assertTrue($cache->set('twice', 'This is the second string', 2)); |
| 43 | $this->assertEquals($cache->get('twice'), 'This is the second string'); |
| 44 | } |
| 45 | |
| 46 | public function testNonExistantKeys() |
| 47 | { |
| 48 | $cache = new Filesystem(sys_get_temp_dir(), mt_rand(1, 5)); |
| 49 | $this->assertNull($cache->get('unknown_key')); |
| 50 | } |
| 51 | |
| 52 | public function testCacheDuration() |
| 53 | { |
| 54 | $expirationDate = new DateTime('Now'); |
| 55 | $expirationDate->add(new DateInterval('PT2S')); |
| 56 | |
| 57 | $cache = new Filesystem(sys_get_temp_dir(), mt_rand(1, 5)); |
| 58 | $this->assertTrue($cache->set('test_duration', 'dummy data', $expirationDate)); |
| 59 | sleep(4); |
| 60 | |
| 61 | $this->assertNull($cache->get('test_duration')); |
| 62 | } |
| 63 | |
| 64 | public function testDeleteCache() |
| 65 | { |
| 66 | $cache = new Filesystem(sys_get_temp_dir(), mt_rand(1, 5)); |
| 67 | |
| 68 | $this->assertTrue($cache->set('deleted_key', 'dummy data')); |
| 69 | $this->assertTrue($cache->has('deleted_key')); |
| 70 | $this->assertTrue($cache->delete('deleted_key')); |
| 71 | $this->assertNull($cache->get('deleted_key')); |
| 72 | $this->assertFalse($cache->has('deleted_key')); |
| 73 | $this->assertFalse($cache->delete('deleted_key')); |
| 74 | } |
| 75 | |
| 76 | public function testEnabledConditions() |
| 77 | { |
| 78 | $cache = new Filesystem('/path/unknown/' . mt_rand(0,5000), 1); |
| 79 | $this->assertFalse($cache->isEnabled()); |
| 80 | |
| 81 | $cache = new Filesystem(sys_get_temp_dir(), 1); |
| 82 | $cache->set('enable_test', 'dummy_data_1'); |
| 83 | $this->assertTrue($cache->has('enable_test')); |
| 84 | $this->assertEquals($cache->get('enable_test'), 'dummy_data_1'); |
| 85 | $this->assertTrue($cache->delete('enable_test')); |
| 86 | |
| 87 | $cache->disable(); |
| 88 | $this->assertFalse($cache->isEnabled()); |
| 89 | $cache->set('enable_test', 'dummy_data_1'); |
| 90 | $this->assertFalse($cache->has('enable_test')); |
| 91 | $this->assertNull($cache->get('enable_test')); |
| 92 | $this->assertFalse($cache->delete('enable_test')); |
| 93 | $this->assertFalse($cache->clear()); |
| 94 | |
| 95 | $cache->enable(); |
| 96 | $cache->set('enable_test', 'dummy_data_1'); |
| 97 | $this->assertTrue($cache->has('enable_test')); |
| 98 | $this->assertEquals($cache->get('enable_test'), 'dummy_data_1'); |
| 99 | $this->assertTrue($cache->delete('enable_test')); |
| 100 | } |
| 101 | |
| 102 | public function testClearCache() |
| 103 | { |
| 104 | $cache = new Filesystem(sys_get_temp_dir(), 1); |
| 105 | $cache->set('clear_1', 'dummy_data_1'); |
| 106 | $cache->set('clear_2', 'dummy_data_2'); |
| 107 | $cache->set('clear_3', 'dummy_data_3'); |
| 108 | |
| 109 | $this->assertEquals($cache->get('clear_1'), 'dummy_data_1'); |
| 110 | $this->assertEquals($cache->get('clear_2'), 'dummy_data_2'); |
| 111 | $this->assertEquals($cache->get('clear_3'), 'dummy_data_3'); |
| 112 | |
| 113 | $this->assertTrue($cache->clear()); |
| 114 | |
| 115 | $this->assertEquals($cache->get('clear_1'), null); |
| 116 | $this->assertEquals($cache->get('clear_2'), null); |
| 117 | $this->assertEquals($cache->get('clear_3'), null); |
| 118 | } |
| 119 | |
| 120 | } |
| 121 |