start ConfigLoader tests

This commit is contained in:
danilopolani
2022-04-01 14:34:43 +02:00
parent 80c411be44
commit 751a532124
3 changed files with 49 additions and 26 deletions

View File

@ -29,9 +29,10 @@ use Symfony\Component\Console\Output\OutputInterface;
$rootPath = dirname($autoloadPath, 2); $rootPath = dirname($autoloadPath, 2);
$argv = new ArgvInput(); $argv = new ArgvInput();
$phpunitConfig = new ConfigLoader($rootPath); $testSuite = TestSuite::getInstance(
$rootPath,
$testSuite = TestSuite::getInstance($rootPath, $argv->getParameterOption('--test-directory', $phpunitConfig->getTestsDirectory())); $argv->getParameterOption('--test-directory', (new ConfigLoader($rootPath))->getTestsDirectory())
);
$isDecorated = $argv->getParameterOption('--colors', 'always') !== 'never'; $isDecorated = $argv->getParameterOption('--colors', 'always') !== 'never';
$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, $isDecorated); $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, $isDecorated);

View File

@ -13,14 +13,17 @@ use Throwable;
*/ */
final class ConfigLoader final class ConfigLoader
{ {
private ?SimpleXMLElement $config = null;
/** /**
* Default path if config loading went wrong. * Default path if config loading went wrong.
* *
* @var string * @var string
*/ */
private const DEFAULT_TESTS_PATH = 'tests'; public const DEFAULT_TESTS_PATH = 'tests';
/**
* XML tree of the PHPUnit configuration file.
*/
private ?SimpleXMLElement $config = null;
/** /**
* Creates a new instance of the config loader. * Creates a new instance of the config loader.
@ -62,6 +65,26 @@ final class ConfigLoader
return is_dir($basePath) ? $basePath : self::DEFAULT_TESTS_PATH; return is_dir($basePath) ? $basePath : self::DEFAULT_TESTS_PATH;
} }
/**
* Get the configuration file path.
*/
public function getConfigurationFilePath(): string|false
{
$candidates = [
$this->rootPath . '/phpunit.xml',
$this->rootPath . '/phpunit.dist.xml',
$this->rootPath . '/phpunit.xml.dist',
];
foreach ($candidates as $candidate) {
if (is_file($candidate)) {
return realpath($candidate);
}
}
return false;
}
/** /**
* Load the configuration file. * Load the configuration file.
*/ */
@ -87,24 +110,4 @@ final class ConfigLoader
// Restore the correct error reporting // Restore the correct error reporting
error_reporting($oldReportingLevel); error_reporting($oldReportingLevel);
} }
/**
* Get the configuration file path.
*/
private function getConfigurationFilePath(): string|false
{
$candidates = [
$this->rootPath . '/phpunit.xml',
$this->rootPath . '/phpunit.dist.xml',
$this->rootPath . '/phpunit.xml.dist',
];
foreach ($candidates as $candidate) {
if (is_file($candidate)) {
return realpath($candidate);
}
}
return false;
}
} }

View File

@ -0,0 +1,19 @@
<?php
use Pest\ConfigLoader;
use Pest\Support\Reflection;
it('fallbacks to default path if no phpunit file is found', function () {
$instance = new ConfigLoader('fake-path');
expect(Reflection::getPropertyValue($instance, 'config'))->toBeNull();
expect($instance->getConfigurationFilePath())->toBeFalse();
expect($instance->getTestsDirectory())->toBe(ConfigLoader::DEFAULT_TESTS_PATH);
});
it('fallbacks to default path if phpunit is not a valid XML')->skip();
it('fallbacks to default path if failing to read phpunit content')->skip();
it('fallbacks to default path if there is no test suites directory')->skip();
it('fallbacks to default path if test suite directory has no value')->skip();
it('fallbacks to default path if test suite directory does not exist')->skip();
it('returns the parent folder of first test suite directory')->skip();