Skip to content
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
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<a href="#installation"><img alt="TESTO"
<a href="#get-started"><img alt="TESTO"
src="https://github.com/php-testo/.github/blob/1.x/resources/logo-full.svg?raw=true"
style="width: 2in; display: block"
/></a>
Expand All @@ -16,7 +16,9 @@

<br />

## Installation
## Get Started

### Installation

```bash
composer require testo/testo
Expand All @@ -27,6 +29,33 @@ composer require testo/testo
[![License](https://img.shields.io/packagist/l/testo/testo.svg?style=flat-square)](LICENSE.md)
[![Total Destroys](https://img.shields.io/packagist/dt/testo/testo.svg?style=flat-square)](https://packagist.org/packages/testo/testo/stats)

### Configuration

By default, if no configuration file is provided, Testo will run tests from the `tests` folder.

To customize the configuration, create a `testo.php` file in the root of your project:

```php
<?php

declare(strict_types=1);

use Testo\Config\ApplicationConfig;
use Testo\Config\SuiteConfig;
use Testo\Config\FinderConfig;

return new ApplicationConfig(
suites: [
new SuiteConfig(
name: 'Unit',
location: new FinderConfig(
include: ['tests/Unit'],
),
),
],
);
```

## IDE Support

Testo comes with the [IDEA plugin `Testo`](https://plugins.jetbrains.com/plugin/28842-testo?noRedirect=true).
Expand Down
21 changes: 17 additions & 4 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Testo\Common\Container;
use Testo\Common\Filter;
use Testo\Common\Internal\ObjectContainer;
use Testo\Common\Path;
use Testo\Config\ApplicationConfig;
use Testo\Config\Internal\ConfigInflector;
use Testo\Config\ServicesConfig;
Expand Down Expand Up @@ -39,13 +40,13 @@ public static function createFromConfig(
/**
* Create the application instance from ENV, CLI arguments, and config file.
*
* @param non-empty-string|null $xml Path to XML file or raw XML content
* @param Path|null $configFile Path to config file
* @param array<string, mixed> $inputOptions Command-line options
* @param array<string, mixed> $inputArguments Command-line arguments
* @param array<string, string> $environment Environment variables
*/
public static function createFromInput(
?string $xml = null,
?Path $configFile = null,
array $inputOptions = [],
array $inputArguments = [],
array $environment = [],
Expand All @@ -57,8 +58,20 @@ public static function createFromInput(
'inputOptions' => $inputOptions,
];

# Read XML config file
// $xml === null or $args['xml'] = $this->readConfig($xml);
# Bind reading provided config file
$configFile === null or $container
->bind(ApplicationConfig::class, function () use ($configFile): ApplicationConfig {
$cfg = include $configFile;
$cfg instanceof ApplicationConfig or throw new \InvalidArgumentException(
\sprintf(
'Configuration file %s must return an instance of %s, %s returned.',
$configFile,
ApplicationConfig::class,
\is_object($cfg) ? \get_class($cfg) : \gettype($cfg),
),
);
return $cfg;
});

# Register Config inflector
$container->addInflector($container->make(ConfigInflector::class, $args));
Expand Down
3 changes: 0 additions & 3 deletions src/Assert/Interceptor/ObjectTrackerInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

namespace Testo\Assert\Interceptor;

use Testo\Assert\State;
use Testo\Assert\State\AssertException;
use Testo\Assert\State\Record;
use Testo\Assert\State\Success;
use Testo\Assert\StaticState;
use Testo\Interceptor\TestRunInterceptor;
use Testo\Test\Dto\Status;
Expand Down
45 changes: 32 additions & 13 deletions src/Common/Command/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
use Symfony\Component\Console\Style\SymfonyStyle;
use Testo\Application;
use Testo\Common\Container;
use Testo\Config\ApplicationConfig;
use Testo\Config\FinderConfig;
use Testo\Config\SuiteConfig;
use Testo\Common\Path;
use Yiisoft\Injector\Injector;

/**
Expand Down Expand Up @@ -72,17 +70,12 @@ protected function execute(
InputInterface $input,
OutputInterface $output,
): int {
$cfg = new ApplicationConfig(
src: new FinderConfig(['src']),
suites: [
new SuiteConfig(
name: 'default',
location: new FinderConfig(['tests/Testo']),
),
],
$this->application = Application::createFromInput(
configFile: $this->getConfigFile($input),
inputOptions: $input->getOptions(),
inputArguments: $input->getArguments(),
environment: \getenv(),
);

$this->application = Application::createFromConfig($cfg);
$this->container = $this->application->getContainer();

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

return $this->container->get(Injector::class)->invoke($this) ?? Command::SUCCESS;
}

/**
* Resolves configuration file path from input or default location.
*
* @param InputInterface $input Command input
*
* @return Path|null Path to the configuration file
*/
protected function getConfigFile(InputInterface $input): ?Path
{
/** @var string|null $config */
$config = $input->getOption('config');
$isConfigured = $config !== null;
// $config ??= './testo.xml';
$config ??= './testo.php';

if (\is_file($config)) {
return Path::create($config);
}

$isConfigured and throw new \InvalidArgumentException(
'Configuration file not found: ' . $config,
);

return null;
}
}
1 change: 0 additions & 1 deletion src/Common/Command/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down
1 change: 0 additions & 1 deletion src/Render/Terminal/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ public static function assertionHistoryHeader(OutputFormat $format): string
* Formats a single assertion line.
*
* @param \Testo\Assert\State\Record $assertion
* @param OutputFormat $format
* @return non-empty-string
*/
public static function assertionLine(object $assertion, OutputFormat $format): string
Expand Down
1 change: 0 additions & 1 deletion src/Render/TerminalInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Testo\Interceptor\TestRunInterceptor;
use Testo\Interceptor\TestSuiteRunInterceptor;
use Testo\Render\Terminal\ColorMode;
use Testo\Render\Terminal\OutputFormat;
use Testo\Render\Terminal\Style;
use Testo\Render\Terminal\TerminalLogger;
use Testo\Test\Dto\CaseInfo;
Expand Down
17 changes: 17 additions & 0 deletions testo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Testo\Config\ApplicationConfig;
use Testo\Config\SuiteConfig;

return new ApplicationConfig(
suites: [
new SuiteConfig(
name: 'default',
location: new \Testo\Config\FinderConfig(
include: ['tests/Testo'],
),
),
],
);