diff --git a/app/Services/Repositories/OptionRepository.php b/app/Services/Repositories/OptionRepository.php index 116cacbb..2ebb4913 100644 --- a/app/Services/Repositories/OptionRepository.php +++ b/app/Services/Repositories/OptionRepository.php @@ -135,22 +135,6 @@ class OptionRepository extends Repository } } - /** - * Prepend a value onto an array option value. - * - * @param string $key - * @param mixed $value - * @return void - */ - public function prepend($key, $value) - { - $array = $this->get($key); - - array_unshift($array, $value); - - $this->set($key, $array); - } - /** * Return the options with key in the given array. * diff --git a/app/Services/Repositories/Repository.php b/app/Services/Repositories/Repository.php index ff9d2531..14a23343 100644 --- a/app/Services/Repositories/Repository.php +++ b/app/Services/Repositories/Repository.php @@ -12,7 +12,7 @@ class Repository implements ArrayAccess // Illuminate\Contracts\Cache\Repository * * @var array */ - protected $items; + protected $items = []; /** * All of the option items that is modified. @@ -89,11 +89,11 @@ class Repository implements ArrayAccess // Illuminate\Contracts\Cache\Repository /** * Get an item from the repository, or store the default value. * - * @param string $key - * @param \Closure $callback + * @param string $key + * @param callable $callback * @return mixed */ - public function remember($key, Closure $callback) + public function remember($key, callable $callback) { // If the item exists in the repository we will just return this immediately // otherwise we will execute the given Closure and repository the result @@ -102,7 +102,7 @@ class Repository implements ArrayAccess // Illuminate\Contracts\Cache\Repository return $value; } - $this->put($key, $value = $callback()); + $this->set($key, $value = $callback()); return $value; } @@ -110,8 +110,8 @@ class Repository implements ArrayAccess // Illuminate\Contracts\Cache\Repository /** * Remove an item from the repository. * - * @param string $key - * @return bool + * @param string|array $key + * @return void */ public function forget($key) { diff --git a/tests/ServicesTest/RepositoriesTest/OptionRepositoryTest.php b/tests/ServicesTest/RepositoriesTest/OptionRepositoryTest.php new file mode 100644 index 00000000..f0e0afff --- /dev/null +++ b/tests/ServicesTest/RepositoriesTest/OptionRepositoryTest.php @@ -0,0 +1,43 @@ +set('k1', '(null)'); + $this->assertNull($repo->get('k1')); + } + + public function testSet() + { + $repo = new OptionRepository(); + $repo->set([ + 'k1' => 'v1', + 'k2' => 'v2' + ]); + $this->assertEquals('v1', $repo->get('k1')); + $this->assertEquals('v2', $repo->get('k2')); + } + + public function testOnly() + { + $repo = new OptionRepository(); + $repo->set([ + 'k1' => 'v1', + 'k2' => 'v2', + 'k3' => 'v3', + ]); + $this->assertArraySubset([ + 'k1' => 'v1', + 'k2' => 'v2' + ], $repo->only(['k1', 'k2'])); + } +} diff --git a/tests/ServicesTest/RepositoriesTest/RepositoryTest.php b/tests/ServicesTest/RepositoriesTest/RepositoryTest.php new file mode 100644 index 00000000..34b8c909 --- /dev/null +++ b/tests/ServicesTest/RepositoriesTest/RepositoryTest.php @@ -0,0 +1,127 @@ +push('a'); + $this->assertTrue($repo->has(0)); + $this->assertFalse($repo->has(1)); + } + + public function testGet() + { + $repo = new Repository(); + $repo->push('a'); + $this->assertEquals('a', $repo->get(0)); + $this->assertNull($repo->get(1)); + $this->assertEquals('b', $repo->get(1, 'b')); + } + + public function testSet() + { + $repo = new Repository(); + $repo->set('k1', 'v1'); + $this->assertEquals('v1', $repo->get('k1')); + + $repo->set([ + 'k2' => 'v2', + 'k3' => 'v3' + ]); + $this->assertEquals('v2', $repo->get('k2')); + $this->assertEquals('v3', $repo->get('k3')); + } + + public function testPush() + { + $repo = new Repository(); + $repo->push('a'); + $this->assertEquals('a', $repo->get(0)); + } + + public function testAll() + { + $repo = new Repository(); + $repo->set('k1', 'v1'); + $repo->set([ + 'k2' => 'v2', + 'k3' => 'v3' + ]); + $repo->push('a'); + $this->assertArraySubset([ + 'k1' => 'v1', + 'k2' => 'v2', + 'k3' => 'v3', + 0 => 'a' + ], $repo->all()); + } + + public function testRemember() + { + $repo = new Repository(); + $repo->set('k1', 'v1'); + $this->assertEquals( + 'v1', + $repo->remember('k1', function () {}) + ); + + $this->assertEquals( + 'v2', + $repo->remember('k2', function () { + return 'v2'; + }) + ); + } + + public function testForget() + { + $repo = new Repository(); + $repo->set('k1', 'v1'); + $repo->forget('k1'); + $this->assertFalse($repo->has('k1')); + + $repo->set([ + 'k2' => 'v2', + 'k3' => 'v3' + ]); + $repo->forget(['k2', 'k3']); + $this->assertFalse($repo->has('k2')); + $this->assertFalse($repo->has('k3')); + } + + public function testOffsetExists() + { + $repo = new Repository(); + $repo->set('k1', 'v1'); + $this->assertTrue($repo->offsetExists('k1')); + } + + public function testOffsetGet() + { + $repo = new Repository(); + $repo->push('a'); + $this->assertEquals('a', $repo->offsetGet(0)); + $this->assertNull($repo->get(1)); + } + + public function testOffsetSet() + { + $repo = new Repository(); + $repo->offsetSet('k1', 'v1'); + $this->assertEquals('v1', $repo->get('k1')); + } + + public function testOffsetUnset() + { + $repo = new Repository(); + $repo->set('k1', 'v1'); + $repo->offsetUnset('k1'); + $this->assertFalse($repo->has('k1')); + } +} diff --git a/tests/ServicesTest/RepositoriesTest/UserRepositoryTest.php b/tests/ServicesTest/RepositoriesTest/UserRepositoryTest.php new file mode 100644 index 00000000..32e619c0 --- /dev/null +++ b/tests/ServicesTest/RepositoriesTest/UserRepositoryTest.php @@ -0,0 +1,24 @@ +assertFalse($repo->has('not_found', 'invalid')); + } + + public function testGet() + { + $repo = new UserRepository(); + $this->assertNull($repo->get('not_found', 'username')); + } +}