You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.8 KiB

5 years ago
  1. <?php
  2. use Efriandika\LaravelSettings\Cache;
  3. class CacheTest extends PHPUnit_Framework_TestCase
  4. {
  5. protected $cache;
  6. protected $cacheFile;
  7. protected function setUp()
  8. {
  9. $this->cacheFile = storage_path('settings.json');
  10. $this->cache = new Cache($this->cacheFile);
  11. }
  12. public function testSet()
  13. {
  14. $this->cache->set('key', 'value');
  15. $contents = file_get_contents($this->cacheFile);
  16. $this->assertEquals('{"key":"s:5:\"value\";"}', $contents);
  17. }
  18. public function testSetArray()
  19. {
  20. $set = ['value' => 1, 'value2' => 2];
  21. $this->cache->set('key', $set);
  22. $contents = file_get_contents($this->cacheFile);
  23. $this->assertEquals('{"key":"a:2:{s:5:\"value\";i:1;s:6:\"value2\";i:2;}"}', $contents);
  24. $this->assertEquals($this->cache->get('key'), $set);
  25. }
  26. public function testGet()
  27. {
  28. $this->cache->set('key', 'value');
  29. $this->assertEquals('value', $this->cache->get('key'));
  30. }
  31. public function testGetAll()
  32. {
  33. $this->cache->set('key', 'value');
  34. $this->cache->set('key2', 'value2');
  35. $this->assertEquals(['key' => 'value', 'key2' => 'value2'], $this->cache->getAll());
  36. }
  37. public function testFlush()
  38. {
  39. $this->cache->set('key', 'value');
  40. $this->cache->flush();
  41. $this->assertEquals([], $this->cache->getAll());
  42. }
  43. public function testHasKey()
  44. {
  45. $this->cache->set('key', 'value');
  46. $this->assertTrue($this->cache->hasKey('key'));
  47. }
  48. public function testForget()
  49. {
  50. $this->cache->set('key', 'value');
  51. $this->cache->forget('key');
  52. $this->assertNull($this->cache->get('key'));
  53. }
  54. protected function tearDown()
  55. {
  56. @unlink(storage_path('settings.json'));
  57. }
  58. }