Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
athphane committed Feb 8, 2024
2 parents aec13f3 + 8eb5e85 commit 0bf32fd
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 6 deletions.
17 changes: 11 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
<testdoxText outputFile="build/coverage.txt"/>
</logging>
<php>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_USERNAME" value="root"/>
<env name="DB_PASSWORD" value=""/>
<env name="DB_DATABASE" value="generators"/>
<env name="DB_HOST" value="127.0.0.1"/>
<env name="DB_PORT" value="3307"/>
<env name="APP_ENV" value="testing"/>
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_STORE" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="array"/>
<env name="PULSE_ENABLED" value="false"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
</php>
</phpunit>
11 changes: 11 additions & 0 deletions src/AdminModel/AdminModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Javaabu\Helpers\AdminModel;

use Illuminate\Database\Eloquent\Builder;

interface AdminModel
{
/**
Expand Down Expand Up @@ -53,4 +55,13 @@ public function getLogUrlAttribute(): string;
* @return string
*/
public function getCauserLogUrlAttribute(): string;

/**
* A search scope
*
* @param Builder $query
* @param $search
* @return mixed
*/
public function scopeSearch($query, $search): mixed;
}
33 changes: 33 additions & 0 deletions src/AdminModel/IsAdminModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace Javaabu\Helpers\AdminModel;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;

trait IsAdminModel
{
/**
Expand Down Expand Up @@ -81,4 +84,34 @@ public function getCauserLogUrlAttribute(): string
'causer_id' => $this->id,
], route('admin.logs.index'));
}

/**
* A search scope
*
* @param Builder $query
* @param $search
* @return mixed
*/
public function scopeSearch($query, $search): mixed
{
if (property_exists($this, 'searchable') && $this->searchable) {
$searchable = Arr::wrap($this->searchable);

$first = true;
foreach ($searchable as $attribute) {
if ($first) {
$first = false;
$method = 'where';
} else {
$method = 'orWhere';
}

$query->{$method}($attribute, 'like', '%'.$search.'%');
}

return $query;
}

return $query->where($this->getKeyName(), $search);
}
}
155 changes: 155 additions & 0 deletions tests/Feature/AdminModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace Javaabu\Helpers\Tests\Feature;

use Illuminate\Database\Eloquent\Model;
use Javaabu\Helpers\AdminModel\AdminModel;
use Javaabu\Helpers\AdminModel\IsAdminModel;
use Javaabu\Helpers\Tests\TestCase;
use Javaabu\Helpers\Tests\InteractsWithDatabase;

class CategoryWithSearchable extends Model implements AdminModel
{
use IsAdminModel;

protected $table = 'categories';

protected array $searchable = [
'name'
];

protected $guarded = [
];

public function getAdminUrlAttribute(): string
{
return '';
}
}

class CategoryWithStringSearchable extends Model implements AdminModel
{
use IsAdminModel;

protected $table = 'categories';

protected string $searchable = 'name';

protected $guarded = [
];

public function getAdminUrlAttribute(): string
{
return '';
}
}

class CategoryWithMultipleSearchable extends Model implements AdminModel
{
use IsAdminModel;

protected $table = 'categories';

protected array $searchable = [
'slug',
'name'
];

protected $guarded = [
];

public function getAdminUrlAttribute(): string
{
return '';
}
}

class CategoryWithoutSearchable extends Model implements AdminModel
{
use IsAdminModel;

protected $table = 'categories';

protected $guarded = [
];

public function getAdminUrlAttribute(): string
{
return '';
}
}

class AdminModelTest extends TestCase
{
use InteractsWithDatabase;

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

$this->runMigrations();
}

/** @test */
public function it_can_search_models_using_a_partial_match(): void
{
$category = new CategoryWithSearchable(['name' => 'Apple', 'slug' => 'some-slug']);
$category->save();

$found = CategoryWithSearchable::search('pple')->first();

$this->assertEquals($category->id, $found->id);
}

/** @test */
public function it_can_search_models_using_a_single_searchable(): void
{
$category = new CategoryWithSearchable(['name' => 'Apple', 'slug' => 'some-slug']);
$category->save();

$found = CategoryWithSearchable::search('Apple')->first();

$this->assertEquals($category->id, $found->id);
$this->assertNull(CategoryWithSearchable::search('some-slug')->first());
}

/** @test */
public function it_can_search_models_using_a_string_searchable(): void
{
$category = new CategoryWithStringSearchable(['name' => 'Apple', 'slug' => 'some-slug']);
$category->save();

$found = CategoryWithStringSearchable::search('Apple')->first();

$this->assertEquals($category->id, $found->id);
$this->assertNull(CategoryWithStringSearchable::search('some-slug')->first());
}

/** @test */
public function it_can_search_models_using_multiple_searchables(): void
{
$category = new CategoryWithMultipleSearchable(['name' => 'Apple', 'slug' => 'some-slug']);
$category->save();

$found = CategoryWithMultipleSearchable::search('Apple')->first();

$this->assertEquals($category->id, $found->id);

$found = CategoryWithMultipleSearchable::search('some-slug')->first();

$this->assertEquals($category->id, $found->id);
}

/** @test */
public function it_can_search_models_without_searchable(): void
{
$category = new CategoryWithoutSearchable(['name' => 'Apple', 'slug' => 'some-slug']);
$category->save();

$found = CategoryWithoutSearchable::search($category->id)->first();

$this->assertEquals($category->id, $found->id);
$this->assertNull(CategoryWithoutSearchable::search('Apple')->first());
$this->assertNull(CategoryWithoutSearchable::search('some-slug')->first());
}
}
13 changes: 13 additions & 0 deletions tests/InteractsWithDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Javaabu\Helpers\Tests;

trait InteractsWithDatabase
{
protected function runMigrations()
{
include_once __DIR__ . '/database/create_categories_table.php';

(new \CreateCategoriesTable)->up();
}
}
32 changes: 32 additions & 0 deletions tests/database/create_categories_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}

0 comments on commit 0bf32fd

Please sign in to comment.