Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add support for env, server and ini settings in parallel test runs #28995

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

Closed
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
4 changes: 2 additions & 2 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ test_script:
- SET X=0
- SET SYMFONY_PHPUNIT_SKIPPED_TESTS=phpunit.skipped
- copy /Y c:\php\php.ini-min c:\php\php.ini
- php phpunit src\Symfony --exclude-group benchmark,intl-data || SET X=!errorlevel!
- php phpunit src\Symfony --bootstrap vendor\autoload.php --exclude-group benchmark,intl-data || SET X=!errorlevel!
- copy /Y c:\php\php.ini-max c:\php\php.ini
- php phpunit src\Symfony --exclude-group benchmark,intl-data || SET X=!errorlevel!
- php phpunit src\Symfony --bootstrap vendor\autoload.php --exclude-group benchmark,intl-data || SET X=!errorlevel!
- exit %X%
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ before_install:
[ -d ~/.composer ] || mkdir ~/.composer
cp .composer/* ~/.composer/
export PHPUNIT=$(readlink -f ./phpunit)
export PHPUNIT_X="$PHPUNIT --exclude-group tty,benchmark,intl-data"
export PHPUNIT_X="$PHPUNIT --bootstrap vendor/autoload.php --exclude-group tty,benchmark,intl-data"
export COMPOSER_UP='composer update --no-progress --no-suggest --ansi'
export COMPONENTS=$(find src/Symfony -mindepth 2 -type f -name phpunit.xml.dist -printf '%h\n')
find ~/.phpenv -name xdebug.ini -delete
Expand Down Expand Up @@ -238,10 +238,10 @@ install:
echo "$COMPONENTS" | xargs -n1 -I{} tar --append -f ~/php-ext/composer-lowest.lock.tar {}/composer.lock
else
echo "$COMPONENTS" | parallel --gnu "tfold {} $PHPUNIT_X {}"
tfold src/Symfony/Component/Console.tty $PHPUNIT src/Symfony/Component/Console --group tty
tfold src/Symfony/Component/Console.tty $PHPUNIT src/Symfony/Component/Console --bootstrap vendor/autoload.php --group tty
if [[ $PHP = ${MIN_PHP%.*} ]]; then
export PHP=$MIN_PHP
tfold src/Symfony/Component/Process.sigchild SYMFONY_DEPRECATIONS_HELPER=weak php-$MIN_PHP/sapi/cli/php ./phpunit --colors=always src/Symfony/Component/Process/
tfold src/Symfony/Component/Process.sigchild SYMFONY_DEPRECATIONS_HELPER=weak php-$MIN_PHP/sapi/cli/php ./phpunit --bootstrap vendor/autoload.php --colors=always src/Symfony/Component/Process/
fi
fi
}
Expand Down
131 changes: 131 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Tests/SimplePhpUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit\Tests;

use PHPUnit\Framework\TestCase;

class SimplePhpUnitTest extends TestCase
{
private static $testFiles = [
__DIR__.'/SimplePhpUnitTest/Modul1/phpunit.xml.dist',
__DIR__.'/SimplePhpUnitTest/Modul2/phpunit.xml.dist',
];

private $currentCwd;

public static function setUpBeforeClass()
{
foreach (self::$testFiles as $testFile) {
$renamedFile = str_replace('.xml.dist', '.txml.dist', $testFile);

if (file_exists($renamedFile)) {
rename($renamedFile, $testFile);
}
}
}

public static function tearDownAfterClass()
{
foreach (self::$testFiles as $testFile) {
if (file_exists($testFile)) {
rename($testFile, str_replace('.xml.dist', '.txml.dist', $testFile));
}
}
}

protected function setUp()
{
$this->currentCwd = getcwd();
chdir(\dirname(__DIR__));
}

protected function tearDown()
{
chdir($this->currentCwd);
}

public function testInstall()
{
$cmd = 'bin/simple-phpunit install';
$this->execute($cmd, $output, $exitCode);
$this->assertSame(0, $exitCode);
}

public function testSimplePhpunitShortConfigurationFile()
{
$cmd = 'bin/simple-phpunit -c Tests/SimplePhpUnitTest/Modul1/phpunit.xml.dist';
$this->execute($cmd, $output);
$this->assertContains('OK (7 tests, 11 assertions)', implode(PHP_EOL, $output));
}

public function testSimplePhpunitWithConfigurationWithFilter()
{
$cmd = 'bin/simple-phpunit --filter=testEnv --configuration Tests/SimplePhpUnitTest/Modul1/phpunit.xml.dist';
$this->execute($cmd, $output);
$this->assertContains('OK (1 test, 1 assertion)', implode(PHP_EOL, $output));
}

public function testParallelTests()
{
$cmd = 'bin/simple-phpunit Tests/SimplePhpUnitTest';
$this->execute($cmd, $output);

// Check parallel test suites are runned successfully
$testSuites = explode('Test Suite', implode(PHP_EOL, $output));

unset($testSuites[0]); // Remove header output
$testSuites = array_values($testSuites);
$this->assertCount(2, $testSuites);

$this->assertContains('OK (7 tests, 11 assertions)', $testSuites[0]);
$this->assertContains('OK (7 tests, 11 assertions)', $testSuites[1]);

// Check different phpunit versions are installed
$this->assertFileExists(\dirname(__DIR__).'/.phpunit/phpunit-6.5-remove-symfony_yaml-phpspec_prophecy/phpunit');
$this->assertFileExists(\dirname(__DIR__).'/.phpunit/phpunit-7.4-remove-phpspec_prophecy-symfony_yaml/phpunit');
}

private function execute($command, &$output = null, &$return_var = null)
{
$oldPhpUnitRootDirectory = getenv('SYMFONY_PHPUNIT_ROOT_DIRECTORY');
$oldPhpUnitDirectory = getenv('SYMFONY_PHPUNIT_DIR');

// Use putenv vor windows compatible setting of environment variables
putenv('SYMFONY_PHPUNIT_ROOT_DIRECTORY='.\dirname(__DIR__));
putenv('SYMFONY_PHPUNIT_DIR='.\dirname(__DIR__).'/.phpunit');

$result = exec(
sprintf('php %s', $command),
$output,
$return_var
);

// Reset env variables
if (false !== $oldPhpUnitRootDirectory) {
// Set to old value
putenv('SYMFONY_PHPUNIT_ROOT_DIRECTORY='.$oldPhpUnitRootDirectory);
} else {
// Remove when no old value exists
putenv('SYMFONY_PHPUNIT_ROOT_DIRECTORY');
}

if (false !== $oldPhpUnitDirectory) {
// Set to old value
putenv('SYMFONY_PHPUNIT_DIR='.$oldPhpUnitDirectory);
} else {
// Remove when no old value exists
putenv('SYMFONY_PHPUNIT_DIR');
}

return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
>
<php>
<ini name="memory_limit" value="-1" />
<ini name="precision" value="7"/>
<server name="SERVER_VAR" value="SERVER_VAR_MODUL_1"/>
<!-- to avoid conflicts with global env force=true is added for this test in CI -->
<env name="ENV_VAR" value="ENV_VAR_MODUL_1" force="true" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled" force="true" />
<env name="SYMFONY_PHPUNIT_REMOVE" value="symfony/yaml phpspec/prophecy" force="true" />
<env name="SYMFONY_PHPUNIT_VERSION" value="6.5" force="true" />
</php>

<testsuites>
<testsuite name="Modul1 Test Suite">
<directory suffix=".tphp">tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>.</directory>
</whitelist>
</filter>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
</phpunit>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Symfony\Bridge\PhpUnit\Tests\SimplePhpUnitTest\Modul1;

use PHPUnit\Framework\TestCase;

class ModulTest extends TestCase
{
public function testEnv()
{
$this->assertSame('ENV_VAR_MODUL_1', getenv('ENV_VAR'));
}

public function testServer()
{
$this->assertSame('SERVER_VAR_MODUL_1', $_SERVER['SERVER_VAR']);
}

public function testIni()
{
$this->assertSame('7', ini_get('precision'));
}

public function testBootstrapEnv()
{
$this->assertSame('BOOTSTRAP_ENV_VAR_MODUL_1', getenv('BOOTSTRAP_ENV_VAR'));

sleep(1); // To Check if the output is streamed

$this->assertTrue(true);
}

public function testBootstrapServer()
{
$this->assertSame('BOOTSTRAP_SERVER_VAR_MODUL_1', $_SERVER['BOOTSTRAP_SERVER_VAR']);
}

public function testBootstrapIni()
{
$this->assertSame('15', ini_get('serialize_precision'));
}

public function testSymfonyEnvs()
{
$this->assertSame('disabled', getenv('SYMFONY_DEPRECATIONS_HELPER'));
$this->assertSame('symfony/yaml phpspec/prophecy', getenv('SYMFONY_PHPUNIT_REMOVE'));
$this->assertSame('6.5', getenv('SYMFONY_PHPUNIT_VERSION'));
exec((defined('PHP_BINARY') ? PHP_BINARY : 'php') . ' ' . $_SERVER['SCRIPT_NAME'] . ' --version', $output);
$this->assertContains('PHPUnit 6.5', $output[0]);

@trigger_error('Deprecation Error which should be ignored');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

putenv('BOOTSTRAP_ENV_VAR=BOOTSTRAP_ENV_VAR_MODUL_1');
$_SERVER['BOOTSTRAP_SERVER_VAR'] = 'BOOTSTRAP_SERVER_VAR_MODUL_1';
ini_set('serialize_precision', 15);
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
>
<php>
<ini name="memory_limit" value="-1" />
<ini name="precision" value="9" />
<server name="SERVER_VAR" value="SERVER_VAR_MODUL_2"/>
<!-- to avoid conflicts with global env force=true is added for this test in CI -->
<env name="ENV_VAR" value="ENV_VAR_MODUL_2" force="true" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" force="true" />
<env name="SYMFONY_PHPUNIT_REMOVE" value="phpspec/prophecy symfony/yaml" force="true" />
<env name="SYMFONY_PHPUNIT_VERSION" value="7.4" force="true" />
</php>

<testsuites>
<testsuite name="Modul2 Test Suite">
<directory suffix=".tphp">tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>.</directory>
</whitelist>
</filter>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
</phpunit>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Symfony\Bridge\PhpUnit\Tests\SimplePhpUnitTest\Modul2;

use PHPUnit\Framework\TestCase;

class ModulTest extends TestCase
{
public function testEnv()
{
$this->assertSame('ENV_VAR_MODUL_2', getenv('ENV_VAR'));
}

public function testServer()
{
$this->assertSame('SERVER_VAR_MODUL_2', $_SERVER['SERVER_VAR']);
}

public function testIni()
{
$this->assertSame('9', ini_get('precision'));
}

public function testBootstrapEnv()
{
$this->assertSame('BOOTSTRAP_ENV_VAR_MODUL_2', getenv('BOOTSTRAP_ENV_VAR'));

sleep(1); // To Check if the output is streamed

$this->assertTrue(true);
}

public function testBootstrapServer()
{
$this->assertSame('BOOTSTRAP_SERVER_VAR_MODUL_2', $_SERVER['BOOTSTRAP_SERVER_VAR']);
}

public function testBootstrapIni()
{
$this->assertSame('11', ini_get('serialize_precision'));
}

public function testSymfonyDeprecationHelper()
{
$this->assertSame('weak', getenv('SYMFONY_DEPRECATIONS_HELPER'));
$this->assertSame('phpspec/prophecy symfony/yaml', getenv('SYMFONY_PHPUNIT_REMOVE'));
$this->assertSame('7.4', getenv('SYMFONY_PHPUNIT_VERSION'));
exec((defined('PHP_BINARY') ? PHP_BINARY : 'php') . ' ' . $_SERVER['SCRIPT_NAME'] . ' --version', $output);
$this->assertContains('PHPUnit 7.4', $output[0]);

@trigger_error('Deprecation Error which should be ignored');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

putenv('BOOTSTRAP_ENV_VAR=BOOTSTRAP_ENV_VAR_MODUL_2');
$_SERVER['BOOTSTRAP_SERVER_VAR'] = 'BOOTSTRAP_SERVER_VAR_MODUL_2';
ini_set('serialize_precision', 11);
Loading