Without share.

This commit is contained in:
2019-11-15 12:14:38 +03:00
commit b3c03162f1
16 changed files with 898 additions and 0 deletions

73
tests/CacheTest.php Executable file
View File

@ -0,0 +1,73 @@
<?php
use Efriandika\LaravelSettings\Cache;
class CacheTest extends PHPUnit_Framework_TestCase
{
protected $cache;
protected $cacheFile;
protected function setUp()
{
$this->cacheFile = storage_path('settings.json');
$this->cache = new Cache($this->cacheFile);
}
public function testSet()
{
$this->cache->set('key', 'value');
$contents = file_get_contents($this->cacheFile);
$this->assertEquals('{"key":"s:5:\"value\";"}', $contents);
}
public function testSetArray()
{
$set = ['value' => 1, 'value2' => 2];
$this->cache->set('key', $set);
$contents = file_get_contents($this->cacheFile);
$this->assertEquals('{"key":"a:2:{s:5:\"value\";i:1;s:6:\"value2\";i:2;}"}', $contents);
$this->assertEquals($this->cache->get('key'), $set);
}
public function testGet()
{
$this->cache->set('key', 'value');
$this->assertEquals('value', $this->cache->get('key'));
}
public function testGetAll()
{
$this->cache->set('key', 'value');
$this->cache->set('key2', 'value2');
$this->assertEquals(['key' => 'value', 'key2' => 'value2'], $this->cache->getAll());
}
public function testFlush()
{
$this->cache->set('key', 'value');
$this->cache->flush();
$this->assertEquals([], $this->cache->getAll());
}
public function testHasKey()
{
$this->cache->set('key', 'value');
$this->assertTrue($this->cache->hasKey('key'));
}
public function testForget()
{
$this->cache->set('key', 'value');
$this->cache->forget('key');
$this->assertNull($this->cache->get('key'));
}
protected function tearDown()
{
@unlink(storage_path('settings.json'));
}
}

123
tests/SettingsTest.php Executable file
View File

@ -0,0 +1,123 @@
<?php
use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Dispatcher;
use Efriandika\LaravelSettings\Settings;
use Efriandika\LaravelSettings\Cache;
class SettingsTest extends PHPUnit_Framework_TestCase
{
protected $settings;
protected $db;
protected $config;
protected function setUp()
{
$this->db = $this->initDb();
$this->config = [
'db_table' => 'settings',
'cache_file' => storage_path('settings.json'),
];
$this->settings = new Settings($this->db, new Cache($this->config['cache_file']), $this->config);
}
public function testSet()
{
$this->settings->set('key', 'value');
$setting = $this->db->table($this->config['db_table'])->where('key', 'key')->first(['value']);
$this->assertEquals('value', unserialize($setting['value']));
}
public function testSetArray()
{
$set = ['valuekey' => 'value'];
$this->settings->set('key', $set);
$setting = $this->db->table($this->config['db_table'])->where('key', 'key')->first(['value']);
$this->assertEquals($set, unserialize($setting['value']));
$this->assertEquals($set, $this->settings->get('key'));
}
public function testGet()
{
$this->settings->set('key', 'value');
$this->assertEquals('value', $this->settings->get('key'));
}
public function testGetAll()
{
$this->settings->set('key', 'value');
$this->settings->set('key2', 'value2');
$this->assertEquals('value', $this->settings->get('key'));
$this->assertEquals('value2', $this->settings->get('key2'));
$this->assertEquals(['key' => 'value', 'key2' => 'value2'], $this->settings->getAll());
}
public function testFlush()
{
$this->settings->set('key', 'value');
$this->settings->flush();
$this->assertEquals([], $this->settings->getAll());
}
public function testHasKey()
{
$this->settings->set('key', 'value');
$this->assertTrue($this->settings->hasKey('key'));
$this->assertFalse($this->settings->hasKey('key2'));
}
public function testHasKeyWithoutCache()
{
$this->settings->set('key', 'value');
$this->assertTrue($this->settings->hasKey('key'));
$this->assertFalse($this->settings->hasKey('key2'));
@unlink(storage_path('settings.json'));
$this->assertTrue($this->settings->hasKey('key'));
$this->assertFalse($this->settings->hasKey('key2'));
}
public function testForget()
{
$this->settings->set('key', 'value');
$this->settings->forget('key');
$this->assertNull($this->settings->get('key'));
}
protected function tearDown()
{
Capsule::schema()->drop('settings');
@unlink(storage_path('settings.json'));
}
private function initDb()
{
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'sqlite',
'host' => 'localhost',
'database' => ':memory:',
'prefix' => '',
]);
$capsule->setEventDispatcher(new Dispatcher(new Container));
$capsule->setAsGlobal();
$capsule->bootEloquent();
Capsule::schema()->create('settings', function ($table) {
$table->string('key', 100)->index()->unique('key');
$table->text('value', 65535)->nullable();
});
return $capsule->getDatabaseManager();
}
}

7
tests/bootstrap.php Executable file
View File

@ -0,0 +1,7 @@
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
function storage_path($filename) {
return dirname(__DIR__) . '/tests/' . $filename;
}