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.

123 lines
3.5 KiB

5 years ago
  1. <?php
  2. use Illuminate\Container\Container;
  3. use Illuminate\Database\Capsule\Manager as Capsule;
  4. use Illuminate\Events\Dispatcher;
  5. use Efriandika\LaravelSettings\Settings;
  6. use Efriandika\LaravelSettings\Cache;
  7. class SettingsTest extends PHPUnit_Framework_TestCase
  8. {
  9. protected $settings;
  10. protected $db;
  11. protected $config;
  12. protected function setUp()
  13. {
  14. $this->db = $this->initDb();
  15. $this->config = [
  16. 'db_table' => 'settings',
  17. 'cache_file' => storage_path('settings.json'),
  18. ];
  19. $this->settings = new Settings($this->db, new Cache($this->config['cache_file']), $this->config);
  20. }
  21. public function testSet()
  22. {
  23. $this->settings->set('key', 'value');
  24. $setting = $this->db->table($this->config['db_table'])->where('key', 'key')->first(['value']);
  25. $this->assertEquals('value', unserialize($setting['value']));
  26. }
  27. public function testSetArray()
  28. {
  29. $set = ['valuekey' => 'value'];
  30. $this->settings->set('key', $set);
  31. $setting = $this->db->table($this->config['db_table'])->where('key', 'key')->first(['value']);
  32. $this->assertEquals($set, unserialize($setting['value']));
  33. $this->assertEquals($set, $this->settings->get('key'));
  34. }
  35. public function testGet()
  36. {
  37. $this->settings->set('key', 'value');
  38. $this->assertEquals('value', $this->settings->get('key'));
  39. }
  40. public function testGetAll()
  41. {
  42. $this->settings->set('key', 'value');
  43. $this->settings->set('key2', 'value2');
  44. $this->assertEquals('value', $this->settings->get('key'));
  45. $this->assertEquals('value2', $this->settings->get('key2'));
  46. $this->assertEquals(['key' => 'value', 'key2' => 'value2'], $this->settings->getAll());
  47. }
  48. public function testFlush()
  49. {
  50. $this->settings->set('key', 'value');
  51. $this->settings->flush();
  52. $this->assertEquals([], $this->settings->getAll());
  53. }
  54. public function testHasKey()
  55. {
  56. $this->settings->set('key', 'value');
  57. $this->assertTrue($this->settings->hasKey('key'));
  58. $this->assertFalse($this->settings->hasKey('key2'));
  59. }
  60. public function testHasKeyWithoutCache()
  61. {
  62. $this->settings->set('key', 'value');
  63. $this->assertTrue($this->settings->hasKey('key'));
  64. $this->assertFalse($this->settings->hasKey('key2'));
  65. @unlink(storage_path('settings.json'));
  66. $this->assertTrue($this->settings->hasKey('key'));
  67. $this->assertFalse($this->settings->hasKey('key2'));
  68. }
  69. public function testForget()
  70. {
  71. $this->settings->set('key', 'value');
  72. $this->settings->forget('key');
  73. $this->assertNull($this->settings->get('key'));
  74. }
  75. protected function tearDown()
  76. {
  77. Capsule::schema()->drop('settings');
  78. @unlink(storage_path('settings.json'));
  79. }
  80. private function initDb()
  81. {
  82. $capsule = new Capsule;
  83. $capsule->addConnection([
  84. 'driver' => 'sqlite',
  85. 'host' => 'localhost',
  86. 'database' => ':memory:',
  87. 'prefix' => '',
  88. ]);
  89. $capsule->setEventDispatcher(new Dispatcher(new Container));
  90. $capsule->setAsGlobal();
  91. $capsule->bootEloquent();
  92. Capsule::schema()->create('settings', function ($table) {
  93. $table->string('key', 100)->index()->unique('key');
  94. $table->text('value', 65535)->nullable();
  95. });
  96. return $capsule->getDatabaseManager();
  97. }
  98. }