From 12884aa7fb8c8aeded02e1dff5bb2be78122b810 Mon Sep 17 00:00:00 2001 From: Zach Leigh Date: Tue, 7 Jun 2016 10:19:08 +0900 Subject: [PATCH] Better organize tests. --- tests/PropertyBagTest.php | 299 -------------------------------------- tests/UnitTest.php | 286 ++++++++++++++++++++++++++++++++++++ 2 files changed, 286 insertions(+), 299 deletions(-) create mode 100644 tests/UnitTest.php diff --git a/tests/PropertyBagTest.php b/tests/PropertyBagTest.php index 56d5170..9528d93 100644 --- a/tests/PropertyBagTest.php +++ b/tests/PropertyBagTest.php @@ -7,245 +7,6 @@ class PropertyBagTest extends TestCase { - /** - * @test - */ - public function a_user_can_access_the_settings_object() - { - $user = $this->makeUser(); - - $settings = $user->settings(); - - $this->assertInstanceOf(UserSettings::class, $settings); - } - - /** - * @test - */ - public function settings_can_be_accessed_from_the_helper_function() - { - $user = $this->makeUser(); - - $this->actingAs($user); - - $settings = settings(); - - $this->assertInstanceOf(UserSettings::class, $settings); - } - - /** - * @test - */ - public function a_valid_setting_key_value_pair_passes_validation() - { - $user = $this->makeUser(); - - $this->actingAs($user); - - $settings = $user->settings($this->registered); - - $result = $settings->isValid('test_settings1', 'bananas'); - - $this->assertTrue($result); - } - - /** - * @test - */ - public function an_invalid_setting_key_fails_validation() - { - $user = $this->makeUser(); - - $this->actingAs($user); - - $settings = $user->settings($this->registered); - - $result = $settings->isValid('fake', true); - - $this->assertFalse($result); - } - - /** - * @test - */ - public function an_invalid_setting_value_fails_validation() - { - $user = $this->makeUser(); - - $this->actingAs($user); - - $settings = $user->settings($this->registered); - - $result = $settings->isValid('test_settings2', 'ok'); - - $this->assertFalse($result); - } - - /** - * @test - */ - public function adding_a_new_setting_creates_a_new_user_setting_record() - { - $user = $this->makeUser(); - - $this->actingAs($user); - - $settings = $user->settings($this->registered); - - $this->assertEmpty($settings->all()); - - $settings->set(['test_settings3' => true]); - - $this->assertContains(true, $settings->all()); - - $this->assertEquals($settings->get('test_settings3'), true); - - $this->seeInDatabase('user_property_bag', [ - 'user_id' => $user->resourceId(), - 'key' => 'test_settings3', - 'value' => json_encode('[true]') - ]); - } - - /** - * @test - */ - public function updating_a_setting_updates_the_setting() - { - $user = $this->makeUser(); - - $this->actingAs($user); - - $settings = $user->settings($this->registered); - - $settings->set(['test_settings2' => true]); - - $settings->set(['test_settings2' => false]); - - $this->assertEquals($settings->get('test_settings2'), false); - - $this->seeInDatabase('user_property_bag', [ - 'user_id' => $user->resourceId(), - 'key' => 'test_settings2', - 'value' => json_encode('[false]') - ]); - } - - /** - * @test - */ - public function a_user_can_set_many_settings_at_once() - { - $user = $this->makeUser(); - - $this->actingAs($user); - - $settings = $user->settings($this->registered); - - $this->assertEmpty($settings->all()); - - $settings->set([ - 'test_settings1' => 'grapes', - 'test_settings2' => false, - ]); - - $this->assertContains('grapes', $settings->all()); - - $this->assertEquals($settings->get('test_settings1'), 'grapes'); - - $this->seeInDatabase('user_property_bag', [ - 'user_id' => $user->resourceId(), - 'key' => 'test_settings1', - 'value' => json_encode('["grapes"]') - ]); - - $this->assertContains(false, $settings->all()); - - $this->assertEquals($settings->get('test_settings2'), false); - - $this->seeInDatabase('user_property_bag', [ - 'user_id' => $user->resourceId(), - 'key' => 'test_settings2', - 'value' => json_encode('[false]') - ]); - } - - /** - * @test - */ - public function only_changed_settings_are_updated() - { - // TODO - } - - /** - * @test - */ - public function settings_on_the_object_match_the_settings_in_the_database() - { - // TODO - } - - /** - * @test - */ - public function a_user_can_get_a_setting() - { - $user = $this->makeUser(); - - $this->actingAs($user); - - $user->settings($this->registered)->set(['test_settings2' => true]); - - $result = $user->settings()->get('test_settings2'); - - $this->assertEquals(true, $result); - } - - /** - * @test - */ - public function a_user_can_get_a_setting_from_the_global_helper() - { - $user = $this->makeUser(); - - $this->actingAs($user); - - $user->settings($this->registered)->set(['test_settings2' => true]); - - $result = settings()->get('test_settings2'); - - $this->assertEquals(true, $result); - } - - /** - * @test - */ - public function if_the_setting_is_not_set_the_default_value_is_returned() - { - $user = $this->makeUser(); - - $this->actingAs($user); - - $result = $user->settings($this->registered)->get('test_settings1'); - - $this->assertEquals('monkey', $result); - } - - /** - * @test - */ - public function a_user_can_not_get_an_invalid_setting() - { - $user = $this->makeUser(); - - $this->actingAs($user); - - $result = $user->settings($this->registered)->get('invalid_setting'); - - $this->assertNull($result); - } - /** * @test */ @@ -288,34 +49,6 @@ public function a_resource_can_access_and_use_the_property_bag() ]); } - /** - * @test - */ - public function settings_can_be_registered_on_settings_class() - { - $group = $this->makeGroup(); - - $settings = $group->settings(); - - $this->assertTrue($settings->isRegistered('test_settings1')); - } - - /** - * @test - */ - public function settings_intsance_is_persisted_on_resource_model() - { - $user = $this->makeUser(); - - $this->actingAs($user); - - $result = $user->settings($this->registered)->get('test_settings1'); - - $result = $user->settings()->get('test_settings1'); - - $this->assertEquals('monkey', $result); - } - /** * @test */ @@ -367,36 +100,4 @@ public function local_settings_are_always_synced_with_database() $this->assertEquals($user->allSettingsFlat()->all(), $test3); } - - /** - * @test - */ - public function if_default_value_is_set_database_entry_is_deleted() - { - $user = $this->makeUser(); - - $this->actingAs($user); - - $settings = $user->settings($this->registered); - - $settings->set([ - 'test_settings1' => 'grapes' - ]); - - $this->seeInDatabase('user_property_bag', [ - 'user_id' => $user->resourceId(), - 'key' => 'test_settings1', - 'value' => json_encode('["grapes"]') - ]); - - $settings->set([ - 'test_settings1' => 'monkey' - ]); - - $this->dontSeeInDatabase('user_property_bag', [ - 'user_id' => $user->resourceId(), - 'key' => 'test_settings1', - 'value' => json_encode('["monkey"]') - ]); - } } diff --git a/tests/UnitTest.php b/tests/UnitTest.php new file mode 100644 index 0000000..bf22d8f --- /dev/null +++ b/tests/UnitTest.php @@ -0,0 +1,286 @@ +makeUser(); + + $settings = $user->settings(); + + $this->assertInstanceOf(UserSettings::class, $settings); + } + + /** + * @test + */ + public function settings_can_be_accessed_from_the_helper_function() + { + $user = $this->makeUser(); + + $this->actingAs($user); + + $settings = settings(); + + $this->assertInstanceOf(UserSettings::class, $settings); + } + + /** + * @test + */ + public function a_valid_setting_key_value_pair_passes_validation() + { + $user = $this->makeUser(); + + $this->actingAs($user); + + $settings = $user->settings($this->registered); + + $result = $settings->isValid('test_settings1', 'bananas'); + + $this->assertTrue($result); + } + + /** + * @test + */ + public function an_invalid_setting_key_fails_validation() + { + $user = $this->makeUser(); + + $this->actingAs($user); + + $settings = $user->settings($this->registered); + + $result = $settings->isValid('fake', true); + + $this->assertFalse($result); + } + + /** + * @test + */ + public function an_invalid_setting_value_fails_validation() + { + $user = $this->makeUser(); + + $this->actingAs($user); + + $settings = $user->settings($this->registered); + + $result = $settings->isValid('test_settings2', 'ok'); + + $this->assertFalse($result); + } + + /** + * @test + */ + public function adding_a_new_setting_creates_a_new_user_setting_record() + { + $user = $this->makeUser(); + + $this->actingAs($user); + + $settings = $user->settings($this->registered); + + $this->assertEmpty($settings->all()); + + $settings->set(['test_settings3' => true]); + + $this->assertEquals(['test_settings3' => true], $settings->all()); + + $this->seeInDatabase('user_property_bag', [ + 'user_id' => $user->resourceId(), + 'key' => 'test_settings3', + 'value' => json_encode('[true]') + ]); + } + + /** + * @test + */ + public function updating_a_setting_updates_the_setting() + { + $user = $this->makeUser(); + + $this->actingAs($user); + + $settings = $user->settings($this->registered); + + $settings->set(['test_settings2' => true]); + + $settings->set(['test_settings2' => false]); + + $this->assertEquals(['test_settings2' => false], $settings->all()); + + $this->seeInDatabase('user_property_bag', [ + 'user_id' => $user->resourceId(), + 'key' => 'test_settings2', + 'value' => json_encode('[false]') + ]); + } + + /** + * @test + */ + public function a_user_can_set_many_settings_at_once() + { + $user = $this->makeUser(); + + $this->actingAs($user); + + $settings = $user->settings($this->registered); + + $this->assertEmpty($settings->all()); + + $test = [ + 'test_settings1' => 'grapes', + 'test_settings2' => false, + ]; + + $settings->set($test); + + $this->assertEquals($test, $settings->all()); + + $this->seeInDatabase('user_property_bag', [ + 'user_id' => $user->resourceId(), + 'key' => 'test_settings1', + 'value' => json_encode('["grapes"]') + ]); + + $this->seeInDatabase('user_property_bag', [ + 'user_id' => $user->resourceId(), + 'key' => 'test_settings2', + 'value' => json_encode('[false]') + ]); + } + + /** + * @test + */ + public function a_user_can_get_a_setting() + { + $user = $this->makeUser(); + + $this->actingAs($user); + + $user->settings($this->registered)->set(['test_settings2' => true]); + + $result = $user->settings()->get('test_settings2'); + + $this->assertEquals(true, $result); + } + + /** + * @test + */ + public function a_user_can_get_a_setting_from_the_global_helper() + { + $user = $this->makeUser(); + + $this->actingAs($user); + + $user->settings($this->registered)->set(['test_settings2' => true]); + + $result = settings()->get('test_settings2'); + + $this->assertEquals(true, $result); + } + + /** + * @test + */ + public function if_the_setting_is_not_set_the_default_value_is_returned() + { + $user = $this->makeUser(); + + $this->actingAs($user); + + $result = $user->settings($this->registered)->get('test_settings1'); + + $this->assertEquals('monkey', $result); + } + + /** + * @test + */ + public function a_user_can_not_get_an_invalid_setting() + { + $user = $this->makeUser(); + + $this->actingAs($user); + + $result = $user->settings($this->registered)->get('invalid_setting'); + + $this->assertNull($result); + } + + /** + * @test + */ + public function settings_can_be_registered_on_settings_class() + { + $group = $this->makeGroup(); + + $settings = $group->settings(); + + $this->assertTrue($settings->isRegistered('test_settings1')); + } + + /** + * @test + */ + public function settings_intsance_is_persisted_on_resource_model() + { + $user = $this->makeUser(); + + $this->actingAs($user); + + $result = $user->settings($this->registered)->get('test_settings1'); + + $result = $user->settings()->get('test_settings1'); + + $this->assertEquals('monkey', $result); + } + + /** + * @test + */ + public function if_default_value_is_set_database_entry_is_deleted() + { + $user = $this->makeUser(); + + $this->actingAs($user); + + $settings = $user->settings($this->registered); + + $settings->set([ + 'test_settings1' => 'grapes' + ]); + + $this->seeInDatabase('user_property_bag', [ + 'user_id' => $user->resourceId(), + 'key' => 'test_settings1', + 'value' => json_encode('["grapes"]') + ]); + + $settings->set([ + 'test_settings1' => 'monkey' + ]); + + $this->dontSeeInDatabase('user_property_bag', [ + 'user_id' => $user->resourceId(), + 'key' => 'test_settings1', + 'value' => json_encode('["monkey"]') + ]); + } +}