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

Skip to content

Commit 84229f8

Browse files
committed
Merge branch '3.1'
* 3.1: [TwigBridge] removed Twig null nodes (deprecated as of Twig 1.25) Make redis host configurable in tests [Console] Fix empty optionnal options with = separator in argv
2 parents 36c399f + 826fc39 commit 84229f8

17 files changed

+108
-37
lines changed

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<env name="DUMP_STRING_LENGTH" value="" />
1616
<env name="LDAP_HOST" value="127.0.0.1" />
1717
<env name="LDAP_PORT" value="3389" />
18+
<env name="REDIS_HOST" value="localhost" />
1819
</php>
1920

2021
<testsuites>

src/Symfony/Bridge/Twig/Node/DumpNode.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ class DumpNode extends \Twig_Node
2020

2121
public function __construct($varPrefix, \Twig_Node $values = null, $lineno, $tag = null)
2222
{
23-
parent::__construct(array('values' => $values), array(), $lineno, $tag);
23+
$nodes = array();
24+
if (null !== $values) {
25+
$nodes['values'] = $values;
26+
}
27+
28+
parent::__construct($nodes, array(), $lineno, $tag);
2429
$this->varPrefix = $varPrefix;
2530
}
2631

@@ -33,9 +38,7 @@ public function compile(\Twig_Compiler $compiler)
3338
->write("if (\$this->env->isDebug()) {\n")
3439
->indent();
3540

36-
$values = $this->getNode('values');
37-
38-
if (null === $values) {
41+
if (!$this->hasNode('values')) {
3942
// remove embedded templates (macros) from the context
4043
$compiler
4144
->write(sprintf('$%svars = array();'."\n", $this->varPrefix))
@@ -50,7 +53,7 @@ public function compile(\Twig_Compiler $compiler)
5053
->write("}\n")
5154
->addDebugInfo($this)
5255
->write(sprintf('\Symfony\Component\VarDumper\VarDumper::dump($%svars);'."\n", $this->varPrefix));
53-
} elseif (1 === $values->count()) {
56+
} elseif (($values = $this->getNode('values')) && 1 === $values->count()) {
5457
$compiler
5558
->addDebugInfo($this)
5659
->write('\Symfony\Component\VarDumper\VarDumper::dump(')

src/Symfony/Bridge/Twig/Node/StopwatchNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
class StopwatchNode extends \Twig_Node
2020
{
21-
public function __construct(\Twig_Node $name, $body, \Twig_Node_Expression_AssignName $var, $lineno = 0, $tag = null)
21+
public function __construct(\Twig_Node $name, \Twig_Node $body, \Twig_Node_Expression_AssignName $var, $lineno = 0, $tag = null)
2222
{
2323
parent::__construct(array('body' => $body, 'name' => $name, 'var' => $var), array(), $lineno, $tag);
2424
}

src/Symfony/Bridge/Twig/Node/TransNode.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,21 @@ class TransNode extends \Twig_Node
1818
{
1919
public function __construct(\Twig_Node $body, \Twig_Node $domain = null, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, \Twig_Node_Expression $locale = null, $lineno = 0, $tag = null)
2020
{
21-
parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars, 'locale' => $locale), array(), $lineno, $tag);
21+
$nodes = array('body' => $body);
22+
if (null !== $domain) {
23+
$nodes['domain'] = $domain;
24+
}
25+
if (null !== $count) {
26+
$nodes['count'] = $count;
27+
}
28+
if (null !== $vars) {
29+
$nodes['vars'] = $vars;
30+
}
31+
if (null !== $locale) {
32+
$nodes['locale'] = $locale;
33+
}
34+
35+
parent::__construct($nodes, array(), $lineno, $tag);
2236
}
2337

2438
/**
@@ -30,15 +44,14 @@ public function compile(\Twig_Compiler $compiler)
3044
{
3145
$compiler->addDebugInfo($this);
3246

33-
$vars = $this->getNode('vars');
3447
$defaults = new \Twig_Node_Expression_Array(array(), -1);
35-
if ($vars instanceof \Twig_Node_Expression_Array) {
48+
if ($this->hasNode('vars') && ($vars = $this->getNode('vars')) instanceof \Twig_Node_Expression_Array) {
3649
$defaults = $this->getNode('vars');
3750
$vars = null;
3851
}
3952
list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults, (bool) $vars);
4053

41-
$method = null === $this->getNode('count') ? 'trans' : 'transChoice';
54+
$method = !$this->hasNode('count') ? 'trans' : 'transChoice';
4255

4356
$compiler
4457
->write('echo $this->env->getExtension(\'translator\')->getTranslator()->'.$method.'(')
@@ -47,7 +60,7 @@ public function compile(\Twig_Compiler $compiler)
4760

4861
$compiler->raw(', ');
4962

50-
if (null !== $this->getNode('count')) {
63+
if ($this->hasNode('count')) {
5164
$compiler
5265
->subcompile($this->getNode('count'))
5366
->raw(', ')
@@ -68,13 +81,13 @@ public function compile(\Twig_Compiler $compiler)
6881

6982
$compiler->raw(', ');
7083

71-
if (null === $this->getNode('domain')) {
84+
if (!$this->hasNode('domain')) {
7285
$compiler->repr('messages');
7386
} else {
7487
$compiler->subcompile($this->getNode('domain'));
7588
}
7689

77-
if (null !== $this->getNode('locale')) {
90+
if ($this->hasNode('locale')) {
7891
$compiler
7992
->raw(', ')
8093
->subcompile($this->getNode('locale'))
@@ -98,7 +111,7 @@ protected function compileString(\Twig_Node $body, \Twig_Node_Expression_Array $
98111
foreach ($matches[1] as $var) {
99112
$key = new \Twig_Node_Expression_Constant('%'.$var.'%', $body->getLine());
100113
if (!$vars->hasElement($key)) {
101-
if ('count' === $var && null !== $this->getNode('count')) {
114+
if ('count' === $var && $this->hasNode('count')) {
102115
$vars->addElement($this->getNode('count'), $key);
103116
} else {
104117
$varExpr = new \Twig_Node_Expression_Name($var, $body->getLine());

src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
7878
}
7979
}
8080
} elseif ($node instanceof TransNode) {
81-
if (null === $node->getNode('domain')) {
81+
if (!$node->hasNode('domain')) {
8282
$node->setNode('domain', $this->scope->get('domain'));
8383
}
8484
}

src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
7575
// extract trans nodes
7676
$this->messages[] = array(
7777
$node->getNode('body')->getAttribute('data'),
78-
$this->getReadDomainFromNode($node->getNode('domain')),
78+
$node->hasNode('domain') ? $this->getReadDomainFromNode($node->getNode('domain')) : null,
7979
);
8080
}
8181

@@ -122,12 +122,8 @@ private function getReadDomainFromArguments(\Twig_Node $arguments, $index)
122122
*
123123
* @return string|null
124124
*/
125-
private function getReadDomainFromNode(\Twig_Node $node = null)
125+
private function getReadDomainFromNode(\Twig_Node $node)
126126
{
127-
if (null === $node) {
128-
return;
129-
}
130-
131127
if ($node instanceof \Twig_Node_Expression_Constant) {
132128
return $node->getAttribute('value');
133129
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolsTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717

1818
class CachePoolsTest extends WebTestCase
1919
{
20+
protected function setUp()
21+
{
22+
$_SERVER['SYMFONY__REDIS_HOST'] = getenv('REDIS_HOST');
23+
}
24+
25+
protected function tearDown()
26+
{
27+
unset($_SERVER['SYMFONY__REDIS_HOST']);
28+
}
29+
2030
public function testCachePools()
2131
{
2232
$this->doTestCachePools(array(), FilesystemAdapter::class);
@@ -30,7 +40,7 @@ public function testRedisCachePools()
3040
try {
3141
$this->doTestCachePools(array('root_config' => 'redis_config.yml', 'environment' => 'redis_cache'), RedisAdapter::class);
3242
} catch (\PHPUnit_Framework_Error_Warning $e) {
33-
if (0 !== strpos($e->getMessage(), 'unable to connect to 127.0.0.1')) {
43+
if (0 !== strpos($e->getMessage(), 'unable to connect to')) {
3444
throw $e;
3545
}
3646
$this->markTestSkipped($e->getMessage());
@@ -50,7 +60,7 @@ public function testRedisCustomCachePools()
5060
try {
5161
$this->doTestCachePools(array('root_config' => 'redis_custom_config.yml', 'environment' => 'custom_redis_cache'), RedisAdapter::class);
5262
} catch (\PHPUnit_Framework_Error_Warning $e) {
53-
if (0 !== strpos($e->getMessage(), 'unable to connect to 127.0.0.1')) {
63+
if (0 !== strpos($e->getMessage(), 'unable to connect to')) {
5464
throw $e;
5565
}
5666
$this->markTestSkipped($e->getMessage());

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/CachePools/redis_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ imports:
44
framework:
55
cache:
66
app: cache.adapter.redis
7+
default_redis_provider: "redis://%redis_host%"
78
pools:
89
cache.pool1:
910
public: true

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/CachePools/redis_custom_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ services:
66
public: false
77
class: Redis
88
calls:
9-
- [connect, [127.0.0.1]]
9+
- [connect, ['%redis_host%']]
1010

1111
cache.app:
1212
parent: cache.adapter.redis

src/Symfony/Bundle/FrameworkBundle/phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
>
99
<php>
1010
<ini name="error_reporting" value="-1" />
11+
<env name="REDIS_HOST" value="localhost" />
1112
</php>
1213

1314
<testsuites>

src/Symfony/Component/Cache/Tests/Adapter/AbstractRedisAdapterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static function setupBeforeClass()
3333
if (!extension_loaded('redis')) {
3434
self::markTestSkipped('Extension redis required.');
3535
}
36-
if (!@((new \Redis())->connect('127.0.0.1'))) {
36+
if (!@((new \Redis())->connect(getenv('REDIS_HOST')))) {
3737
$e = error_get_last();
3838
self::markTestSkipped($e['message']);
3939
}

src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@ class PredisAdapterTest extends AbstractRedisAdapterTest
1919
public static function setupBeforeClass()
2020
{
2121
parent::setupBeforeClass();
22-
self::$redis = new \Predis\Client();
22+
self::$redis = new \Predis\Client(array('host' => getenv('REDIS_HOST')));
2323
}
2424

2525
public function testCreateConnection()
2626
{
27-
$redis = RedisAdapter::createConnection('redis://localhost/1', array('class' => \Predis\Client::class, 'timeout' => 3));
27+
$redisHost = getenv('REDIS_HOST');
28+
29+
$redis = RedisAdapter::createConnection('redis://'.$redisHost.'/1', array('class' => \Predis\Client::class, 'timeout' => 3));
2830
$this->assertInstanceOf(\Predis\Client::class, $redis);
2931

3032
$connection = $redis->getConnection();
3133
$this->assertInstanceOf(StreamConnection::class, $connection);
3234

3335
$params = array(
3436
'scheme' => 'tcp',
35-
'host' => 'localhost',
37+
'host' => $redisHost,
3638
'path' => '',
3739
'dbindex' => '1',
3840
'port' => 6379,

src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,28 @@ public static function setupBeforeClass()
1919
{
2020
parent::setupBeforeClass();
2121
self::$redis = new \Redis();
22-
self::$redis->connect('127.0.0.1');
22+
self::$redis->connect(getenv('REDIS_HOST'));
2323
}
2424

2525
public function testCreateConnection()
2626
{
27-
$redis = RedisAdapter::createConnection('redis://localhost');
27+
$redisHost = getenv('REDIS_HOST');
28+
29+
$redis = RedisAdapter::createConnection('redis://'.$redisHost);
2830
$this->assertInstanceOf(\Redis::class, $redis);
2931
$this->assertTrue($redis->isConnected());
3032
$this->assertSame(0, $redis->getDbNum());
3133

32-
$redis = RedisAdapter::createConnection('redis://localhost/2');
34+
$redis = RedisAdapter::createConnection('redis://'.$redisHost.'/2');
3335
$this->assertSame(2, $redis->getDbNum());
3436

35-
$redis = RedisAdapter::createConnection('redis://localhost', array('timeout' => 3));
37+
$redis = RedisAdapter::createConnection('redis://'.$redisHost, array('timeout' => 3));
3638
$this->assertEquals(3, $redis->getTimeout());
3739

38-
$redis = RedisAdapter::createConnection('redis://localhost?timeout=4');
40+
$redis = RedisAdapter::createConnection('redis://'.$redisHost.'?timeout=4');
3941
$this->assertEquals(4, $redis->getTimeout());
4042

41-
$redis = RedisAdapter::createConnection('redis://localhost', array('read_timeout' => 5));
43+
$redis = RedisAdapter::createConnection('redis://'.$redisHost, array('read_timeout' => 5));
4244
$this->assertEquals(5, $redis->getReadTimeout());
4345
}
4446

src/Symfony/Component/Cache/Tests/Adapter/RedisArrayAdapterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ public static function setupBeforeClass()
1919
if (!class_exists('RedisArray')) {
2020
self::markTestSkipped('The RedisArray class is required.');
2121
}
22-
self::$redis = new \RedisArray(array('localhost'), array('lazy_connect' => true));
22+
self::$redis = new \RedisArray(array(getenv('REDIS_HOST')), array('lazy_connect' => true));
2323
}
2424
}

src/Symfony/Component/Cache/phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
>
99
<php>
1010
<ini name="error_reporting" value="-1" />
11+
<env name="REDIS_HOST" value="localhost" />
1112
</php>
1213

1314
<testsuites>

src/Symfony/Component/Console/Input/ArgvInput.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ private function parseLongOption($token)
147147
$name = substr($token, 2);
148148

149149
if (false !== $pos = strpos($name, '=')) {
150-
$this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
150+
if (0 === strlen($value = substr($name, $pos + 1))) {
151+
array_unshift($this->parsed, null);
152+
}
153+
$this->addLongOption(substr($name, 0, $pos), $value);
151154
} else {
152155
$this->addLongOption($name, null);
153156
}
@@ -234,7 +237,7 @@ private function addLongOption($name, $value)
234237
if (isset($next[0]) && '-' !== $next[0]) {
235238
$value = $next;
236239
} elseif (empty($next)) {
237-
$value = '';
240+
$value = null;
238241
} else {
239242
array_unshift($this->parsed, $next);
240243
}

src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ public function provideOptions()
7171
array('foo' => 'bar'),
7272
'->parse() parses long options with a required value (with a space separator)',
7373
),
74+
array(
75+
array('cli.php', '--foo='),
76+
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
77+
array('foo' => null),
78+
'->parse() parses long options with optional value which is empty (with a = separator) as null',
79+
),
80+
array(
81+
array('cli.php', '--foo=', 'bar'),
82+
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)),
83+
array('foo' => null),
84+
'->parse() parses long options with optional value which is empty (with a = separator) followed by an argument',
85+
),
7486
array(
7587
array('cli.php', '-f'),
7688
array(new InputOption('foo', 'f')),
@@ -343,4 +355,30 @@ public function testParseSingleDashAsArgument()
343355
$input->bind(new InputDefinition(array(new InputArgument('file'))));
344356
$this->assertEquals(array('file' => '-'), $input->getArguments(), '->parse() parses single dash as an argument');
345357
}
358+
359+
public function testParseOptionWithValueOptionalGivenEmptyAndRequiredArgument()
360+
{
361+
$input = new ArgvInput(array('cli.php', '--foo=', 'bar'));
362+
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED))));
363+
$this->assertEquals(array('foo' => null), $input->getOptions(), '->parse() parses optional options with empty value as null');
364+
$this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->parse() parses required arguments');
365+
366+
$input = new ArgvInput(array('cli.php', '--foo=0', 'bar'));
367+
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED))));
368+
$this->assertEquals(array('foo' => '0'), $input->getOptions(), '->parse() parses optional options with empty value as null');
369+
$this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->parse() parses required arguments');
370+
}
371+
372+
public function testParseOptionWithValueOptionalGivenEmptyAndOptionalArgument()
373+
{
374+
$input = new ArgvInput(array('cli.php', '--foo=', 'bar'));
375+
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::OPTIONAL))));
376+
$this->assertEquals(array('foo' => null), $input->getOptions(), '->parse() parses optional options with empty value as null');
377+
$this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->parse() parses optional arguments');
378+
379+
$input = new ArgvInput(array('cli.php', '--foo=0', 'bar'));
380+
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::OPTIONAL))));
381+
$this->assertEquals(array('foo' => '0'), $input->getOptions(), '->parse() parses optional options with empty value as null');
382+
$this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->parse() parses optional arguments');
383+
}
346384
}

0 commit comments

Comments
 (0)