-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
6 changed files
with
255 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |