Skip to content

Commit f3d99ff

Browse files
Add params to runner classes (#188)
1 parent 444a416 commit f3d99ff

13 files changed

Lines changed: 141 additions & 32 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- windows-latest
2424

2525
php:
26-
- "8.0"
26+
- 8.0
2727

2828
steps:
2929
- name: Checkout

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"codeception/c3": "^2.6.0",
5555
"codeception/codeception": "^4.1.5",
5656
"codeception/module-asserts": "@dev",
57+
"codeception/module-cli": "^1.1",
5758
"codeception/module-phpbrowser": "@dev",
5859
"phpunit/phpunit": "^9.5",
5960
"roave/infection-static-analysis-plugin": "^1.7",

public/index-test.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,21 @@
2121
$_SERVER['SCRIPT_NAME'] = '/index-test.php';
2222
}
2323

24-
define('YII_ENV', getenv('env') ?? 'test');
25-
2624
require_once dirname(__DIR__) . '/vendor/autoload.php';
2725

28-
$runner = new WebApplicationRunner();
29-
// Development mode:
30-
$runner->debug();
31-
// Run application:
26+
/**
27+
* Set debug value for web application runner, for default its `true` add additionally the validation of the
28+
* container-di configurations (debug mode).
29+
*/
30+
define('YII_DEBUG', getenv('YII_DEBUG') ?: true);
31+
32+
/**
33+
* Set environment value for web application runner, for default its `null`.
34+
*
35+
* @link https://github.com/yiisoft/config#environments
36+
*/
37+
define('YII_ENV', getenv('YII_ENV') ?: null);
38+
39+
// Run web application runner
40+
$runner = new WebApplicationRunner(YII_DEBUG, YII_ENV);
3241
$runner->run();

public/index.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@
1515
$_SERVER['SCRIPT_NAME'] = '/index.php';
1616
}
1717

18-
define('YII_ENV', getenv('env') ?? 'production');
19-
2018
require_once dirname(__DIR__) . '/vendor/autoload.php';
2119

22-
$runner = new WebApplicationRunner();
23-
// Development mode:
24-
$runner->debug();
25-
// Run application:
20+
/**
21+
* Set debug value for web application runner, for default its `true` add additionally the validation of the
22+
* container-di configurations (debug mode).
23+
*/
24+
define('YII_DEBUG', getenv('YII_DEBUG') ?: true);
25+
26+
/**
27+
* Set environment value for web application runner, for default its `null`.
28+
*
29+
* @link https://github.com/yiisoft/config#environments
30+
*/
31+
define('YII_ENV', getenv('YII_ENV') ?: null);
32+
33+
// Run web application runner
34+
$runner = new WebApplicationRunner(YII_DEBUG, YII_ENV);
2635
$runner->run();

src/Command/Hello.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Symfony\Component\Console\Command\Command;
88
use Symfony\Component\Console\Input\InputInterface;
99
use Symfony\Component\Console\Output\OutputInterface;
10+
use Yiisoft\Yii\Console\ExitCode;
1011

1112
final class Hello extends Command
1213
{
@@ -25,7 +26,6 @@ public function configure(): void
2526
protected function execute(InputInterface $input, OutputInterface $output): int
2627
{
2728
$output->writeln('Hello!');
28-
29-
return 1;
29+
return ExitCode::OK;
3030
}
3131
}

src/Runner/ConsoleApplicationRunner.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919

2020
final class ConsoleApplicationRunner
2121
{
22+
private bool $debug;
23+
private ?string $environment;
24+
25+
public function __construct(bool $debug, ?string $environment)
26+
{
27+
$this->debug = $debug;
28+
$this->environment = $environment;
29+
}
30+
2231
/**
2332
* @throws CircularReferenceException|ErrorException|Exception|InvalidConfigException|NotFoundException
2433
* @throws NotInstantiableException
@@ -28,7 +37,7 @@ public function run(): void
2837
$config = new Config(
2938
dirname(__DIR__, 2),
3039
'/config/packages', // Configs path.
31-
null,
40+
$this->environment,
3241
[
3342
'params',
3443
'events',
@@ -37,11 +46,7 @@ public function run(): void
3746
],
3847
);
3948

40-
$container = new Container(
41-
$config->get('console'),
42-
$config->get('providers-console')
43-
);
44-
49+
$container = new Container($config->get('console'), $config->get('providers-console'), [], $this->debug);
4550
$container = $container->get(ContainerInterface::class);
4651

4752
// Run bootstrap

src/Runner/WebApplicationRunner.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@
3434

3535
final class WebApplicationRunner
3636
{
37-
private bool $debug = false;
37+
private bool $debug;
38+
private ?string $environment;
3839

39-
public function debug(bool $enable = true): void
40+
public function __construct(bool $debug, ?string $environment)
4041
{
41-
$this->debug = $enable;
42+
$this->debug = $debug;
43+
$this->environment = $environment;
4244
}
4345

4446
/**
@@ -56,7 +58,7 @@ public function run(): void
5658
$config = new Config(
5759
dirname(__DIR__, 2),
5860
'/config/packages', // Configs path.
59-
null,
61+
$this->environment,
6062
[
6163
'params',
6264
'events',
@@ -65,7 +67,7 @@ public function run(): void
6567
],
6668
);
6769

68-
$container = new Container($config->get('web'), $config->get('providers-web'));
70+
$container = new Container($config->get('web'), $config->get('providers-web'), [], $this->debug);
6971

7072
// Register error handler with real container-configured dependencies.
7173
$this->registerErrorHandler($container->get(ErrorHandler::class), $errorHandler);

tests/Cli.suite.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
actor: CliTester
2+
modules:
3+
enabled:
4+
- Cli
5+
- \App\Tests\Helper\Cli
6+
step_decorators: ~

tests/Cli/ConsoleCest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\Cli;
6+
7+
use App\Tests\CliTester;
8+
9+
final class ConsoleCest
10+
{
11+
public function testCommandYii(CliTester $I): void
12+
{
13+
$command = dirname(__DIR__, 2) . '/yii';
14+
$I->runShellCommand($command);
15+
$I->seeInShellOutput('Yii Console');
16+
}
17+
18+
public function testCommandHello(CliTester $I): void
19+
{
20+
$command = dirname(__DIR__, 2) . '/yii';
21+
$I->runShellCommand($command . ' hello');
22+
$I->seeInShellOutput('Hello!');
23+
}
24+
}

tests/Unit/HelloCest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace App\Tests\Unit;
66

7+
use App\Tests\UnitTester;
78
use Psr\Container\ContainerInterface;
89
use Symfony\Component\Console\Application;
910
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
1011
use Symfony\Component\Console\Tester\CommandTester;
11-
use App\Tests\UnitTester;
1212
use Yiisoft\Config\Config;
1313
use Yiisoft\Di\Container;
14+
use Yiisoft\Yii\Console\ExitCode;
1415

1516
final class HelloCest
1617
{
@@ -44,7 +45,7 @@ public function testExecute(UnitTester $I): void
4445

4546
$commandCreate->setInputs(['yes']);
4647

47-
$I->assertEquals(1, $commandCreate->execute([]));
48+
$I->assertSame(ExitCode::OK, $commandCreate->execute([]));
4849

4950
$output = $commandCreate->getDisplay(true);
5051

0 commit comments

Comments
 (0)