Complete Setting tests

This commit is contained in:
Phan An 2017-08-05 17:32:16 +01:00
parent d66522bd37
commit 9cd1e86533
3 changed files with 88 additions and 0 deletions

View file

@ -59,6 +59,13 @@ class Setting extends Model
$this->attributes['value'] = serialize($value);
}
/**
* Get the unserialized setting value.
*
* @param mixed $value
*
* @return mixed
*/
public function getValueAttribute($value)
{
return unserialize($value);

View file

@ -61,3 +61,10 @@ $factory->define(\App\Models\Interaction::class, function ($faker) {
'play_count' => $faker->randomNumber,
];
});
$factory->define(\App\Models\Setting::class, function ($faker) {
return [
'key' => $faker->slug,
'value' => $faker->name,
];
});

View file

@ -0,0 +1,74 @@
<?php
namespace Tests\Unit;
use App\Models\Setting;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class SettingTest extends TestCase
{
use DatabaseMigrations, DatabaseTransactions;
/** @test */
public function it_can_be_instantiated()
{
$this->assertInstanceOf(Setting::class, new Setting());
}
/** @test */
public function it_sets_a_key_value_pair()
{
// Given a key-value pair
$key = 'foo';
$value = 'bar';
// When I call the method to save the key-value
Setting::set($key, $value);
// Then I see the key and serialized value in the database
$this->assertDatabaseHas('settings', [
'key' => 'foo',
'value' => serialize('bar'),
]);
}
/** @test */
public function it_supports_associative_arrays_when_saving_settings()
{
// Given an associative array of multiple settings
$settings = [
'foo' => 'bar',
'baz' => 'qux',
];
// When I call the method to save the settings
Setting::set($settings);
// Then I see all settings the database
$this->assertDatabaseHas('settings', [
'key' => 'foo',
'value' => serialize('bar'),
])->assertDatabaseHas('settings', [
'key' => 'baz',
'value' => serialize('qux'),
]);
}
/** @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',
]);
// When I get the setting using the key
$value = Setting::get('foo');
// Then I receive the value in an unserialized format
$this->assertSame('bar', $value);
}
}