Skip to content

Commit

Permalink
Refactor pass to argument and added more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
agungsugiarto committed Sep 19, 2020
1 parent 661b129 commit d3019da
Show file tree
Hide file tree
Showing 15 changed files with 273 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ https://example.com/news?orderBy=email_desc&begin=2019-01-24&end=2019-01-26
You can also build your own custom scopes. In your repository override `scope()` method:

```php
public function scope($request)
public function scope(IncomingRequest $request)
{
// apply build-in scopes
parent::scope($request);
Expand Down
4 changes: 3 additions & 1 deletion src/Contracts/CriterionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Fluent\Repository\Contracts;

use CodeIgniter\Model;

interface CriterionInterface
{
/**
Expand All @@ -10,5 +12,5 @@ interface CriterionInterface
* @param \CodeIgniter\Model $entity
* @return mixed
*/
public function apply($entity);
public function apply(Model $entity);
}
4 changes: 2 additions & 2 deletions src/Contracts/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function get(array $columns = ['*']);
* Execute the query and get the first result.
*
* @param array $columns
* @return array
* @return array|object
*/
public function first($columns = ['*']);

Expand Down Expand Up @@ -91,7 +91,7 @@ public function updateBatch(array $attributes, $id);
/**
* Delete a record by id.
*
* @param int $id
* @param mixed $id
* @return mixed
*/
public function destroy($id);
Expand Down
4 changes: 3 additions & 1 deletion src/Contracts/ScopesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Fluent\Repository\Contracts;

use CodeIgniter\HTTP\IncomingRequest;

interface ScopesInterface
{
/**
Expand All @@ -10,5 +12,5 @@ interface ScopesInterface
* @param \CodeIgniter\HTTP\IncomingRequest $request
* @return $this
*/
public function scope($request);
public function scope(IncomingRequest $request);
}
3 changes: 2 additions & 1 deletion src/Criteria/FindWhere.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fluent\Repository\Criteria;

use CodeIgniter\Model;
use Fluent\Repository\Contracts\CriterionInterface;

class FindWhere implements CriterionInterface
Expand All @@ -23,7 +24,7 @@ public function __construct(array $conditions)
/**
* @inheritdoc
*/
public function apply($entity)
public function apply(Model $entity)
{
foreach ($this->conditions as $field => $value) {
if (is_array($value)) {
Expand Down
3 changes: 2 additions & 1 deletion src/Eloquent/RepositoryAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fluent\Repository\Eloquent;

use CodeIgniter\HTTP\IncomingRequest;
use Fluent\Repository\Scopes\Scopes;
use Fluent\Repository\Contracts\ScopesInterface;
use Fluent\Repository\Contracts\CriteriaInterface;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function withCriteria(array $criteria)
/**
* @inheritdoc
*/
public function scope($request)
public function scope(IncomingRequest $request)
{
$this->entity = (new Scopes($request, $this->searchable))->scope($this->entity);

Expand Down
5 changes: 3 additions & 2 deletions src/Scopes/Scopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fluent\Repository\Scopes;

use CodeIgniter\HTTP\IncomingRequest;
use Fluent\Repository\Scopes\Clauses\WhereScope;
use Fluent\Repository\Scopes\Clauses\OrderByScope;
use Fluent\Repository\Scopes\Clauses\OrWhereScope;
Expand All @@ -26,7 +27,7 @@ class Scopes extends ScopesAbstract
* @param array $searchable
* @return void
*/
public function __construct($request, $searchable)
public function __construct(IncomingRequest $request, $searchable)
{
parent::__construct($request);

Expand All @@ -45,7 +46,7 @@ public function __construct($request, $searchable)
* @param string $key
* @return string
*/
protected function mappings($key)
protected function mappings(string $key)
{
$mappings = [
'or' => OrWhereScope::class,
Expand Down
14 changes: 8 additions & 6 deletions src/Scopes/ScopesAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Fluent\Repository\Scopes;

use CodeIgniter\HTTP\IncomingRequest;

class ScopesAbstract
{
/** @var \CodeIgniter\HTTP\IncomingRequest $request */
/** @var \CodeIgniter\HTTP\IncomingRequest */
protected $request;

/** @var array $scopes */
Expand All @@ -15,15 +17,15 @@ class ScopesAbstract
*
* @param \CodeIgniter\HTTP\IncomingRequest $request
*/
public function __construct($request)
public function __construct(IncomingRequest $request)
{
$this->request = $request;
}

/**
* In your repository define which fields can be used to scope your queries.
*
* @param \CodeIgniter\Database\BaseBuilder $builder
* @param \CodeIgniter\Database\BaseBuilder|\CodeIgniter\Model $builder
* @return \CodeIgniter\Database\BaseBuilder $builder
*/
public function scope($builder)
Expand All @@ -43,7 +45,7 @@ public function scope($builder)
* @param string $scope
* @return object
*/
protected function resolveScope($scope)
protected function resolveScope(string $scope)
{
return new $this->scopes[$scope]();
}
Expand All @@ -56,7 +58,7 @@ protected function resolveScope($scope)
protected function getScopes()
{
return $this->filterScopes(
$this->request->getVar(array_keys($this->scopes))
$this->request->getGet(array_keys($this->scopes))
);
}

Expand All @@ -66,7 +68,7 @@ protected function getScopes()
* @param array $scopes
* @return array
*/
protected function filterScopes($scopes)
protected function filterScopes(array $scopes)
{
return array_filter($scopes, function ($scope) {
return isset($scope);
Expand Down
41 changes: 41 additions & 0 deletions tests/Criteria/SampleCriteria.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Fluent\Repository\Tests\Criteria;

use CodeIgniter\Model;
use Fluent\Repository\Contracts\CriterionInterface;

class SampleCriteria implements CriterionInterface
{
/** @var array $conditions */
protected $conditions;

/**
* Constructor FindWhare.
*
* @param array $conditions
* @return void
*/
public function __construct(array $conditions)
{
$this->conditions = $conditions;
}

/**
* @inheritdoc
*/
public function apply(Model $entity)
{
foreach ($this->conditions as $field => $value)
{
if (is_array($value)) {
list($field, $condition, $val) = $value;
$entity = $entity->orWhere($field . $condition, $val);
} else {
$entity = $entity->where($field, $value);
}

return $entity;
}
}
}
2 changes: 2 additions & 0 deletions tests/Models/NewsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class NewsModel extends Model
protected $allowedFields = ['title', 'description'];
protected $useTimestamps = true;

public $orderable = ['title'];

/**
* Faker generator.
*
Expand Down
133 changes: 133 additions & 0 deletions tests/NewsRepositoryRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace Fluent\Repository\Tests;

use CodeIgniter\Config\Services;
use CodeIgniter\I18n\Time;
use CodeIgniter\Test\CIDatabaseTestCase;
use CodeIgniter\Test\ControllerTester;
use Fluent\Repository\Tests\Repository\NewsRepositoryScope;
use Tests\Support\Database\Seeds\NewsSeeder;

class NewsRepositoryRequestTest extends CIDatabaseTestCase
{
protected $refresh = false;

protected $seed = NewsSeeder::class;

/** @var NewsRepositoryScope */
protected $repository;

/** @var \CodeIgniter\HTTP\IncomingRequest */
protected $request;

protected function setUp(): void
{
parent::setUp();

/** @var NewsRepositoryScope */
$this->repository = new NewsRepositoryScope();

$this->request = Services::request();
}

public function testRepositoryMyCustomScope()
{
$this->request->setMethod('get')
->setGlobal('get', [
'id' => '1',
]
);

$this->assertNotEmpty($this->repository->scope(Services::request())->first());
}

public function testRepositoryMyCustomScopeIsNull()
{
$this->request->setMethod('get')
->setGlobal('get', [
'id' => '1000',
]
);

$this->assertNull($this->repository->scope(Services::request())->first());
}

public function testRepositoryScopeTitle()
{
$this->request->setMethod('get')
->setGlobal('get', [
'title' => 'A',
]
);

$this->assertNotEmpty($this->repository->scope(Services::request())->first());
}

public function testRepositoryScopeTitleIsNull()
{
$this->request->setMethod('get')
->setGlobal('get', [
'title' => 'Aaaaaa',
]
);

$this->assertNull($this->repository->scope(Services::request())->first());
}

public function testRepositoryScopeDescription()
{
$this->request->setMethod('get')
->setGlobal('get', [
'description' => 'A',
]
);

$this->assertNotEmpty($this->repository->scope(Services::request())->first());
}

public function testRepositoryScopeDescriptionIsNull()
{
$this->request->setMethod('get')
->setGlobal('get', [
'description' => 'Aaaaaa',
]
);

$this->assertNull($this->repository->scope(Services::request())->first());
}

public function testRepositoryScopeRequestOrderByAsc()
{
$this->request->setMethod('get')
->setGlobal('get', [
'orderBy' => 'title_asc',
]
);

$this->assertNotEmpty($this->repository->scope(Services::request())->paginate());
}

public function testRepositoryScopeRequestOrderByDesc()
{
$this->request->setMethod('get')
->setGlobal('get', [
'orderBy' => 'title_desc',
]
);

$this->assertNotEmpty($this->repository->scope(Services::request())->paginate());
}

public function testRepositoryScopeRequestBeginEnd()
{
$this->request->setMethod('get')
->setGlobal('get', [
'begin' => Time::now(),
'end' => Time::now(),
]
);

$this->assertNotNull($this->repository->scope(Services::request())->paginate());
}
}
14 changes: 14 additions & 0 deletions tests/NewsRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Fluent\Repository\Tests;

use CodeIgniter\Test\CIDatabaseTestCase;
use Fluent\Repository\Tests\Criteria\SampleCriteria;
use Fluent\Repository\Tests\Repository\NewsRepository;
use Tests\Support\Database\Seeds\NewsSeeder;

Expand Down Expand Up @@ -44,6 +45,19 @@ public function testRepositoryFindWhere()
);
}

public function testRepositoryWithCriteria()
{
$this->assertNotEmpty(
$this->repository->withCriteria([
new SampleCriteria([
'id' => 1,
['id', '=', 2]
]),
])
->get()
);
}

public function testRepositoryPaginate()
{
$this->assertNotEmpty($this->repository->paginate());
Expand Down
Loading

0 comments on commit d3019da

Please sign in to comment.