test(services): add tests for repositories

This commit is contained in:
Pig Fang 2017-12-28 12:52:29 +08:00
parent 776a0a67ae
commit 55a137f014
5 changed files with 201 additions and 23 deletions

View File

@ -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.
*

View File

@ -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)
{

View File

@ -0,0 +1,43 @@
<?php
use App\Services\Repositories\OptionRepository;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class OptionRepositoryTest extends TestCase
{
use DatabaseTransactions;
public function testGet()
{
$repo = new OptionRepository();
$repo->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']));
}
}

View File

@ -0,0 +1,127 @@
<?php
use App\Services\Repositories\Repository;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class RepositoryTest extends TestCase
{
public function testHas()
{
$repo = new Repository();
$repo->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'));
}
}

View File

@ -0,0 +1,24 @@
<?php
use App\Models\User;
use App\Services\Repositories\UserRepository;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class UserRepositoryTest extends TestCase
{
use DatabaseTransactions;
public function testHas()
{
$repo = new UserRepository();
$this->assertFalse($repo->has('not_found', 'invalid'));
}
public function testGet()
{
$repo = new UserRepository();
$this->assertNull($repo->get('not_found', 'username'));
}
}