Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Feature/fix programmatic config #52

Merged
merged 3 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ All notable changes to this project will be documented in this file, in reverse
- [#48](https://github.com/zendframework/zend-expressive-swoole/pull/48) adds a `shutdown` handler to the Swoole HTTP server that clears the PID
manager, ensuring the PID file is cleared.

- [#52](https://github.com/zendframework/zend-expressive-swoole/pull/52) fixes an error thrown by the `start` command when using this component in
configuration-driven expressive applications, due to the fact that the command
always tried to require the `config/pipeline.php` and `config/routes.php`
files.

## 2.0.0 - 2018-11-15

### Added
Expand Down
16 changes: 13 additions & 3 deletions src/Command/StartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Zend\Expressive\MiddlewareFactory;
use Zend\Expressive\Swoole\PidManager;

use function file_exists;

class StartCommand extends Command
{
use IsRunningTrait;
Expand All @@ -34,6 +36,11 @@ class StartCommand extends Command
do not provide the option, 4 workers will be started.
EOH;

private const PROGRAMMATIC_CONFIG_FILES = [
'config/pipeline.php',
'config/routes.php',
];

/**
* @var ContainerInterface
*/
Expand Down Expand Up @@ -93,9 +100,12 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
$factory = $this->container->get(MiddlewareFactory::class);

// Execute programmatic/declarative middleware pipeline and routing
// configuration statements
(require 'config/pipeline.php')($app, $factory, $this->container);
(require 'config/routes.php')($app, $factory, $this->container);
// configuration statements, if they exist
foreach (self::PROGRAMMATIC_CONFIG_FILES as $configFile) {
if (file_exists($configFile)) {
(require $configFile)($app, $factory, $this->container);
}
}

// Run the application
$app->run();
Expand Down
48 changes: 35 additions & 13 deletions test/Command/StartCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,7 @@ public function testExecuteRunsApplicationWithoutSettingOptionsIfNoneProvided(ar
$this->input->getOption('daemonize')->willReturn(false);
$this->input->getOption('num-workers')->willReturn(null);

$this->pidManager->read()->willReturn($pids);
$this->pushServiceToContainer(PidManager::class, $this->pidManager);

$httpServer = $this->prophesize(TestAsset\HttpServer::class);
$this->pushServiceToContainer(SwooleHttpServer::class, $httpServer);

$middlewareFactory = $this->prophesize(MiddlewareFactory::class);
$this->pushServiceToContainer(MiddlewareFactory::class, $middlewareFactory);

$application = $this->prophesize(Application::class);
$this->pushServiceToContainer(Application::class, $application);

$command = new StartCommand($this->container->reveal());
[$command, $httpServer, $application] = $this->prepareSuccessfulStartCommand($pids);

$execute = $this->reflectMethod($command, 'execute');

Expand All @@ -274,4 +262,38 @@ public function testExecuteRunsApplicationWithoutSettingOptionsIfNoneProvided(ar
->writeln(Argument::containingString('Server is already running'))
->shouldNotHaveBeenCalled();
}

public function testExecutionDoesNotFailEvenIfProgrammaticConfigFilesDoNotExist()
{
set_include_path($this->originalIncludePath);

[$command] = $this->prepareSuccessfulStartCommand([]);

$execute = $this->reflectMethod($command, 'execute');

$this->assertSame(0, $execute->invoke(
$command,
$this->input->reveal(),
$this->output->reveal()
));
}

private function prepareSuccessfulStartCommand(array $pids) : array
{
$this->pidManager->read()->willReturn($pids);
$this->pushServiceToContainer(PidManager::class, $this->pidManager);

$httpServer = $this->prophesize(TestAsset\HttpServer::class);
$this->pushServiceToContainer(SwooleHttpServer::class, $httpServer);

$middlewareFactory = $this->prophesize(MiddlewareFactory::class);
$this->pushServiceToContainer(MiddlewareFactory::class, $middlewareFactory);

$application = $this->prophesize(Application::class);
$this->pushServiceToContainer(Application::class, $application);

$command = new StartCommand($this->container->reveal());

return [$command, $httpServer, $application];
}
}