Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure the application via a container #2793

Merged
merged 8 commits into from Aug 11, 2019
25 changes: 25 additions & 0 deletions Slim/Factory/AppFactory.php
Expand Up @@ -89,6 +89,31 @@ public static function create(
);
}

/**
* @param ContainerInterface $container
* @return App
*/
public static function createFromContainer(ContainerInterface $container): App
{
$responseFactory = $container->has(ResponseFactoryInterface::class)
? $container->get(ResponseFactoryInterface::class)
: self::determineResponseFactory();

$callableResolver = $container->has(CallableResolverInterface::class)
? $container->get(CallableResolverInterface::class)
: null;

$routeCollector = $container->has(RouteCollectorInterface::class)
? $container->get(RouteCollectorInterface::class)
: null;

$routeResolver = $container->has(RouteResolverInterface::class)
? $container->get(RouteResolverInterface::class)
: null;

return new App($responseFactory, $container, $callableResolver, $routeCollector, $routeResolver);
}

/**
* @return ResponseFactoryInterface
* @throws RuntimeException
Expand Down
65 changes: 65 additions & 0 deletions tests/Factory/AppFactoryTest.php
Expand Up @@ -228,4 +228,69 @@ public function testResponseAndStreamFactoryIsBeingInjectedInDecoratedResponseFa

$this->assertSame($streamFactoryProphecy->reveal(), $streamFactoryProperty->getValue($response));
}

public function testCreateAppWithContainerUsesContainerDependenciesWhenPresent()
{
$responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class);
$callableResolverProphecy = $this->prophesize(CallableResolverInterface::class);
$routeResolverProphecy = $this->prophesize(RouteResolverInterface::class);
$routeParserProphecy = $this->prophesize(RouteParserInterface::class);

$routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class);
$routeCollectorProphecy
->getRouteParser()
->willReturn($routeParserProphecy->reveal())
->shouldBeCalledOnce();

$containerProphecy = $this->prophesize(ContainerInterface::class);

$containerProphecy
->has(ResponseFactoryInterface::class)
->willReturn(true)
->shouldBeCalledOnce();

$containerProphecy
->get(ResponseFactoryInterface::class)
->willReturn($responseFactoryProphecy->reveal())
->shouldBeCalledOnce();

$containerProphecy
->has(CallableResolverInterface::class)
->willReturn(true)
->shouldBeCalledOnce();

$containerProphecy
->get(CallableResolverInterface::class)
->willReturn($callableResolverProphecy->reveal())
->shouldBeCalledOnce();

$containerProphecy
->has(RouteCollectorInterface::class)
->willReturn(true)
->shouldBeCalledOnce();

$containerProphecy
->get(RouteCollectorInterface::class)
->willReturn($routeCollectorProphecy->reveal())
->shouldBeCalledOnce();

$containerProphecy
->has(RouteResolverInterface::class)
->willReturn(true)
->shouldBeCalledOnce();

$containerProphecy
->get(RouteResolverInterface::class)
->willReturn($routeResolverProphecy->reveal())
->shouldBeCalledOnce();

AppFactory::setSlimHttpDecoratorsAutomaticDetection(false);
$app = AppFactory::createFromContainer($containerProphecy->reveal());

$this->assertSame($app->getResponseFactory(), $responseFactoryProphecy->reveal());
$this->assertSame($app->getContainer(), $containerProphecy->reveal());
$this->assertSame($app->getCallableResolver(), $callableResolverProphecy->reveal());
$this->assertSame($app->getRouteCollector(), $routeCollectorProphecy->reveal());
$this->assertSame($app->getRouteResolver(), $routeResolverProphecy->reveal());
}
}