Skip to content

Commit

Permalink
Adding missing test coverage on Application class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brent Scheffler committed Apr 12, 2022
1 parent 8d2c154 commit e0639b8
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,12 @@ private function getReflectionParametersForCallable(callable $handler): array
$reflector = $reflectionClass->getMethod($method);
}

elseif( \is_object($handler) && \method_exists($handler, "__invoke")) {

elseif( \is_object($handler) && \method_exists($handler, "__invoke") ) {
$reflectionObject = new ReflectionObject($handler);
$reflector = $reflectionObject->getMethod("__invoke");
}

elseif( \is_string($handler)) {
elseif( \is_string($handler) ) {
$reflector = new ReflectionFunction($handler);
}

Expand Down
134 changes: 130 additions & 4 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use Capsule\ServerRequest;
use Carton\Container;
use DateTime;
use DateTimeImmutable;
use Nimbly\Limber\Application;
use Nimbly\Limber\EmptyStream;
use Nimbly\Limber\ExceptionHandlerInterface;
use Nimbly\Limber\Exceptions\ApplicationException;
use Nimbly\Limber\Exceptions\CallableResolutionException;
use Nimbly\Limber\Exceptions\ClassResolutionException;
use Nimbly\Limber\Exceptions\HttpException;
use Nimbly\Limber\Exceptions\MethodNotAllowedHttpException;
Expand All @@ -21,6 +23,7 @@
use Nimbly\Limber\Router\Router;
use Nimbly\Limber\Router\RouterInterface;
use Nimbly\Limber\Tests\Fixtures\ConstructorClass;
use Nimbly\Limber\Tests\Fixtures\HandlerClass;
use Nimbly\Limber\Tests\Fixtures\InvokableClass;
use Nimbly\Limber\Tests\Fixtures\SampleMiddleware;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -568,9 +571,7 @@ public function test_resolve_reflection_parameters_with_class_using_container():

public function test_resolve_reflection_parameters_with_server_request_instance(): void
{
$application = new Application(
new Router
);
$application = new Application(new Router);

$reflectionClass = new ReflectionClass($application);
$reflectionMethod = $reflectionClass->getMethod("resolveReflectionParameters");
Expand Down Expand Up @@ -624,7 +625,43 @@ public function test_resolve_reflection_parameters_with_making_class_with_constr
);
}

public function test_resolve_reflection_parameters_with_unresolvable_throws_dependency_resolution_exception(): void
public function test_resolve_reflection_parameters_with_non_reflection_named_type_throws_parameter_resolution_exception(): void
{
$application = new Application(new Router);

$reflectionClass = new ReflectionClass($application);
$reflectionMethod = $reflectionClass->getMethod("resolveReflectionParameters");
$reflectionMethod->setAccessible(true);

$callable = function(DateTime|DateTimeImmutable $dateTime): void {
echo "The date is now: " . $dateTime;
};

$reflectionFunction = new ReflectionFunction($callable);

$this->expectException(ParameterResolutionException::class);
$reflectionMethod->invokeArgs($application, [$reflectionFunction->getParameters()]);
}

public function test_resolve_reflection_parameters_with_unmakeable_throws_parameter_resolution_exception(): void
{
$application = new Application(new Router);

$reflectionClass = new ReflectionClass($application);
$reflectionMethod = $reflectionClass->getMethod("resolveReflectionParameters");
$reflectionMethod->setAccessible(true);

$callable = function(ServerRequest $request): void {
echo "Hello world!";
};

$reflectionFunction = new ReflectionFunction($callable);

$this->expectException(ParameterResolutionException::class);
$reflectionMethod->invokeArgs($application, [$reflectionFunction->getParameters()]);
}

public function test_resolve_reflection_parameters_with_unresolvable_throws_parameter_resolution_exception(): void
{
$application = new Application(new Router);

Expand All @@ -642,6 +679,28 @@ public function test_resolve_reflection_parameters_with_unresolvable_throws_depe
$reflectionMethod->invokeArgs($application, [$reflectionFunction->getParameters()]);
}

public function test_resolve_reflection_parameters_with_default_values(): void
{
$application = new Application(new Router);

$reflectionClass = new ReflectionClass($application);
$reflectionMethod = $reflectionClass->getMethod("resolveReflectionParameters");
$reflectionMethod->setAccessible(true);

$callable = function(string $option = "opt1", ?string $option2 = null): void {
echo "Hello world with " . $option;
};

$reflectionFunction = new ReflectionFunction($callable);

$parameters = $reflectionMethod->invokeArgs($application, [$reflectionFunction->getParameters()]);

$this->assertEquals(
["opt1", null],
$parameters
);
}

public function test_call(): void
{
$application = new Application(new Router);
Expand Down Expand Up @@ -724,6 +783,64 @@ public function test_make_on_class_with_constructor(): void
);
}

public function test_make_callable_on_class_string(): void
{
$application = new Application(new Router);

$reflectionClass = new ReflectionClass($application);
$reflectionMethod = $reflectionClass->getMethod("makeCallable");
$reflectionMethod->setAccessible(true);

$callable = $reflectionMethod->invokeArgs($application, [InvokableClass::class]);

$this->assertIsCallable($callable);
$this->assertInstanceOf(InvokableClass::class, $callable);
}

public function test_make_callable_on_class_and_method_string(): void
{
$application = new Application(new Router);

$reflectionClass = new ReflectionClass($application);
$reflectionMethod = $reflectionClass->getMethod("makeCallable");
$reflectionMethod->setAccessible(true);

$class_string = HandlerClass::class . "@handle";

$callable = $reflectionMethod->invokeArgs($application, [$class_string]);

$this->assertIsCallable($callable);
$this->assertIsArray($callable);
$this->assertInstanceOf(HandlerClass::class, $callable[0]);
$this->assertEquals("handle", $callable[1]);
}

public function test_make_callable_on_callable_returns_callable(): void
{
$application = new Application(new Router);

$reflectionClass = new ReflectionClass($application);
$reflectionMethod = $reflectionClass->getMethod("makeCallable");
$reflectionMethod->setAccessible(true);

$callable = $reflectionMethod->invokeArgs($application, ["\\strtolower"]);

$this->assertIsCallable($callable);
$this->assertEquals("\\strtolower", $callable);
}

public function test_make_callable_on_non_callable_throws_callable_resolution_exception(): void
{
$application = new Application(new Router);

$reflectionClass = new ReflectionClass($application);
$reflectionMethod = $reflectionClass->getMethod("makeCallable");
$reflectionMethod->setAccessible(true);

$this->expectException(CallableResolutionException::class);
$reflectionMethod->invokeArgs($application, ["\\non_existent_callable"]);
}

public function test_make_on_class_with_constructor_and_user_args(): void
{
$application = new Application(new Router);
Expand All @@ -745,4 +862,13 @@ public function test_make_on_class_with_constructor_and_user_args(): void
$instance->getParam1()
);
}

public function test_make_on_non_makeable_class_throws_class_resolution_exception(): void
{
$application = new Application(new Router);

$this->expectException(ClassResolutionException::class);

$application->make("\\Non\\Existent\\Class");
}
}
19 changes: 19 additions & 0 deletions tests/Fixtures/HandlerClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Nimbly\Limber\Tests\Fixtures;

use Capsule\Response;
use Capsule\ResponseStatus;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class HandlerClass
{
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new Response(
ResponseStatus::OK,
"Hello world!"
);
}
}
13 changes: 13 additions & 0 deletions tests/Fixtures/UnresolvableConstructorClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Nimbly\Limber\Tests\Fixtures;

class UnresolvableConstructorClass
{
public function __construct(

)
{

}
}
2 changes: 2 additions & 0 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
/**
* @covers Nimbly\Limber\Router\Router
* @covers Nimbly\Limber\Router\Route
*
* @uses Nimbly\Limber\Router\RouterInterface
*/
class RouterTest extends TestCase
{
Expand Down

0 comments on commit e0639b8

Please sign in to comment.