Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added joins functionality #9

Merged
merged 2 commits into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"illuminate/http": "5.5.*|5.6.*"
},
"require-dev": {
"phpunit/phpunit": "~6.0|~7.0",
"orchestra/testbench": "3.5.*|3.6.*"
"orchestra/testbench": "^3.6",
"phpunit/phpunit": "~6.0|~7.0"
},
"autoload": {
"psr-4": {
Expand Down
72 changes: 71 additions & 1 deletion src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ abstract class Filter implements FilterContract
*/
protected $filterMap;

/**
* @var array
*/
protected $joins = [];
/**
* @return Filter
*/


public function __construct(Request $request)
{
Expand Down Expand Up @@ -76,6 +84,68 @@ protected function builderPresent()
}
}

/**
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param String $join
* @param String $key
* @param String $joinKey
* @param String $joinType
*
* @return Filter
*/
public function addJoin($join,$key,$joinKey,$joinType = null)
{
if(in_array($join, $this->joins)){
return $this;
}
array_push($this->joins,$join);
if($joinType == "left"){
$this->builder->leftJoin($join,$key,$joinKey);
}else if($joinType == "right"){
$this->builder->rightJoin($join,$key,$joinKey);
}else{
$this->builder->join($join,$key,$joinKey);
}
return $this;
}

/**
* @param String $join
* @param String $key
* @param String $joinKey
* @param String $joinType
*
* @return Builder
*/
public function setJoin($join,$key,$joinKey,$joinType = null)
{
$this->addJoin($join,$key,$joinKey,$joinType);
return $this->builder;
}

/**
* @param String $join
* @param String $key
* @param String $joinKey
*
* @return Builder
*/
public function setLeftJoin($join,$key,$joinKey)
{
return $this->setJoin($join,$key,$joinKey,"left");
}

/**
* @param String $join
* @param String $key
* @param String $joinKey
*
* @return Builder
*/
public function setRightJoin($join,$key,$joinKey)
{
return $this->setJoin($join,$key,$joinKey,"right");
}

/**
* @return $this
Expand Down Expand Up @@ -115,4 +185,4 @@ private function filters(): array

return $filters ?? [];
}
}
}
85 changes: 85 additions & 0 deletions tests/JoinTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Kyslik\LaravelFilterable\Test;

use Carbon\Carbon;
use Kyslik\LaravelFilterable\Exceptions\InvalidArgumentException;
use Kyslik\LaravelFilterable\Generic\Templater;
use Orchestra\Testbench\TestCase as Orchestra;

class JoinTest extends TestCase
{

function test_single_join()
{
$this->assertJoinQuery('select * inner join "user" on "role"."user_id" = "user"."id" where "user"."username" = \'name\'',
['name' => 'name']);
}

function test_join_and_other_filter(){
$this->assertJoinQuery('select * inner join "user" on "role"."user_id" = "user"."id" where "user"."username" = \'name\' '.
'and "user"."email" = \'email@example.com\'',
['name' => 'name', 'email' => "email@example.com"]);

}

function test_left_right_joins()
{
$this->assertJoinQuery('select * left join "user" on "role"."user_id" = "user"."id" where "user"."username" = \'name\'',
['left' => 'name']);
$this->assertJoinQuery('select * right join "user" on "role"."user_id" = "user"."id" where "user"."username" = \'name\'',
['right' => 'name']);
}

function test_multiple_joins(){
$this->assertJoinQuery('select * '.
'inner join "user" on "role"."user_id" = "user"."id" '.
'inner join "permission" on "role"."id" = "permission"."role_id" '.
'where "user"."username" = \'name\' '.
'and "permission"."level" = \'6\'',
[ 'permission' => '6', "name" => "name"]);
}

function test_join_through_tables(){
$this->assertJoinQuery('select * '.
'inner join "permission" on "role"."id" = "permission"."role_id" '.
'inner join "permissiontype" on "permission"."id" = "permissiontype"."permission_id" '.
'where "permissiontype"."type" = \'admin\'',
[ "permissiontype" => "admin"]);
}

function test_join_though_tables_and_other_filter(){
$this->assertJoinQuery('select * '.
'inner join "permission" on "role"."id" = "permission"."role_id" '.
'inner join "permissiontype" on "permission"."id" = "permissiontype"."permission_id" '.
'where "permissiontype"."type" = \'admin\' '.
'and "permissiontype"."active" = \'1\'' ,
[ "permissiontype" => "admin",'permissiontype_active' => 1]);
}

function test_join_though_multiple_tables_and_other_filter(){
$this->assertJoinQuery('select * '.
'inner join "user" on "role"."user_id" = "user"."id" '.
'inner join "permission" on "role"."id" = "permission"."role_id" '.
'inner join "permissiontype" on "permission"."id" = "permissiontype"."permission_id" '.
'where "user"."username" = \'name\' '.
'and "permissiontype"."type" = \'admin\' '.
'and "permissiontype"."active" = \'1\'' ,
["name" => "name", "permissiontype" => "admin",'permissiontype_active' => 1]);
}

private function assertJoinQuery($expectedQuery, $builderQuery)
{
$filter = $this->buildCustomFilter(http_build_query($builderQuery));
$this->assertEquals($expectedQuery,$this->dumpQuery($filter->apply($this->builder)));
$this->resetBuilder();
}


private function assertQuery($expectedQuery, $params)
{
$filter = $this->buildFilter(http_build_query($params));
$this->assertEquals($expectedQuery, $this->dumpQuery($filter->apply($this->builder)));
$this->resetBuilder();
}
}
27 changes: 27 additions & 0 deletions tests/Stubs/Permission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Kyslik\LaravelFilterable\Test\Stubs;

use Kyslik\LaravelFilterable\Filter;

class PermissionFilter extends Filter
{

protected $filterables = [
'id',
'role_id',
'level',
'created_at',
'updated_at',
'deleted_at',
'active',
'published',
];

function filterMap(): array
{
return [
];
}

}
27 changes: 27 additions & 0 deletions tests/Stubs/PermissionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Kyslik\LaravelFilterable\Test\Stubs;

use Kyslik\LaravelFilterable\Filter;

class PermissionFilter extends Filter
{

protected $filterables = [
'id',
'permission_id',
'type',
'created_at',
'updated_at',
'deleted_at',
'active',
'published',
];

function filterMap(): array
{
return [
];
}

}
67 changes: 67 additions & 0 deletions tests/Stubs/RoleFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Kyslik\LaravelFilterable\Test\Stubs;

use Kyslik\LaravelFilterable\Filter;

class RoleFilter extends Filter
{

protected $filterables = [
'id',
'user_id',
'role',
'created_at',
'updated_at',
'deleted_at',
'active',
'published',
];

function filterMap(): array
{
return [
'name' => ['name'],
'email' => ['email'],
'name_left' => ['left'],
'name_right' => ['right'],
'permission' => ['permission'],
'permissiontype' => ['permissiontype'],
'permissiontype_active' => ['permissiontype_active'],

];
}

public function name($username){
return $this->setJoin("user","role.user_id","user.id")->where("user.username",$username);
}

public function email($email){
return $this->setJoin("user","role.user_id","user.id")->where("user.email",$email);
}

public function name_left($username){
return $this->setLeftJoin("user","role.user_id","user.id")->where("user.username",$username);
}

public function name_right($username){
return $this->setRightJoin("user","role.user_id","user.id")->where("user.username",$username);
}

public function permission($level){
return $this->setJoin("permission","role.id","permission.role_id")->where("permission.level",$level);
}

public function permissiontype($permission_type){
return $this->addJoin("permission","role.id","permission.role_id")
->setJoin("permissiontype","permission.id","permissiontype.permission_id")
->where("permissiontype.type",$permission_type);
}

public function permissiontype_active($permission_type_active){
return $this->addJoin("permission","role.id","permission.role_id")
->setJoin("permissiontype","permission.id","permissiontype.permission_id")
->where("permissiontype.active",$permission_type_active);
}

}
4 changes: 4 additions & 0 deletions tests/Stubs/UserFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public function getGroupingOperator()
{
return $this->groupingOperator;
}

// public function role($role){
// return $this->setJoin("roles","user.id","roles.user_id")->where("roles.role",$role);
// }
}
11 changes: 10 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Kyslik\LaravelFilterable\FilterableServiceProvider;
use Kyslik\LaravelFilterable\Generic\Templater;
use Kyslik\LaravelFilterable\Test\Stubs\UserFilter;
use Kyslik\LaravelFilterable\Test\Stubs\RoleFilter;
use Orchestra\Testbench\TestCase as Orchestra;

abstract class TestCase extends Orchestra
Expand Down Expand Up @@ -54,8 +55,16 @@ protected function buildFilter($requestQuery)
}


protected function buildCustomFilter($requestQuery)
{
/** @var Request $request */
$request = resolve(Request::class)->create('http://test.dev?'.$requestQuery);
return new RoleFilter($request);
}


protected function dumpQuery(Builder $builder)
{
return vsprintf(str_replace(['?'], ['\'%s\''], $builder->toSql()), $builder->getBindings());
}
}
}