-
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add whereLike clause to query builder (#874)
- Loading branch information
Showing
4 changed files
with
193 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yajra\Oci8\Tests\Database; | ||
|
||
use Illuminate\Foundation\Testing\DatabaseMigrations; | ||
use Orchestra\Testbench\TestCase; | ||
|
||
abstract class DatabaseTestCase extends TestCase | ||
{ | ||
use DatabaseMigrations; | ||
|
||
/** | ||
* The current database driver. | ||
* | ||
* @return string | ||
*/ | ||
protected string $driver = 'oracle'; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->beforeApplicationDestroyed(function () { | ||
foreach (array_keys($this->app['db']->getConnections()) as $name) { | ||
$this->app['db']->purge($name); | ||
} | ||
}); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
protected function defineEnvironment($app): void | ||
{ | ||
$connection = $app['config']->get('database.default'); | ||
|
||
$this->driver = $app['config']->get("database.connections.$connection.driver"); | ||
} | ||
} |
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,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yajra\Oci8\Tests\Database; | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class QueryBuilderWhereLikeTest extends DatabaseTestCase | ||
{ | ||
public function testWhereLike() | ||
{ | ||
$users = DB::table('users')->whereLike('email', 'john.doe@example.com')->get(); | ||
$this->assertCount(1, $users); | ||
$this->assertSame('John.Doe@example.com', $users[0]->email); | ||
|
||
$this->assertSame(4, DB::table('users')->whereNotLike('email', 'john.doe@example.com')->count()); | ||
} | ||
|
||
public function testWhereLikeWithPercentWildcard() | ||
{ | ||
$this->assertSame(5, DB::table('users')->whereLike('email', '%@example.com')->count()); | ||
$this->assertSame(2, DB::table('users')->whereNotLike('email', '%Doe%')->count()); | ||
|
||
$users = DB::table('users')->whereLike('email', 'john%')->get(); | ||
$this->assertCount(1, $users); | ||
$this->assertSame('John.Doe@example.com', $users[0]->email); | ||
} | ||
|
||
public function testWhereLikeWithUnderscoreWildcard() | ||
{ | ||
$users = DB::table('users')->whereLike('email', '_a_e_%@example.com')->get(); | ||
$this->assertCount(2, $users); | ||
$this->assertSame('janedoe@example.com', $users[0]->email); | ||
$this->assertSame('Dale.Doe@example.com', $users[1]->email); | ||
} | ||
|
||
public function testWhereLikeCaseSensitive() | ||
{ | ||
$users = DB::table('users')->whereLike('email', 'john.doe@example.com', true)->get(); | ||
$this->assertCount(0, $users); | ||
|
||
$users = DB::table('users')->whereLike('email', 'tim.smith@example.com', true)->get(); | ||
$this->assertCount(1, $users); | ||
$this->assertSame('tim.smith@example.com', $users[0]->email); | ||
$this->assertSame(5, DB::table('users')->whereNotLike('email', 'john.doe@example.com', true)->count()); | ||
} | ||
|
||
public function testWhereLikeWithPercentWildcardCaseSensitive() | ||
{ | ||
$this->assertSame(2, DB::table('users')->whereLike('email', '%Doe@example.com', true)->count()); | ||
$this->assertSame(4, DB::table('users')->whereNotLike('email', '%smith%', true)->count()); | ||
|
||
$users = DB::table('users')->whereLike('email', '%Doe@example.com', true)->get(); | ||
$this->assertCount(2, $users); | ||
$this->assertSame('John.Doe@example.com', $users[0]->email); | ||
$this->assertSame('Dale.Doe@example.com', $users[1]->email); | ||
} | ||
|
||
public function testWhereLikeWithUnderscoreWildcardCaseSensitive() | ||
{ | ||
$users = DB::table('users')->whereLike('email', 'j__edoe@example.com', true)->get(); | ||
$this->assertCount(1, $users); | ||
$this->assertSame('janedoe@example.com', $users[0]->email); | ||
|
||
$users = DB::table('users')->whereNotLike('email', '%_oe@example.com', true)->get(); | ||
$this->assertCount(2, $users); | ||
$this->assertSame('Earl.Smith@example.com', $users[0]->email); | ||
$this->assertSame('tim.smith@example.com', $users[1]->email); | ||
} | ||
|
||
protected function afterRefreshingDatabase(): void | ||
{ | ||
Schema::create('users', function (Blueprint $table) { | ||
$table->id('id'); | ||
$table->string('name', 200); | ||
$table->text('email'); | ||
}); | ||
} | ||
|
||
protected function destroyDatabaseMigrations(): void | ||
{ | ||
Schema::drop('users'); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
DB::table('users')->insert([ | ||
['name' => 'John Doe', 'email' => 'John.Doe@example.com'], | ||
['name' => 'Jane Doe', 'email' => 'janedoe@example.com'], | ||
['name' => 'Dale doe', 'email' => 'Dale.Doe@example.com'], | ||
['name' => 'Earl Smith', 'email' => 'Earl.Smith@example.com'], | ||
['name' => 'tim smith', 'email' => 'tim.smith@example.com'], | ||
]); | ||
} | ||
} |