Skip to content

Commit

Permalink
add test of refact
Browse files Browse the repository at this point in the history
  • Loading branch information
wheesnoza committed Oct 2, 2020
1 parent 716cfbd commit d0730d1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/ModelSearchAspectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,25 @@ public function it_throws_an_exception_if_there_are_no_searchable_attributes()

$searchAspect->getResults('john');
}

/** @test */
public function it_can_build_an_eloquent_query_by_many_same_methods()
{
TestModel::createWithNameAndLastNameAndGenderAndStatus('Taylor', 'Otwell', 'woman', true);

$searchAspect = ModelSearchAspect::forModel(TestModel::class)
->addSearchableAttribute('name', true)
->where('gender', 'woman')
->where('status', 'activated');

DB::enableQueryLog();

$searchAspect->getResults('taylor');

$expectedQuery = 'select * from "test_models" where "gender" = ? and "status" = ? and (LOWER(`name`) LIKE ?)';

$executedQuery = Arr::get(DB::getQueryLog(), '0.query');

$this->assertEquals($expectedQuery, $executedQuery);
}
}
10 changes: 10 additions & 0 deletions tests/Models/TestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public static function createWithNameAndLastName(string $name, $lastName): self
]);
}

public static function createWithNameAndLastNameAndGenderAndStatus(string $name, string $lastName, string $gender, bool $active): self
{
return static::create([
'name' => $name,
'last_name' => $lastName,
'gender' => $gender,
'active' => $active,
]);
}

public function getSearchResult(): SearchResult
{
return new SearchResult($this, $this->name);
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ protected function setUpDatabase(Application $app)
$table->string('name');
$table->string('last_name')->nullable();
$table->boolean('active')->default(false);
$table->string('gender')->nullable();
});

Schema::create('test_comments', function (Blueprint $table) {
Expand Down

0 comments on commit d0730d1

Please sign in to comment.