Skip to content

Commit

Permalink
Store all values as JSON to avoid type issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleigh committed Jun 6, 2016
1 parent 6274078 commit 2183329
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function up()
$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->string('key');
$table->boolean('value');
$table->json('value');
$table->timestamps();

$table->foreign('user_id')->references('id')->on('users');
Expand Down
19 changes: 15 additions & 4 deletions src/Settings/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ public function flagAsChanged($key, $syncType)
*/
public function refreshSettings()
{
$this->settings = $this->resource->allSettings();
$this->resource->load('propertyBag');

$this->settings = $this->resource->allSettings()
->flatMap(function ($model) {
return [$model->key => json_decode($model->value)[0]];
});
}

/**
Expand Down Expand Up @@ -244,7 +249,7 @@ protected function newRecord($key)
return $model::create([
$this->primaryKey => $this->resource->id(),
'key' => $key,
'value' => $this->get($key),
'value' => json_encode([$this->get($key)]),
]);
}

Expand All @@ -259,9 +264,15 @@ protected function updateRecord($key)
{
$model = $this->resource->getPropertyBagClass();

return $model::where($this->primaryKey, '=', $this->resource->id())
$record = $model::where($this->primaryKey, '=', $this->resource->id())
->where('key', '=', $key)
->update(['value' => $this->get($key)]);
->first();

$record->value = json_encode([$this->get($key)]);

$record->save();

return $record;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/UserSettings/UserPropertyBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ class UserPropertyBag extends Model
'value'
];

/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'value' => 'array'
];

/**
* The table associated with the model.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/Classes/GroupPropertyBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ class GroupPropertyBag extends Model
'value'
];

/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'value' => 'array'
];

/**
* The table associated with the model.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/Migrations/CreateGroupSettingsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function up()
$table->increments('id');
$table->integer('group_id')->unsigned()->index();
$table->string('key');
$table->boolean('value');
$table->json('value');
$table->timestamps();

$table->foreign('group_id')->references('id')->on('users');
Expand Down
112 changes: 15 additions & 97 deletions tests/PropertyBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Support\Collection;
use LaravelPropertyBag\UserSettings\UserSettings;

class SettingsTest extends TestCase
class PropertyBagTest extends TestCase
{
/**
* @test
Expand Down Expand Up @@ -103,7 +103,7 @@ public function adding_a_new_setting_creates_a_new_user_setting_record()
$this->seeInDatabase('user_property_bag', [
'user_id' => $user->id(),
'key' => 'test_settings2',
'value' => 1
'value' => json_encode('[true]')
]);
}

Expand All @@ -127,7 +127,7 @@ public function updating_a_setting_updates_the_setting()
$this->seeInDatabase('user_property_bag', [
'user_id' => $user->id(),
'key' => 'test_settings2',
'value' => 0
'value' => json_encode('[false]')
]);
}

Expand Down Expand Up @@ -156,7 +156,7 @@ public function a_user_can_set_many_settings_at_once()
$this->seeInDatabase('user_property_bag', [
'user_id' => $user->id(),
'key' => 'test_settings1',
'value' => 'grapes'
'value' => json_encode('["grapes"]')
]);

$this->assertContains('test_settings2', $settings->all());
Expand All @@ -166,7 +166,7 @@ public function a_user_can_set_many_settings_at_once()
$this->seeInDatabase('user_property_bag', [
'user_id' => $user->id(),
'key' => 'test_settings2',
'value' => 1
'value' => json_encode('[true]')
]);
}

Expand All @@ -178,6 +178,14 @@ public function only_changed_settings_are_updated()
// TODO
}

/**
* @test
*/
public function settings_on_the_object_match_the_settings_in_the_database()
{
// TODO
}

/**
* @test
*/
Expand Down Expand Up @@ -266,7 +274,7 @@ public function a_resource_can_access_and_use_the_property_bag()
$this->seeInDatabase('group_settings', [
'group_id' => $group->id(),
'key' => 'test_settings1',
'value' => 'grapes'
'value' => json_encode('["grapes"]')
]);

$this->assertContains('test_settings2', $settings->all());
Expand All @@ -276,7 +284,7 @@ public function a_resource_can_access_and_use_the_property_bag()
$this->seeInDatabase('group_settings', [
'group_id' => $group->id(),
'key' => 'test_settings2',
'value' => 1
'value' => json_encode('[true]')
]);
}

Expand All @@ -292,96 +300,6 @@ public function settings_can_be_registered_on_settings_class()
$this->assertTrue($settings->isRegistered('test_settings1'));
}

/**
* @test
*/
public function it_distinguishes_between_bool_and_int_types()
{
$user = $this->makeUser();

$this->actingAs($user);

$user->settings($this->registered)->set(['test_settings3' => true]);

settings()->refreshSettings();

$result = settings()->get('test_settings3');

$this->assertTrue($result === true);

$this->assertTrue($result !== 1);

$user->settings($this->registered)->set(['test_settings3' => 1]);

settings()->refreshSettings();

$result = settings()->get('test_settings3');

$this->assertTrue($result === 1);

$this->assertTrue($result !== true);
}

/**
* @test
*/
public function it_distinguishes_between_bool_and_string_types()
{
$user = $this->makeUser();

$this->actingAs($user);

$user->settings($this->registered)->set(['test_settings3' => 'true']);

settings()->refreshSettings();

$result = settings()->get('test_settings3');

$this->assertTrue($result === 'true');

$this->assertTrue($result !== true);

$user->settings($this->registered)->set(['test_settings3' => false]);

settings()->refreshSettings();

$result = settings()->get('test_settings3');

$this->assertTrue($result === false);

$this->assertTrue($result !== 'false');
}

/**
* @test
*/
public function it_distinguishes_between_int_and_string_types()
{
$user = $this->makeUser();

$this->actingAs($user);

$user->settings($this->registered)->set(['test_settings3' => 1]);

settings()->refreshSettings();

$result = settings()->get('test_settings3');

$this->assertTrue($result === 1);

$this->assertTrue($result !== '1');

$user->settings($this->registered)->set(['test_settings3' => '0']);

settings()->refreshSettings();

$result = settings()->get('test_settings3');

$this->assertTrue($result === '0');

$this->assertTrue($result !== 0);
}

/**
* @test
*/
Expand Down
99 changes: 99 additions & 0 deletions tests/TypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace LaravelPropertyBag\tests;

use Illuminate\Support\Collection;
use LaravelPropertyBag\UserSettings\UserSettings;

class TypeTest extends TestCase
{
/**
* @test
*/
public function it_distinguishes_between_bool_and_int_types()
{
$user = $this->makeUser();

$this->actingAs($user);

$user->settings($this->registered)->set(['test_settings3' => true]);

settings()->refreshSettings();

$result = settings()->get('test_settings3');

$this->assertTrue($result === true);

$this->assertTrue($result !== 1);

$user->settings($this->registered)->set(['test_settings3' => 1]);

settings()->refreshSettings();

$result = settings()->get('test_settings3');

$this->assertTrue($result === 1);

$this->assertTrue($result !== true);
}

/**
* @test
*/
public function it_distinguishes_between_bool_and_string_types()
{
$user = $this->makeUser();

$this->actingAs($user);

$user->settings($this->registered)->set(['test_settings3' => 'true']);

settings()->refreshSettings();

$result = settings()->get('test_settings3');

$this->assertTrue($result === 'true');

$this->assertTrue($result !== true);

$user->settings($this->registered)->set(['test_settings3' => false]);

settings()->refreshSettings();

$result = settings()->get('test_settings3');

$this->assertTrue($result === false);

$this->assertTrue($result !== 'false');
}

/**
* @test
*/
public function it_distinguishes_between_int_and_string_types()
{
$user = $this->makeUser();

$this->actingAs($user);

$user->settings($this->registered)->set(['test_settings3' => 1]);

settings()->refreshSettings();

$result = settings()->get('test_settings3');

$this->assertTrue($result === 1);

$this->assertTrue($result !== '1');

$user->settings($this->registered)->set(['test_settings3' => '0']);

settings()->refreshSettings();

$result = settings()->get('test_settings3');

$this->assertTrue($result === '0');

$this->assertTrue($result !== 0);
}
}

0 comments on commit 2183329

Please sign in to comment.