1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
*/
namespace Slim\Tests;
use PHPUnit;
use Slim\CallableResolver;
use Slim\Container;
use Slim\Tests\Mocks\CallableTest;
use Slim\Tests\Mocks\InvokableTest;
class CallableResolverTest extends PHPUnit\Framework\TestCase
{
/**
* @var Container
*/
private $container;
protected function setUp(): void
{
CallableTest::$CalledCount = 0;
InvokableTest::$CalledCount = 0;
$this->container = new Container();
}
public function testClosure()
{
$test = function () {
static $called_count = 0;
return $called_count++;
};
$resolver = new CallableResolver($this->container);
$callable = $resolver->resolve($test);
$callable();
$this->assertEquals(1, $callable());
}
public function testFunctionName()
{
// @codingStandardsIgnoreStart
function testCallable()
{
static $called_count = 0;
return $called_count++;
};
// @codingStandardsIgnoreEnd
$resolver = new CallableResolver($this->container);
$callable = $resolver->resolve(__NAMESPACE__ . '\testCallable');
$callable();
$this->assertEquals(1, $callable());
}
public function testObjMethodArray()
{
$obj = new CallableTest();
$resolver = new CallableResolver($this->container);
$callable = $resolver->resolve([$obj, 'toCall']);
$callable();
$this->assertEquals(1, CallableTest::$CalledCount);
}
public function testSlimCallable()
{
$resolver = new CallableResolver($this->container);
$callable = $resolver->resolve('Slim\Tests\Mocks\CallableTest:toCall');
$callable();
$this->assertEquals(1, CallableTest::$CalledCount);
}
public function testSlimCallableContainer()
{
$resolver = new CallableResolver($this->container);
$resolver->resolve('Slim\Tests\Mocks\CallableTest:toCall');
$this->assertEquals($this->container, CallableTest::$CalledContainer);
}
public function testContainer()
{
$this->container['callable_service'] = new CallableTest();
$resolver = new CallableResolver($this->container);
$callable = $resolver->resolve('callable_service:toCall');
$callable();
$this->assertEquals(1, CallableTest::$CalledCount);
}
public function testResolutionToAnInvokableClassInContainer()
{
$this->container['an_invokable'] = function ($c) {
return new InvokableTest();
};
$resolver = new CallableResolver($this->container);
$callable = $resolver->resolve('an_invokable');
$callable();
$this->assertEquals(1, InvokableTest::$CalledCount);
}
public function testResolutionToAnInvokableClass()
{
$resolver = new CallableResolver($this->container);
$callable = $resolver->resolve('Slim\Tests\Mocks\InvokableTest');
$callable();
$this->assertEquals(1, InvokableTest::$CalledCount);
}
public function testMethodNotFoundThrowException()
{
$this->container['callable_service'] = new CallableTest();
$resolver = new CallableResolver($this->container);
$this->expectException('\RuntimeException');
$resolver->resolve('callable_service:noFound');
}
public function testFunctionNotFoundThrowException()
{
$resolver = new CallableResolver($this->container);
$this->expectException('\RuntimeException');
$resolver->resolve('noFound');
}
public function testClassNotFoundThrowException()
{
$resolver = new CallableResolver($this->container);
$this->expectException('\RuntimeException');
$this->expectExceptionMessage('Callable Unknown does not exist');
$resolver->resolve('Unknown:notFound');
}
public function testCallableClassNotFoundThrowException()
{
$resolver = new CallableResolver($this->container);
$this->expectException('\RuntimeException');
$this->expectExceptionMessage('is not resolvable');
$resolver->resolve(['Unknown', 'notFound']);
}
public function testCallableInvalidTypeThrowException()
{
$resolver = new CallableResolver($this->container);
$this->expectException('\RuntimeException');
$this->expectExceptionMessage('is not resolvable');
$resolver->resolve(__LINE__);
}
}
|