Skip to content

Commit

Permalink
Merge 5975762 into 8ab3432
Browse files Browse the repository at this point in the history
  • Loading branch information
adriansuter committed Aug 18, 2019
2 parents 8ab3432 + 5975762 commit fcd5356
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Slim/DeferredCallable.php
Expand Up @@ -9,6 +9,7 @@

namespace Slim;

use RuntimeException;
use Slim\Interfaces\CallableResolverInterface;

class DeferredCallable
Expand All @@ -29,6 +30,13 @@ class DeferredCallable
*/
public function __construct($callable, ?CallableResolverInterface $resolver = null)
{
if ($resolver === null && is_string($callable) && preg_match(CallableResolver::$callablePattern, $callable)) {
throw new RuntimeException(sprintf(
'Slim callable notation %s is not allowed without callable resolver.',
$callable
));
}

$this->callable = $callable;
$this->callableResolver = $resolver;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/DeferredCallableTest.php
Expand Up @@ -10,6 +10,7 @@
namespace Slim\Tests;

use Psr\Container\ContainerInterface;
use RuntimeException;
use Slim\CallableResolver;
use Slim\DeferredCallable;
use Slim\Tests\Mocks\CallableTest;
Expand Down Expand Up @@ -68,4 +69,41 @@ public function testItReturnsInvokedCallableResponse()
$response = $deferred($foo);
$this->assertEquals($bar, $response);
}

public function testFunctionNameIfNoResolver()
{
$deferredTrim = new DeferredCallable('trim');
$this->assertSame('foo', $deferredTrim(' foo '));
}

public function testClosureIfNoResolver()
{
$closure = function ($a, $b) {
return $a + $b;
};

$deferredClosure = new DeferredCallable($closure);
$this->assertSame(42, $deferredClosure(31, 11));
}

public static function getFoo(): string
{
return 'foo';
}

public function testClassNameMethodNameNotation()
{
// Test `ClassName::methodName` notation for a static method.
$deferredGetFoo = new DeferredCallable(get_class($this) . '::getFoo');
$this->assertSame('foo', $deferredGetFoo());
}

/**
* @expectedException RuntimeException
* @expectedExceptionMessage Slim callable notation CallableTest:toCall is not allowed without callable resolver.
*/
public function testSlimCallableNotationThrowsExceptionIfNoResolver()
{
new DeferredCallable('CallableTest:toCall');
}
}

0 comments on commit fcd5356

Please sign in to comment.