Skip to content

Commit

Permalink
Update unit testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhichao-poper committed Nov 10, 2020
1 parent 569cf49 commit 811d8e7
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/ConditionsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private function convertOperator($operator): string
'le' => '<=',
];

return $operatorMap[$operator] ?? '=';
return $operatorMap[$operator] ?? $operator ?? '=';
}


Expand Down
8 changes: 4 additions & 4 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public function boot()
*/
public function register()
{
Builder::macro('advanced', function(Builder $builder, $conditions) {
return (new ConditionsBuilder($builder))->attach($conditions);
Builder::macro('advanced', function($conditions = []) {
return (new ConditionsBuilder($this))->attach($conditions);
});

Request::macro('conditions', function(Request $request, $wheres) {
return (new ConditionsGenerator($request, $wheres))->getRequestConditions();
Request::macro('conditions', function($wheres = []) {
return (new ConditionsGenerator($this, $wheres))->getRequestConditions();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public function up(): void
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('company_id')->nullable();
$table->unsignedInteger('team_id')->nullable();
$table->string('name')->nullable();
$table->string('email')->nullable();
$table->string('password')->nullable();
Expand Down
5 changes: 4 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests;

use Orchestra\Testbench\TestCase as BaseTestCase;
use Zhuzhichao\LaravelAdvancedSearch\ServiceProvider;

abstract class TestCase extends BaseTestCase
{
Expand All @@ -14,7 +15,9 @@ abstract class TestCase extends BaseTestCase
*/
protected function getPackageProviders($app)
{
return [];
return [
ServiceProvider::class
];
}

/**
Expand Down
44 changes: 44 additions & 0 deletions tests/Unit/ConditionsBuilder/SimpleLikeQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tests\Unit\ConditionsBuilder;

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;
use MatrixLab\LaravelAdvancedSearch\ModelScope;
use Tests\DBTestCase;
use Tests\Utils\Models\Company;
use Tests\Utils\Models\Post;
use Tests\Utils\Models\User;

class SimpleLikeQueryTest extends DBTestCase
{
protected function setUp(): void
{
parent::setUp();
Company::factory()->make([
'name' => 'this name is testing for search, first name',
])->save();
Company::factory()->make([
'name' => 'this name is testing for search, double first name',
])->save();
Company::factory()->make([
'name' => 'this name is testing for search, second name',
])->save();

User::factory()->make([

]);
}

public function test_without_scope()
{
self::assertEquals(2, Company::advanced(['wheres' => [
'name.like' => '%first%',
]])->count());
}

public function test_with_cope()
{

}
}
35 changes: 35 additions & 0 deletions tests/Unit/ConditionsBuilder/WhereQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Tests\Unit\ConditionsBuilder;

use Tests\DBTestCase;
use Tests\Utils\Models\Company;
use Tests\Utils\Models\User;

class WhereQueryTest extends DBTestCase
{
protected function setUp(): void
{
parent::setUp();

User::factory()->count(10)->create();
User::factory()->make([
'name' => 'zhuzhichao',
'email' => '[email protected]',
])->save();
}

public function test_model_without_search_keyword_method()
{
self::assertEquals(Company::count(), Company::advanced([
'keyword' => 'Chou'
])->count());
}

public function test_model_with_search_keyword_method()
{
self::assertEquals(1, User::advanced([
'keyword' => 'zhichao'
])->count());
}
}
9 changes: 9 additions & 0 deletions tests/Utils/Models/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace Tests\Utils\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Tests\Factories\CompanyFactory;

class Company extends Model
{
use HasFactory;

protected $guarded = [];

public function users(): HasMany
Expand All @@ -20,4 +24,9 @@ public function scopeName($query, $value)

return $query;
}

protected static function newFactory()
{
return new CompanyFactory;
}
}
9 changes: 9 additions & 0 deletions tests/Utils/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
namespace Tests\Utils\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tests\Factories\CompanyFactory;
use Tests\Factories\UserFactory;

class User extends Authenticatable
{
use SoftDeletes;
use HasFactory;

/**
* @var mixed[]
Expand Down Expand Up @@ -50,4 +54,9 @@ public function scopeSearchKeyword($q, $key)

return $q;
}

protected static function newFactory()
{
return new UserFactory;
}
}

0 comments on commit 811d8e7

Please sign in to comment.