Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PHPUnit 9.x #27

Merged
merged 9 commits into from Jun 18, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -16,7 +16,7 @@
},
"require": {
"php": "^7.0",
"phpunit/phpunit": "^6.0 || ^7.0 || ^8.0",
"phpunit/phpunit": "^6.0 || ^7.0 || ^8.0 || ^9.0",
"symfony/console": "^2.7 || ^3.0 || ^4.0 || ^5.0"
},
"bin": [
Expand Down
3 changes: 0 additions & 3 deletions phpunit.xml.dist
Expand Up @@ -7,9 +7,6 @@
</testsuite>
</testsuites>
<filter>
<blacklist>
<directory suffix=".php">tests/</directory>
</blacklist>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
</whitelist>
Expand Down
11 changes: 5 additions & 6 deletions src/Command/ValidateCommand.php
Expand Up @@ -49,24 +49,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln($this->getApplication()->getLongVersion());

$configuration = InputHandler::handleInput($input);
$configurationHolder = InputHandler::handleInput($input);
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
$output->writeln(PHP_EOL . sprintf(
'Configuration file loaded: %s',
$configuration->getFilename()
$configurationHolder->getFilename()
));
}

$suiteList = TestSuiteLoader::loadSuite($configuration);
if (!count($suiteList)) {
$testCollection = TestSuiteLoader::loadSuite($configurationHolder);
if ($testCollection->isEmpty()) {
$output->writeln(PHP_EOL . 'No tests found to validate.');
return 0;
}

$failedCount = 0;
$suiteIterator = new \RecursiveIteratorIterator($suiteList);
/** @var TestCase $suite */
foreach ($suiteIterator as $suite) {
foreach ($testCollection as $suite) {
if ($suite instanceof WarningTestCase) {
continue;
}
Expand Down
16 changes: 8 additions & 8 deletions src/Handler/InputHandler.php
Expand Up @@ -5,7 +5,7 @@
use OckCyp\CoversValidator\Loader\ConfigLoader;
use OckCyp\CoversValidator\Loader\FileLoader;
use OckCyp\CoversValidator\Locator\ConfigLocator;
use PHPUnit\Util\Configuration;
use OckCyp\CoversValidator\Model\ConfigurationHolder;
use Symfony\Component\Console\Input\InputInterface;

class InputHandler
Expand All @@ -14,23 +14,23 @@ class InputHandler
* Handle console input
*
* @param InputInterface $input
* @return Configuration
* @return ConfigurationHolder
*/
public static function handleInput(InputInterface $input)
{
$configOption = $input->getOption('configuration');
$configFile = ConfigLocator::locate($configOption);
$configuration = ConfigLoader::loadConfig($configFile);
$configurationHolder = ConfigLoader::loadConfig($configFile);

$phpunit = $configuration->getPHPUnitConfiguration();
$bootstrap = $configurationHolder->getBootstrap();
if (null !== $input->getOption('bootstrap')) {
$phpunit['bootstrap'] = $input->getOption('bootstrap');
$bootstrap = $input->getOption('bootstrap');
}

if (isset($phpunit['bootstrap'])) {
FileLoader::loadFile($phpunit['bootstrap']);
if ($bootstrap) {
FileLoader::loadFile($bootstrap);
}

return $configuration;
return $configurationHolder;
}
}
27 changes: 24 additions & 3 deletions src/Loader/ConfigLoader.php
Expand Up @@ -2,6 +2,8 @@

namespace OckCyp\CoversValidator\Loader;

use OckCyp\CoversValidator\Model\ConfigurationHolder;
use PHPUnit\TextUI\Configuration\Loader;
use PHPUnit\Util\Configuration;

class ConfigLoader
Expand All @@ -10,10 +12,29 @@ class ConfigLoader
* Load configuration from file
*
* @param string $fileName
* @return Configuration
* @return ConfigurationHolder
*/
public static function loadConfig($fileName)
public static function loadConfig(string $fileName): ConfigurationHolder
{
return Configuration::getInstance($fileName);
if (class_exists(Configuration::class)) {
// @codeCoverageIgnoreStart
$configuration = Configuration::getInstance($fileName);
$filename = $configuration->getFilename();
$phpunit = $configuration->getPHPUnitConfiguration();
$bootstrap = '';
if (isset($phpunit['bootstrap'])) {
$bootstrap = $phpunit['bootstrap'];
}
// @codeCoverageIgnoreEnd
} else {
$loader = new Loader();

$configuration = $loader->load($fileName);
$filename = $configuration->filename();
$phpunit = $configuration->phpunit();
$bootstrap = $phpunit->hasBootstrap() ? $phpunit->bootstrap() : null;
}

return new ConfigurationHolder($configuration, $filename, $bootstrap);
}
}
2 changes: 2 additions & 0 deletions src/Loader/FileLoader.php
Expand Up @@ -15,8 +15,10 @@ public static function loadFile($filename)
// PHPUnit 6.x
\PHPUnit\Util\Fileloader::checkAndLoad($filename);
} else {
// @codeCoverageIgnoreStart
// PHPUnit 7.x+
\PHPUnit\Util\FileLoader::checkAndLoad($filename);
// @codeCoverageIgnoreEnd
}
}
}
12 changes: 6 additions & 6 deletions src/Loader/TestSuiteLoader.php
Expand Up @@ -2,19 +2,19 @@

namespace OckCyp\CoversValidator\Loader;

use PHPUnit\Framework\TestSuite;
use PHPUnit\Util\Configuration;
use OckCyp\CoversValidator\Model\ConfigurationHolder;
use OckCyp\CoversValidator\Model\TestCollection;

class TestSuiteLoader
{
/**
* Load test suite
*
* @param Configuration $configuration
* @return TestSuite
* @param ConfigurationHolder $configurationHolder
* @return TestCollection
*/
public static function loadSuite(Configuration $configuration)
public static function loadSuite(ConfigurationHolder $configurationHolder): TestCollection
{
return $configuration->getTestSuiteConfiguration();
return new TestCollection($configurationHolder);
}
}
51 changes: 51 additions & 0 deletions src/Model/ConfigurationHolder.php
@@ -0,0 +1,51 @@
<?php

namespace OckCyp\CoversValidator\Model;

use PHPUnit\Util\Configuration as PHPUnit8Configuration;
use PHPUnit\TextUI\Configuration\Configuration;

class ConfigurationHolder
{
/**
* @var PHPUnit8Configuration|Configuration
*/
private $configuration;

/**
* @var string
*/
private $filename;

/**
* @var string|null
*/
private $bootstrap;

/**
* @param PHPUnit8Configuration|Configuration $configuration
* @param string $filename
* @param string|null $bootstrap
*/
public function __construct($configuration, string $filename, $bootstrap)
{
$this->configuration = $configuration;
$this->filename = $filename;
$this->bootstrap = $bootstrap;
}

public function getFilename(): string
{
return $this->filename;
}

public function getConfiguration()
{
return $this->configuration;
}

public function getBootstrap()
{
return $this->bootstrap;
}
}
64 changes: 64 additions & 0 deletions src/Model/TestCollection.php
@@ -0,0 +1,64 @@
<?php

namespace OckCyp\CoversValidator\Model;

use PHPUnit\Util\Configuration as PHPUnit8Configuration;
use PHPUnit\TextUI\Configuration\TestSuiteMapper;

class TestCollection implements \Iterator
{
/**
* @var \Iterator
*/
private $iterator;

/**
* @var \Iterator
*/
private $iteratorIterator;

public function __construct(ConfigurationHolder $configurationHolder)
{
$configuration = $configurationHolder->getConfiguration();

if ($configuration instanceof PHPUnit8Configuration) {
$this->iterator = $configuration->getTestSuiteConfiguration();
} else {
$testSuiteMapper = new TestSuiteMapper();

$this->iterator = $testSuiteMapper->map($configuration->testSuite(), '');
}

$this->iteratorIterator = new \RecursiveIteratorIterator($this->iterator);
}

public function current()
{
return $this->iteratorIterator->current();
}

public function key()
{
return $this->iteratorIterator->key();
}

public function next()
{
$this->iteratorIterator->next();
}

public function rewind()
{
$this->iteratorIterator->rewind();
}

public function valid(): bool
{
return $this->iteratorIterator->valid();
}

public function isEmpty(): bool
{
return !\count($this->iterator);
}
}
18 changes: 18 additions & 0 deletions tests/BaseTestCase.php
Expand Up @@ -19,4 +19,22 @@ protected function getFixtureClassName($class)
{
return static::FIXTURE_NS_PREFIX . $class;
}

protected function assertRegex(string $pattern, string $string, string $message = '')
{
if (method_exists($this, 'assertMatchesRegularExpression')) {
return $this->assertMatchesRegularExpression($pattern, $string, $message);
}

return $this->assertRegExp($pattern, $string, $message);
}

protected function assertNotRegex(string $pattern, string $string, string $message = '')
{
if (method_exists($this, 'assertMatchesRegularExpression')) {
return $this->assertDoesNotMatchRegularExpression($pattern, $string, $message);
}

return $this->assertNotRegExp($pattern, $string, $message);
}
}