-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add route key trait to use Optimus in models (#6)
* Add trait for implicit route model binding
- Loading branch information
1 parent
a256b7c
commit a146758
Showing
5 changed files
with
266 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,50 @@ | ||
<?php | ||
|
||
namespace Cog\Laravel\Optimus\Traits; | ||
|
||
use Cog\Laravel\Optimus\Facades\Optimus; | ||
|
||
trait OptimusEncodedRouteKey | ||
{ | ||
/** | ||
* Get the value of the model's route key. | ||
* | ||
* @return mixed | ||
*/ | ||
public function getRouteKey() | ||
{ | ||
$id = parent::getRouteKey(); | ||
|
||
return $this->getOptimus()->encode($id); | ||
} | ||
|
||
/** | ||
* Retrieve the model for a bound value. | ||
* | ||
* @param mixed $value | ||
* | ||
* @return \Illuminate\Database\Eloquent\Model|null | ||
*/ | ||
public function resolveRouteBinding($value) | ||
{ | ||
$id = $this->getOptimus()->decode($value); | ||
|
||
return $this->where($this->getRouteKeyName(), '=', $id)->first(); | ||
} | ||
|
||
/** | ||
* Get the Optimus instance. | ||
* | ||
* @return \Cog\Laravel\Optimus\OptimusManager | ||
*/ | ||
protected function getOptimus() | ||
{ | ||
$connection = null; | ||
|
||
if (property_exists($this, 'optimusConnection')) { | ||
$connection = $this->optimusConnection; | ||
} | ||
|
||
return Optimus::connection($connection); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace Cog\Tests\Laravel\Optimus\Stubs\Models; | ||
|
||
use Cog\Laravel\Optimus\Traits\OptimusEncodedRouteKey; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class UserWithCustomOptimusConnection extends Model | ||
{ | ||
use OptimusEncodedRouteKey; | ||
|
||
protected $optimusConnection = 'custom'; | ||
|
||
protected $table = 'users'; | ||
protected $guarded = []; | ||
} |
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,14 @@ | ||
<?php | ||
|
||
namespace Cog\Tests\Laravel\Optimus\Stubs\Models; | ||
|
||
use Cog\Laravel\Optimus\Traits\OptimusEncodedRouteKey; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class UserWithDefaultOptimusConnection extends Model | ||
{ | ||
use OptimusEncodedRouteKey; | ||
|
||
protected $table = 'users'; | ||
protected $guarded = []; | ||
} |
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,141 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Optimus. | ||
* | ||
* (c) Anton Komarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cog\Tests\Laravel\Optimus\Traits; | ||
|
||
use Cog\Laravel\Optimus\Facades\Optimus; | ||
use Cog\Tests\Laravel\Optimus\AbstractTestCase; | ||
use Cog\Tests\Laravel\Optimus\Stubs\Models\UserWithCustomOptimusConnection; | ||
use Cog\Tests\Laravel\Optimus\Stubs\Models\UserWithDefaultOptimusConnection; | ||
use Illuminate\Routing\Middleware\SubstituteBindings; | ||
use Route; | ||
|
||
/** | ||
* Class OptimusEncodedRouteKeyTest. | ||
* | ||
* @package Cog\Tests\Laravel\Optimus\Traits | ||
*/ | ||
class OptimusEncodedRouteKeyTest extends AbstractTestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->loadLaravelMigrations(config('database.default')); | ||
$this->configurePrimeNumbers(); | ||
} | ||
|
||
public function testRouteKeyIsEncoded() | ||
{ | ||
$user = $this->createUserWithDefaultOptimusConnection(); | ||
$encodedId = Optimus::encode($user->id); | ||
|
||
$this->assertEquals($encodedId, $user->getRouteKey()); | ||
} | ||
|
||
public function testOptimusConnectionCanBeConfigured() | ||
{ | ||
$user = $this->createUserWithCustomOptimusConnection(); | ||
$routeKey = $user->getRouteKey(); | ||
|
||
$correctEncodedId = Optimus::connection('custom')->encode($user->id); | ||
$incorrectEncodedId = Optimus::connection('main')->encode($user->id); | ||
|
||
$this->assertEquals($correctEncodedId, $routeKey); | ||
$this->assertNotEquals($incorrectEncodedId, $routeKey); | ||
} | ||
|
||
public function testResolveModelWithEncodedKey() | ||
{ | ||
$user = $this->createUserWithDefaultOptimusConnection(); | ||
$encodedId = $user->getRouteKey(); | ||
$resolvedUser = $user->resolveRouteBinding($encodedId); | ||
|
||
$this->assertEquals($user->id, $resolvedUser->id); | ||
} | ||
|
||
public function testEncodedKeyIsUsedForRouteModelBinding() | ||
{ | ||
$user = $this->createUserWithDefaultOptimusConnection(); | ||
$encodedId = $user->getRouteKey(); | ||
|
||
Route::get('users/{user}', function (UserWithDefaultOptimusConnection $user) { | ||
return $user; | ||
})->middleware(SubstituteBindings::class); | ||
|
||
$this->get("users/{$encodedId}")->assertJsonFragment(['id' => $user->id]); | ||
} | ||
|
||
public function testEncodedRouteKeyIsUsedWhenGeneratingNamedRouteUrls() | ||
{ | ||
$user = $this->createUserWithDefaultOptimusConnection(); | ||
$encodedId = Optimus::encode($user->id); | ||
|
||
Route::get('users/{user}', function () { | ||
})->name('test.route'); | ||
|
||
$expectedUrl = "{$this->baseUrl}/users/{$encodedId}"; | ||
$generatedUrl = route('test.route', [$user]); | ||
|
||
$this->assertEquals($expectedUrl, $generatedUrl); | ||
} | ||
|
||
/** | ||
* Create a test user with default Optimus connection in the database. | ||
* | ||
* @return \Cog\Tests\Laravel\Optimus\Stubs\Models\UserWithDefaultOptimusConnection | ||
*/ | ||
protected function createUserWithDefaultOptimusConnection() | ||
{ | ||
return UserWithDefaultOptimusConnection::create([ | ||
'name' => 'Default Test User', | ||
'email' => '[email protected]', | ||
'password' => 'p4ssw0rd', | ||
]); | ||
} | ||
|
||
/** | ||
* Create a test user with custom Optimus connection in the database. | ||
* | ||
* @return \Cog\Tests\Laravel\Optimus\Stubs\Models\UserWithCustomOptimusConnection | ||
*/ | ||
protected function createUserWithCustomOptimusConnection() | ||
{ | ||
return UserWithCustomOptimusConnection::create([ | ||
'name' => 'Custom Test User', | ||
'email' => '[email protected]', | ||
'password' => 'p4ssw0rd', | ||
]); | ||
} | ||
|
||
/** | ||
* Configure some random prime numbers. | ||
* | ||
* @return void | ||
*/ | ||
protected function configurePrimeNumbers() | ||
{ | ||
config()->set('optimus.connections', [ | ||
'main' => [ | ||
'prime' => 1490261603, | ||
'inverse' => 1573362507, | ||
'random' => 1369544188, | ||
], | ||
'custom' => [ | ||
'prime' => 1770719809, | ||
'inverse' => 1417283009, | ||
'random' => 508877541, | ||
], | ||
]); | ||
} | ||
} |