Skip to content

Commit

Permalink
Handle string values in resolveRouteBinding() (#22)
Browse files Browse the repository at this point in the history
Fix resolveRouteBinding() method when route value is string
  • Loading branch information
ivanvermeyen authored Feb 29, 2020
1 parent a5e6214 commit 1eb84f4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Traits/OptimusEncodedRouteKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public function getRouteKey()
*/
public function resolveRouteBinding($value)
{
if (is_string($value) && ctype_digit($value)) {
$value = (int) $value;
}

if (!is_int($value)) {
return null;
}

$id = $this->getOptimus()->decode($value);

return $this->where($this->getRouteKeyName(), '=', $id)->first();
Expand Down
64 changes: 64 additions & 0 deletions tests/Traits/OptimusEncodedRouteKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Cog\Tests\Laravel\Optimus\Stubs\Models\UserWithDefaultOptimusConnection;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Support\Facades\Route;
use Jenssegers\Optimus\Optimus as JenssegersOptimus;

class OptimusEncodedRouteKeyTest extends AbstractTestCase
{
Expand Down Expand Up @@ -85,6 +86,69 @@ public function testEncodedRouteKeyIsUsedWhenGeneratingNamedRouteUrls(): void
$this->assertEquals($expectedUrl, $generatedUrl);
}

public function testNonExistingIDsReturnNull()
{
$user = $this->createUserWithDefaultOptimusConnection();
$resolvedUser = $user->resolveRouteBinding(999);

$this->assertNull($resolvedUser);
}

public function testStringValuesReturnNull()
{
$user = $this->createUserWithDefaultOptimusConnection();
$resolvedUser = $user->resolveRouteBinding('not-an-integer');

$this->assertNull($resolvedUser);
}

public function testStringValuesContainingIntegersReturnNull()
{
$user = $this->createUserWithDefaultOptimusConnection();
$resolvedUser = $user->resolveRouteBinding('1-not-just-an-integer');

$this->assertNull($resolvedUser);
}

public function testStringValuesContainingEncodedRouteKeysReturnNull()
{
$user = $this->createUserWithDefaultOptimusConnection();
$encodedRouteKey = $user->getRouteKey();
$resolvedUser = $user->resolveRouteBinding("{$encodedRouteKey}-suffix-to-the-encoded-route-key");

$this->assertNull($resolvedUser);
}

public function testArrayValuesReturnNull()
{
$user = $this->createUserWithDefaultOptimusConnection();
$encodedRouteKey = $user->getRouteKey();
$resolvedUser = $user->resolveRouteBinding([$encodedRouteKey]);

$this->assertNull($resolvedUser);
}

public function testFloatValuesReturnNull()
{
$user = $this->createUserWithDefaultOptimusConnection();
$resolvedUser = $user->resolveRouteBinding(12.3);

$this->assertNull($resolvedUser);
}

public function testExistingIntegerValuesBelow256AreResolved()
{
$user = $this->createUserWithDefaultOptimusConnection();

$optimus = $this->mock(JenssegersOptimus::class);
$optimus->shouldReceive('decode')->with(123)->andReturn($user->id);
Optimus::shouldReceive('connection')->andReturn($optimus);

$resolvedUser = $user->resolveRouteBinding(123);

$this->assertTrue($user->is($resolvedUser));
}

/**
* Create a test user with default Optimus connection in the database.
*
Expand Down

0 comments on commit 1eb84f4

Please sign in to comment.