-
Notifications
You must be signed in to change notification settings - Fork 196
Support for setting PHP settings through configuration #193
Changes from 2 commits
0282f2f
4c73602
6851310
4ae7ff6
7bd9632
bade4e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,13 +136,32 @@ public function __invoke(ContainerInterface $container) | |
|
||
$app = new Application($router, $container, $finalHandler, $emitter); | ||
|
||
$this->setPhpSettings($container); | ||
$this->injectPreMiddleware($app, $container); | ||
$this->injectRoutes($app, $container); | ||
$this->injectPostMiddleware($app, $container); | ||
|
||
return $app; | ||
} | ||
|
||
/** | ||
* Sets provided PHP settings, if any. | ||
* | ||
* @param ContainerInterface $container | ||
*/ | ||
private function setPhpSettings(ContainerInterface $container) | ||
{ | ||
$config = $container->has('config') ? $container->get('config') : []; | ||
|
||
if (!isset($config['php_settings']) || !is_array($config['php_settings'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the value is not an array I guess an exception should be raised instead silently ignore. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kinda imitated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. /cc @weierophinney I think all those type checks should throw Exception instead of silently ignore. |
||
return; | ||
} | ||
|
||
foreach ($config['php_settings'] as $name => $value) { | ||
ini_set($name, $value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should raise an exception if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I agree, exception can be raised there. |
||
} | ||
} | ||
|
||
/** | ||
* Inject routes from configuration, if any. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,60 @@ public function getRouterFromApplication(Application $app) | |
return $r->getValue($app); | ||
} | ||
|
||
public function testFactorySetsPhpSettingsFromConfig() | ||
{ | ||
$this->container | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really needed? |
||
->has('Zend\Expressive\Router\RouterInterface') | ||
->willReturn(false); | ||
$this->container | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This expectation is unrelated with the feature under test. Only adds noise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that it adds noise, but for some reason I couldn't bypass those opening lines within the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Maks3w Any idea how to bypass this error when I remove all those expectations:
? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is related with the previous statement. https://github.com/zendframework/zend-expressive/pull/193/files#r45551040 Ok, it must be as you wrote. |
||
->get('Zend\Expressive\Router\RouterInterface') | ||
->shouldNotBeCalled(); | ||
|
||
$this->container | ||
->has('Zend\Diactoros\Response\EmitterInterface') | ||
->willReturn(false); | ||
$this->container | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this expectation. |
||
->get('Zend\Diactoros\Response\EmitterInterface') | ||
->shouldNotBeCalled(); | ||
|
||
$this->container | ||
->has('Zend\Expressive\FinalHandler') | ||
->willReturn(false); | ||
$this->container | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this expectation. |
||
->get('Zend\Expressive\FinalHandler') | ||
->shouldNotBeCalled(); | ||
|
||
$config = [ | ||
'php_settings' => [ | ||
'display_errors' => 1, | ||
'date.timezone' => 'Europe/Belgrade', | ||
], | ||
]; | ||
|
||
$currentPhpSettings = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better use PHPUnit
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the tip |
||
foreach ($config['php_settings'] as $name => $value) { | ||
$currentPhpSettings[$name] = ini_get($name); | ||
} | ||
|
||
$this->container | ||
->has('config') | ||
->willReturn(true); | ||
|
||
$this->container | ||
->get('config') | ||
->willReturn($config); | ||
|
||
$this->factory->__invoke($this->container->reveal()); | ||
|
||
foreach ($config['php_settings'] as $name => $value) { | ||
$this->assertEquals($value, ini_get($name)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this foreach should be wrapped in a try/finally where try is this loop and finally is the tear down loop |
||
} | ||
|
||
foreach ($currentPhpSettings as $name => $value) { | ||
ini_set($name, $value); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Maks3w Is this in line with what you've mentioned about having only one loop? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PHPUnit assertions throw exceptions when assertion fail. So ini_restore won't be restored when test fail so next tests in the run will be executed with a wrong environment setting. After $config $this->iniSet('display_errors', 0);
$this->iniSet('date.timezone', 'UTC'); |
||
} | ||
|
||
public function testFactoryWillPullAllReplaceableDependenciesFromContainerWhenPresent() | ||
{ | ||
$router = $this->prophesize('Zend\Expressive\Router\RouterInterface'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Maks3w What do you think about moving
$this->setPhpSettings($container);
at the very beginning of a__invoke()
method, because it doesn't need$app
at all?