2017-12-09 22:39:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Integration\Models;
|
|
|
|
|
|
|
|
use App\Models\Setting;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class SettingTest extends TestCase
|
|
|
|
{
|
2017-12-10 00:23:37 +00:00
|
|
|
/** @test */
|
|
|
|
public function it_sets_a_key_value_pair()
|
|
|
|
{
|
|
|
|
// Given a key-value pair
|
|
|
|
$key = 'foo';
|
|
|
|
$value = 'bar';
|
2017-12-09 22:39:34 +00:00
|
|
|
|
2017-12-10 00:23:37 +00:00
|
|
|
// When I call the method to save the key-value
|
|
|
|
Setting::set($key, $value);
|
2017-12-09 22:39:34 +00:00
|
|
|
|
2017-12-10 00:23:37 +00:00
|
|
|
// Then I see the key and serialized value in the database
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertDatabaseHas('settings', [
|
2017-12-10 00:23:37 +00:00
|
|
|
'key' => 'foo',
|
|
|
|
'value' => serialize('bar'),
|
|
|
|
]);
|
|
|
|
}
|
2017-12-09 22:39:34 +00:00
|
|
|
|
2017-12-10 00:23:37 +00:00
|
|
|
/** @test */
|
|
|
|
public function it_supports_associative_arrays_when_saving_settings()
|
|
|
|
{
|
|
|
|
// Given an associative array of multiple settings
|
|
|
|
$settings = [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'baz' => 'qux',
|
|
|
|
];
|
2017-12-09 22:39:34 +00:00
|
|
|
|
2017-12-10 00:23:37 +00:00
|
|
|
// When I call the method to save the settings
|
|
|
|
Setting::set($settings);
|
2017-12-09 22:39:34 +00:00
|
|
|
|
2017-12-10 00:23:37 +00:00
|
|
|
// Then I see all settings the database
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertDatabaseHas('settings', [
|
2017-12-10 00:23:37 +00:00
|
|
|
'key' => 'foo',
|
|
|
|
'value' => serialize('bar'),
|
|
|
|
])->assertDatabaseHas('settings', [
|
|
|
|
'key' => 'baz',
|
|
|
|
'value' => serialize('qux'),
|
|
|
|
]);
|
|
|
|
}
|
2017-12-09 22:39:34 +00:00
|
|
|
|
2017-12-10 00:23:37 +00:00
|
|
|
/** @test */
|
|
|
|
public function existing_settings_should_be_updated()
|
|
|
|
{
|
|
|
|
Setting::set('foo', 'bar');
|
|
|
|
Setting::set('foo', 'baz');
|
2017-12-09 22:39:34 +00:00
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertEquals('baz', Setting::get('foo'));
|
2017-12-10 00:23:37 +00:00
|
|
|
}
|
2017-12-09 22:39:34 +00:00
|
|
|
|
2017-12-10 00:23:37 +00:00
|
|
|
/** @test */
|
|
|
|
public function it_gets_the_setting_value_in_an_unserialized_format()
|
|
|
|
{
|
|
|
|
// Given a setting in the database
|
|
|
|
factory(Setting::class)->create([
|
|
|
|
'key' => 'foo',
|
|
|
|
'value' => 'bar',
|
|
|
|
]);
|
2017-12-09 22:39:34 +00:00
|
|
|
|
2017-12-10 00:23:37 +00:00
|
|
|
// When I get the setting using the key
|
|
|
|
$value = Setting::get('foo');
|
2017-12-09 22:39:34 +00:00
|
|
|
|
2017-12-10 00:23:37 +00:00
|
|
|
// Then I receive the value in an unserialized format
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertSame('bar', $value);
|
2017-12-10 00:23:37 +00:00
|
|
|
}
|
2017-12-09 22:39:34 +00:00
|
|
|
}
|