Skip to content

Commit e38f5a5

Browse files
committed
feat: Process --config flag;
load `testo.php` by default if existing
1 parent 8e57201 commit e38f5a5

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

src/Application.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Testo\Common\Container;
88
use Testo\Common\Filter;
99
use Testo\Common\Internal\ObjectContainer;
10+
use Testo\Common\Path;
1011
use Testo\Config\ApplicationConfig;
1112
use Testo\Config\Internal\ConfigInflector;
1213
use Testo\Config\ServicesConfig;
@@ -39,13 +40,13 @@ public static function createFromConfig(
3940
/**
4041
* Create the application instance from ENV, CLI arguments, and config file.
4142
*
42-
* @param non-empty-string|null $xml Path to XML file or raw XML content
43+
* @param Path|null $configFile Path to config file
4344
* @param array<string, mixed> $inputOptions Command-line options
4445
* @param array<string, mixed> $inputArguments Command-line arguments
4546
* @param array<string, string> $environment Environment variables
4647
*/
4748
public static function createFromInput(
48-
?string $xml = null,
49+
?Path $configFile = null,
4950
array $inputOptions = [],
5051
array $inputArguments = [],
5152
array $environment = [],
@@ -57,8 +58,20 @@ public static function createFromInput(
5758
'inputOptions' => $inputOptions,
5859
];
5960

60-
# Read XML config file
61-
// $xml === null or $args['xml'] = $this->readConfig($xml);
61+
# Bind reading provided config file
62+
$configFile === null or $container
63+
->bind(ApplicationConfig::class, function () use ($configFile): ApplicationConfig {
64+
$cfg = include $configFile;
65+
$cfg instanceof ApplicationConfig or throw new \InvalidArgumentException(
66+
\sprintf(
67+
'Configuration file %s must return an instance of %s, %s returned.',
68+
$configFile,
69+
ApplicationConfig::class,
70+
\is_object($cfg) ? \get_class($cfg) : \gettype($cfg),
71+
),
72+
);
73+
return $cfg;
74+
});
6275

6376
# Register Config inflector
6477
$container->addInflector($container->make(ConfigInflector::class, $args));

src/Common/Command/Base.php

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
use Symfony\Component\Console\Style\SymfonyStyle;
1313
use Testo\Application;
1414
use Testo\Common\Container;
15-
use Testo\Config\ApplicationConfig;
16-
use Testo\Config\FinderConfig;
17-
use Testo\Config\SuiteConfig;
15+
use Testo\Common\Path;
1816
use Yiisoft\Injector\Injector;
1917

2018
/**
@@ -72,17 +70,12 @@ protected function execute(
7270
InputInterface $input,
7371
OutputInterface $output,
7472
): int {
75-
$cfg = new ApplicationConfig(
76-
src: new FinderConfig(['src']),
77-
suites: [
78-
new SuiteConfig(
79-
name: 'default',
80-
location: new FinderConfig(['tests/Testo']),
81-
),
82-
],
73+
$this->application = Application::createFromInput(
74+
configFile: $this->getConfigFile($input),
75+
inputOptions: $input->getOptions(),
76+
inputArguments: $input->getArguments(),
77+
environment: \getenv(),
8378
);
84-
85-
$this->application = Application::createFromConfig($cfg);
8679
$this->container = $this->application->getContainer();
8780

8881
$this->container->set($input, InputInterface::class);
@@ -91,4 +84,30 @@ protected function execute(
9184

9285
return $this->container->get(Injector::class)->invoke($this) ?? Command::SUCCESS;
9386
}
87+
88+
/**
89+
* Resolves configuration file path from input or default location.
90+
*
91+
* @param InputInterface $input Command input
92+
*
93+
* @return Path|null Path to the configuration file
94+
*/
95+
protected function getConfigFile(InputInterface $input): ?Path
96+
{
97+
/** @var string|null $config */
98+
$config = $input->getOption('config');
99+
$isConfigured = $config !== null;
100+
// $config ??= './testo.xml';
101+
$config ??= './testo.php';
102+
103+
if (\is_file($config)) {
104+
return Path::create($config);
105+
}
106+
107+
$isConfigured and throw new \InvalidArgumentException(
108+
'Configuration file not found: ' . $config,
109+
);
110+
111+
return null;
112+
}
94113
}

src/Common/Command/Run.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Symfony\Component\Console\Attribute\AsCommand;
88
use Symfony\Component\Console\Command\Command;
9-
use Symfony\Component\Console\Input\InputArgument;
109
use Symfony\Component\Console\Input\InputInterface;
1110
use Symfony\Component\Console\Input\InputOption;
1211
use Symfony\Component\Console\Output\OutputInterface;

0 commit comments

Comments
 (0)