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

Skip to content

Commit de43901

Browse files
Add support for env, server and ini settings in parallel test runs
1 parent da4db91 commit de43901

File tree

10 files changed

+600
-121
lines changed

10 files changed

+600
-121
lines changed

.appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ test_script:
5454
- SET X=0
5555
- SET SYMFONY_PHPUNIT_SKIPPED_TESTS=phpunit.skipped
5656
- copy /Y c:\php\php.ini-min c:\php\php.ini
57-
- php phpunit src\Symfony --exclude-group benchmark,intl-data || SET X=!errorlevel!
57+
- php phpunit src\Symfony --bootstrap vendor\autoload.php --exclude-group benchmark,intl-data || SET X=!errorlevel!
5858
- copy /Y c:\php\php.ini-max c:\php\php.ini
59-
- php phpunit src\Symfony --exclude-group benchmark,intl-data || SET X=!errorlevel!
59+
- php phpunit src\Symfony --bootstrap vendor\autoload.php --exclude-group benchmark,intl-data || SET X=!errorlevel!
6060
- exit %X%

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ before_install:
6969
[ -d ~/.composer ] || mkdir ~/.composer
7070
cp .composer/* ~/.composer/
7171
export PHPUNIT=$(readlink -f ./phpunit)
72-
export PHPUNIT_X="$PHPUNIT --exclude-group tty,benchmark,intl-data"
72+
export PHPUNIT_X="$PHPUNIT --bootstrap vendor/autoload.php --exclude-group tty,benchmark,intl-data"
7373
export COMPOSER_UP='composer update --no-progress --no-suggest --ansi'
7474
export COMPONENTS=$(find src/Symfony -mindepth 2 -type f -name phpunit.xml.dist -printf '%h\n')
7575
find ~/.phpenv -name xdebug.ini -delete
@@ -238,10 +238,10 @@ install:
238238
echo "$COMPONENTS" | xargs -n1 -I{} tar --append -f ~/php-ext/composer-lowest.lock.tar {}/composer.lock
239239
else
240240
echo "$COMPONENTS" | parallel --gnu "tfold {} $PHPUNIT_X {}"
241-
tfold src/Symfony/Component/Console.tty $PHPUNIT src/Symfony/Component/Console --group tty
241+
tfold src/Symfony/Component/Console.tty $PHPUNIT src/Symfony/Component/Console --bootstrap vendor/autoload.php --group tty
242242
if [[ $PHP = ${MIN_PHP%.*} ]]; then
243243
export PHP=$MIN_PHP
244-
tfold src/Symfony/Component/Process.sigchild SYMFONY_DEPRECATIONS_HELPER=weak php-$MIN_PHP/sapi/cli/php ./phpunit --colors=always src/Symfony/Component/Process/
244+
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/
245245
fi
246246
fi
247247
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\PhpUnit\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
16+
class SimplePhpUnitTest extends TestCase
17+
{
18+
private static $testFiles = [
19+
__DIR__.'/SimplePhpUnitTest/Modul1/phpunit.xml.dist',
20+
__DIR__.'/SimplePhpUnitTest/Modul2/phpunit.xml.dist',
21+
];
22+
23+
private $currentCwd;
24+
25+
public static function setUpBeforeClass()
26+
{
27+
foreach (self::$testFiles as $testFile) {
28+
$renamedFile = str_replace('.xml.dist', '.txml.dist', $testFile);
29+
30+
if (file_exists($renamedFile)) {
31+
rename($renamedFile, $testFile);
32+
}
33+
}
34+
}
35+
36+
public static function tearDownAfterClass()
37+
{
38+
foreach (self::$testFiles as $testFile) {
39+
if (file_exists($testFile)) {
40+
rename($testFile, str_replace('.xml.dist', '.txml.dist', $testFile));
41+
}
42+
}
43+
}
44+
45+
protected function setUp()
46+
{
47+
$this->currentCwd = getcwd();
48+
chdir(\dirname(__DIR__));
49+
}
50+
51+
protected function tearDown()
52+
{
53+
chdir($this->currentCwd);
54+
}
55+
56+
public function testInstall()
57+
{
58+
$cmd = 'bin/simple-phpunit install';
59+
$this->execute($cmd, $output, $exitCode);
60+
$this->assertSame(0, $exitCode);
61+
}
62+
63+
public function testSimplePhpunitShortConfigurationFile()
64+
{
65+
$cmd = 'bin/simple-phpunit -c Tests/SimplePhpUnitTest/Modul1/phpunit.xml.dist';
66+
$this->execute($cmd, $output);
67+
$this->assertContains('OK (7 tests, 11 assertions)', implode(PHP_EOL, $output));
68+
}
69+
70+
public function testSimplePhpunitWithConfigurationWithFilter()
71+
{
72+
$cmd = 'bin/simple-phpunit --filter=testEnv --configuration Tests/SimplePhpUnitTest/Modul1/phpunit.xml.dist';
73+
$this->execute($cmd, $output);
74+
$this->assertContains('OK (1 test, 1 assertion)', implode(PHP_EOL, $output));
75+
}
76+
77+
public function testParallelTests()
78+
{
79+
$cmd = 'bin/simple-phpunit Tests/SimplePhpUnitTest';
80+
$this->execute($cmd, $output);
81+
82+
// Check parallel test suites are runned successfully
83+
$testSuites = explode('Test Suite', implode(PHP_EOL, $output));
84+
85+
unset($testSuites[0]); // Remove header output
86+
$testSuites = array_values($testSuites);
87+
$this->assertCount(2, $testSuites);
88+
89+
$this->assertContains('OK (7 tests, 11 assertions)', $testSuites[0]);
90+
$this->assertContains('OK (7 tests, 11 assertions)', $testSuites[1]);
91+
92+
// Check different phpunit versions are installed
93+
$this->assertFileExists(\dirname(__DIR__).'/.phpunit/phpunit-6.5-remove-symfony_yaml-phpspec_prophecy/phpunit');
94+
$this->assertFileExists(\dirname(__DIR__).'/.phpunit/phpunit-7.4-remove-phpspec_prophecy-symfony_yaml/phpunit');
95+
}
96+
97+
private function execute($command, &$output = null, &$return_var = null)
98+
{
99+
$oldPhpUnitRootDirectory = getenv('SYMFONY_PHPUNIT_ROOT_DIRECTORY');
100+
$oldPhpUnitDirectory = getenv('SYMFONY_PHPUNIT_DIR');
101+
102+
// Use putenv vor windows compatible setting of environment variables
103+
putenv('SYMFONY_PHPUNIT_ROOT_DIRECTORY='.\dirname(__DIR__));
104+
putenv('SYMFONY_PHPUNIT_DIR='.\dirname(__DIR__).'/.phpunit');
105+
106+
$result = exec(
107+
sprintf('php %s', $command),
108+
$output,
109+
$return_var
110+
);
111+
112+
// Reset env variables
113+
if (false !== $oldPhpUnitRootDirectory) {
114+
// Set to old value
115+
putenv('SYMFONY_PHPUNIT_ROOT_DIRECTORY='.$oldPhpUnitRootDirectory);
116+
} else {
117+
// Remove when no old value exists
118+
putenv('SYMFONY_PHPUNIT_ROOT_DIRECTORY');
119+
}
120+
121+
if (false !== $oldPhpUnitDirectory) {
122+
// Set to old value
123+
putenv('SYMFONY_PHPUNIT_DIR='.$oldPhpUnitDirectory);
124+
} else {
125+
// Remove when no old value exists
126+
putenv('SYMFONY_PHPUNIT_DIR');
127+
}
128+
129+
return $result;
130+
}
131+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
6+
backupGlobals="false"
7+
colors="true"
8+
bootstrap="tests/bootstrap.php"
9+
>
10+
<php>
11+
<ini name="memory_limit" value="-1" />
12+
<ini name="precision" value="7"/>
13+
<server name="SERVER_VAR" value="SERVER_VAR_MODUL_1"/>
14+
<!-- to avoid conflicts with global env force=true is added for this test in CI -->
15+
<env name="ENV_VAR" value="ENV_VAR_MODUL_1" force="true" />
16+
<env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled" force="true" />
17+
<env name="SYMFONY_PHPUNIT_REMOVE" value="symfony/yaml phpspec/prophecy" force="true" />
18+
<env name="SYMFONY_PHPUNIT_VERSION" value="6.5" force="true" />
19+
</php>
20+
21+
<testsuites>
22+
<testsuite name="Modul1 Test Suite">
23+
<directory suffix=".tphp">tests</directory>
24+
</testsuite>
25+
</testsuites>
26+
27+
<filter>
28+
<whitelist>
29+
<directory>.</directory>
30+
</whitelist>
31+
</filter>
32+
33+
<listeners>
34+
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
35+
</listeners>
36+
</phpunit>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Symfony\Bridge\PhpUnit\Tests\SimplePhpUnitTest\Modul1;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class ModulTest extends TestCase
8+
{
9+
public function testEnv()
10+
{
11+
$this->assertSame('ENV_VAR_MODUL_1', getenv('ENV_VAR'));
12+
}
13+
14+
public function testServer()
15+
{
16+
$this->assertSame('SERVER_VAR_MODUL_1', $_SERVER['SERVER_VAR']);
17+
}
18+
19+
public function testIni()
20+
{
21+
$this->assertSame('7', ini_get('precision'));
22+
}
23+
24+
public function testBootstrapEnv()
25+
{
26+
$this->assertSame('BOOTSTRAP_ENV_VAR_MODUL_1', getenv('BOOTSTRAP_ENV_VAR'));
27+
28+
sleep(1); // To Check if the output is streamed
29+
30+
$this->assertTrue(true);
31+
}
32+
33+
public function testBootstrapServer()
34+
{
35+
$this->assertSame('BOOTSTRAP_SERVER_VAR_MODUL_1', $_SERVER['BOOTSTRAP_SERVER_VAR']);
36+
}
37+
38+
public function testBootstrapIni()
39+
{
40+
$this->assertSame('15', ini_get('serialize_precision'));
41+
}
42+
43+
public function testSymfonyEnvs()
44+
{
45+
$this->assertSame('disabled', getenv('SYMFONY_DEPRECATIONS_HELPER'));
46+
$this->assertSame('symfony/yaml phpspec/prophecy', getenv('SYMFONY_PHPUNIT_REMOVE'));
47+
$this->assertSame('6.5', getenv('SYMFONY_PHPUNIT_VERSION'));
48+
exec((defined('PHP_BINARY') ? PHP_BINARY : 'php') . ' ' . $_SERVER['SCRIPT_NAME'] . ' --version', $output);
49+
$this->assertContains('PHPUnit 6.5', $output[0]);
50+
51+
@trigger_error('Deprecation Error which should be ignored');
52+
}
53+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
putenv('BOOTSTRAP_ENV_VAR=BOOTSTRAP_ENV_VAR_MODUL_1');
4+
$_SERVER['BOOTSTRAP_SERVER_VAR'] = 'BOOTSTRAP_SERVER_VAR_MODUL_1';
5+
ini_set('serialize_precision', 15);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
6+
backupGlobals="false"
7+
colors="true"
8+
bootstrap="tests/bootstrap.php"
9+
>
10+
<php>
11+
<ini name="memory_limit" value="-1" />
12+
<ini name="precision" value="9" />
13+
<server name="SERVER_VAR" value="SERVER_VAR_MODUL_2"/>
14+
<!-- to avoid conflicts with global env force=true is added for this test in CI -->
15+
<env name="ENV_VAR" value="ENV_VAR_MODUL_2" force="true" />
16+
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" force="true" />
17+
<env name="SYMFONY_PHPUNIT_REMOVE" value="phpspec/prophecy symfony/yaml" force="true" />
18+
<env name="SYMFONY_PHPUNIT_VERSION" value="7.4" force="true" />
19+
</php>
20+
21+
<testsuites>
22+
<testsuite name="Modul2 Test Suite">
23+
<directory suffix=".tphp">tests</directory>
24+
</testsuite>
25+
</testsuites>
26+
27+
<filter>
28+
<whitelist>
29+
<directory>.</directory>
30+
</whitelist>
31+
</filter>
32+
33+
<listeners>
34+
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
35+
</listeners>
36+
</phpunit>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Symfony\Bridge\PhpUnit\Tests\SimplePhpUnitTest\Modul2;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class ModulTest extends TestCase
8+
{
9+
public function testEnv()
10+
{
11+
$this->assertSame('ENV_VAR_MODUL_2', getenv('ENV_VAR'));
12+
}
13+
14+
public function testServer()
15+
{
16+
$this->assertSame('SERVER_VAR_MODUL_2', $_SERVER['SERVER_VAR']);
17+
}
18+
19+
public function testIni()
20+
{
21+
$this->assertSame('9', ini_get('precision'));
22+
}
23+
24+
public function testBootstrapEnv()
25+
{
26+
$this->assertSame('BOOTSTRAP_ENV_VAR_MODUL_2', getenv('BOOTSTRAP_ENV_VAR'));
27+
28+
sleep(1); // To Check if the output is streamed
29+
30+
$this->assertTrue(true);
31+
}
32+
33+
public function testBootstrapServer()
34+
{
35+
$this->assertSame('BOOTSTRAP_SERVER_VAR_MODUL_2', $_SERVER['BOOTSTRAP_SERVER_VAR']);
36+
}
37+
38+
public function testBootstrapIni()
39+
{
40+
$this->assertSame('11', ini_get('serialize_precision'));
41+
}
42+
43+
public function testSymfonyDeprecationHelper()
44+
{
45+
$this->assertSame('weak', getenv('SYMFONY_DEPRECATIONS_HELPER'));
46+
$this->assertSame('phpspec/prophecy symfony/yaml', getenv('SYMFONY_PHPUNIT_REMOVE'));
47+
$this->assertSame('7.4', getenv('SYMFONY_PHPUNIT_VERSION'));
48+
exec((defined('PHP_BINARY') ? PHP_BINARY : 'php') . ' ' . $_SERVER['SCRIPT_NAME'] . ' --version', $output);
49+
$this->assertContains('PHPUnit 7.4', $output[0]);
50+
51+
@trigger_error('Deprecation Error which should be ignored');
52+
}
53+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
putenv('BOOTSTRAP_ENV_VAR=BOOTSTRAP_ENV_VAR_MODUL_2');
4+
$_SERVER['BOOTSTRAP_SERVER_VAR'] = 'BOOTSTRAP_SERVER_VAR_MODUL_2';
5+
ini_set('serialize_precision', 11);

0 commit comments

Comments
 (0)