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

Skip to content

Commit 05c3314

Browse files
committed
Merge branch '2.3' into 2.4
* 2.3: [HttpKernel] added an analyze of environment parameters for built-in server. change command to which available under most unix systems add way to test command under windows fix shell command injection [Form] allowed CallbackTransformer to use callable [Process] Added process synchronization to the incremental output tests
2 parents 6f56ea4 + 6176345 commit 05c3314

File tree

6 files changed

+89
-19
lines changed

6 files changed

+89
-19
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/router_dev.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
return false;
2626
}
2727

28+
$_SERVER = array_merge($_SERVER, $_ENV);
2829
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'app_dev.php';
2930

3031
require 'app_dev.php';

src/Symfony/Bundle/FrameworkBundle/Resources/config/router_prod.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
return false;
2626
}
2727

28+
$_SERVER = array_merge($_SERVER, $_ENV);
2829
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'app.php';
2930

3031
require 'app.php';

src/Symfony/Component/Finder/Shell/Shell.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,19 @@ public function getType()
5050
*/
5151
public function testCommand($command)
5252
{
53-
if (self::TYPE_WINDOWS === $this->type) {
54-
// todo: find a way to test if Windows command exists
55-
return false;
56-
}
57-
5853
if (!function_exists('exec')) {
5954
return false;
6055
}
6156

6257
// todo: find a better way (command could not be available)
63-
exec('command -v '.$command, $output, $code);
58+
$testCommand = 'which ';
59+
if (self::TYPE_WINDOWS === $this->type) {
60+
$testCommand = 'where ';
61+
}
62+
63+
$command = escapeshellcmd($command);
64+
65+
exec($testCommand.$command, $output, $code);
6466

6567
return 0 === $code && count($output) > 0;
6668
}

src/Symfony/Component/Form/CallbackTransformer.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,33 @@ class CallbackTransformer implements DataTransformerInterface
1818
{
1919
/**
2020
* The callback used for forward transform
21-
* @var \Closure
21+
* @var callable
2222
*/
2323
private $transform;
2424

2525
/**
2626
* The callback used for reverse transform
27-
* @var \Closure
27+
* @var callable
2828
*/
2929
private $reverseTransform;
3030

3131
/**
3232
* Constructor.
3333
*
34-
* @param \Closure $transform The forward transform callback
35-
* @param \Closure $reverseTransform The reverse transform callback
34+
* @param callable $transform The forward transform callback
35+
* @param callable $reverseTransform The reverse transform callback
36+
*
37+
* @throws \InvalidArgumentException when the given callbacks is invalid
3638
*/
37-
public function __construct(\Closure $transform, \Closure $reverseTransform)
39+
public function __construct($transform, $reverseTransform)
3840
{
41+
if (!is_callable($transform)) {
42+
throw new \InvalidArgumentException('Argument 1 should be a callable');
43+
}
44+
if (!is_callable($reverseTransform)) {
45+
throw new \InvalidArgumentException('Argument 2 should be a callable');
46+
}
47+
3948
$this->transform = $transform;
4049
$this->reverseTransform = $reverseTransform;
4150
}
@@ -47,7 +56,7 @@ public function __construct(\Closure $transform, \Closure $reverseTransform)
4756
*
4857
* @return mixed The value in the transformed representation
4958
*
50-
* @throws UnexpectedTypeException when the argument is not a string
59+
* @throws UnexpectedTypeException when the argument is not of the expected type
5160
* @throws TransformationFailedException when the transformation fails
5261
*/
5362
public function transform($data)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Symfony\Component\Form\Tests;
4+
5+
use Symfony\Component\Form\CallbackTransformer;
6+
7+
class CallbackTransformerTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testTransform()
10+
{
11+
$transformer = new CallbackTransformer(
12+
function($value) { return $value.' has been transformed'; },
13+
function($value) { return $value.' has reversely been transformed'; }
14+
);
15+
16+
$this->assertEquals('foo has been transformed', $transformer->transform('foo'));
17+
$this->assertEquals('bar has reversely been transformed', $transformer->reverseTransform('bar'));
18+
}
19+
20+
/**
21+
* @dataProvider invalidCallbacksProvider
22+
*
23+
* @expectedException \InvalidArgumentException
24+
*/
25+
public function testConstructorWithInvalidCallbacks($transformCallback, $reverseTransformCallback)
26+
{
27+
new CallbackTransformer($transformCallback, $reverseTransformCallback);
28+
}
29+
30+
public function invalidCallbacksProvider()
31+
{
32+
return array(
33+
array( null, function(){} ),
34+
array( function(){}, null ),
35+
);
36+
}
37+
}

src/Symfony/Component/Process/Tests/AbstractProcessTest.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,23 @@ public function testGetErrorOutput()
260260

261261
public function testGetIncrementalErrorOutput()
262262
{
263-
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { usleep(100000); file_put_contents(\'php://stderr\', \'ERROR\'); $n++; }')));
263+
// use a lock file to toggle between writing ("W") and reading ("R") the
264+
// error stream
265+
$lock = tempnam(sys_get_temp_dir(), get_class($this).'Lock');
266+
file_put_contents($lock, 'W');
267+
268+
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { if (\'W\' === file_get_contents('.var_export($lock, true).')) { file_put_contents(\'php://stderr\', \'ERROR\'); $n++; file_put_contents('.var_export($lock, true).', \'R\'); } usleep(100); }')));
264269

265270
$p->start();
266271
while ($p->isRunning()) {
267-
$this->assertLessThanOrEqual(1, preg_match_all('/ERROR/', $p->getIncrementalErrorOutput(), $matches));
268-
usleep(20000);
272+
if ('R' === file_get_contents($lock)) {
273+
$this->assertLessThanOrEqual(1, preg_match_all('/ERROR/', $p->getIncrementalErrorOutput(), $matches));
274+
file_put_contents($lock, 'W');
275+
}
276+
usleep(100);
269277
}
278+
279+
unlink($lock);
270280
}
271281

272282
public function testFlushErrorOutput()
@@ -280,21 +290,31 @@ public function testFlushErrorOutput()
280290

281291
public function testGetOutput()
282292
{
283-
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n=0;while ($n<3) {echo \' foo \';$n++; usleep(500); }')));
293+
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { echo \' foo \'; $n++; }')));
284294

285295
$p->run();
286296
$this->assertEquals(3, preg_match_all('/foo/', $p->getOutput(), $matches));
287297
}
288298

289299
public function testGetIncrementalOutput()
290300
{
291-
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n=0;while ($n<3) { echo \' foo \'; usleep(50000); $n++; }')));
301+
// use a lock file to toggle between writing ("W") and reading ("R") the
302+
// output stream
303+
$lock = tempnam(sys_get_temp_dir(), get_class($this).'Lock');
304+
file_put_contents($lock, 'W');
305+
306+
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { if (\'W\' === file_get_contents('.var_export($lock, true).')) { echo \' foo \'; $n++; file_put_contents('.var_export($lock, true).', \'R\'); } usleep(100); }')));
292307

293308
$p->start();
294309
while ($p->isRunning()) {
295-
$this->assertLessThanOrEqual(1, preg_match_all('/foo/', $p->getIncrementalOutput(), $matches));
296-
usleep(20000);
310+
if ('R' === file_get_contents($lock)) {
311+
$this->assertLessThanOrEqual(1, preg_match_all('/foo/', $p->getIncrementalOutput(), $matches));
312+
file_put_contents($lock, 'W');
313+
}
314+
usleep(100);
297315
}
316+
317+
unlink($lock);
298318
}
299319

300320
public function testFlushOutput()

0 commit comments

Comments
 (0)