Skip to content

Commit 04ca6a2

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: fixed test [Request] Ignore invalid IP addresses sent by proxies Throw for missing container extensions [TwigBridge] add missing unit tests (AppVariable) Able to load big xml files with DomCrawler fixed typo [Form] Fix constraints could be null if not set [Finder] Check PHP version before applying a workaround for a PHP bug fixed CS add defaultNull to version sort bundles in config:dump-reference command Fixer findings. [Translation][Writer] avoid calling setBackup if the dumper is not an instance of FileDumper. [FrameworkBundle] Compute the kernel root hash only one time
2 parents 65d0bb3 + 0fe08a8 commit 04ca6a2

File tree

23 files changed

+296
-27
lines changed

23 files changed

+296
-27
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php
2+
3+
namespace Symfony\Bridge\Twig\Tests;
4+
5+
use Symfony\Bridge\Twig\AppVariable;
6+
use Symfony\Component\HttpFoundation\Request;
7+
use Symfony\Component\HttpFoundation\Session\Session;
8+
9+
class AppVariableTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @var AppVariable
13+
*/
14+
protected $appVariable;
15+
16+
public function setUp()
17+
{
18+
$this->appVariable = new AppVariable();
19+
}
20+
21+
/**
22+
* @dataProvider debugDataProvider
23+
*/
24+
public function testDebug($debugFlag)
25+
{
26+
$this->appVariable->setDebug($debugFlag);
27+
28+
$this->assertEquals($debugFlag, $this->appVariable->getDebug());
29+
}
30+
31+
public function debugDataProvider()
32+
{
33+
return array(
34+
'debug on' => array(true),
35+
'debug off' => array(false),
36+
);
37+
}
38+
39+
public function testEnvironment()
40+
{
41+
$this->appVariable->setEnvironment('dev');
42+
43+
$this->assertEquals('dev', $this->appVariable->getEnvironment());
44+
}
45+
46+
public function testGetSession()
47+
{
48+
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
49+
$request->method('getSession')->willReturn($session = new Session());
50+
51+
$this->setRequestStack($request);
52+
53+
$this->assertEquals($session, $this->appVariable->getSession());
54+
}
55+
56+
public function testGetSessionWithNoRequest()
57+
{
58+
$this->setRequestStack(null);
59+
60+
$this->assertNull($this->appVariable->getSession());
61+
}
62+
63+
public function testGetRequest()
64+
{
65+
$this->setRequestStack($request = new Request());
66+
67+
$this->assertEquals($request, $this->appVariable->getRequest());
68+
}
69+
70+
public function testGetUser()
71+
{
72+
$this->setTokenStorage($user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'));
73+
74+
$this->assertEquals($user, $this->appVariable->getUser());
75+
}
76+
77+
public function testGetUserWithUsernameAsTokenUser()
78+
{
79+
$this->setTokenStorage($user = 'username');
80+
81+
$this->assertNull($this->appVariable->getUser());
82+
}
83+
84+
public function testGetUserWithNoToken()
85+
{
86+
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
87+
$this->appVariable->setTokenStorage($tokenStorage);
88+
89+
$this->assertNull($this->appVariable->getUser());
90+
}
91+
92+
/**
93+
* @expectedException \RuntimeException
94+
*/
95+
public function testEnvironmentNotSet()
96+
{
97+
$this->appVariable->getEnvironment();
98+
}
99+
100+
/**
101+
* @expectedException \RuntimeException
102+
*/
103+
public function testDebugNotSet()
104+
{
105+
$this->appVariable->getDebug();
106+
}
107+
108+
/**
109+
* @expectedException \RuntimeException
110+
*/
111+
public function testGetUserWithTokenStorageNotSet()
112+
{
113+
$this->appVariable->getUser();
114+
}
115+
116+
/**
117+
* @expectedException \RuntimeException
118+
*/
119+
public function testGetRequestWithRequestStackNotSet()
120+
{
121+
$this->appVariable->getRequest();
122+
}
123+
124+
/**
125+
* @expectedException \RuntimeException
126+
*/
127+
public function testGetSessionWithRequestStackNotSet()
128+
{
129+
$this->appVariable->getSession();
130+
}
131+
132+
protected function setRequestStack($request)
133+
{
134+
$requestStackMock = $this->getMock('Symfony\Component\HttpFoundation\RequestStack');
135+
$requestStackMock->method('getCurrentRequest')->willReturn($request);
136+
137+
$this->appVariable->setRequestStack($requestStackMock);
138+
}
139+
140+
protected function setTokenStorage($user)
141+
{
142+
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
143+
$this->appVariable->setTokenStorage($tokenStorage);
144+
145+
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
146+
$tokenStorage->method('getToken')->willReturn($token);
147+
148+
$token->method('getUser')->willReturn($user);
149+
}
150+
}

src/Symfony/Bridge/Twig/Tests/Node/TransNodeTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function testCompileStrict()
3636
trim($compiler->compile($node)->getSource())
3737
);
3838
}
39+
3940
protected function getVariableGetterWithoutStrictCheck($name)
4041
{
4142
if (PHP_VERSION_ID >= 50400) {

src/Symfony/Bundle/FrameworkBundle/Command/AbstractConfigCommand.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ protected function findExtension($name)
5454
$bundles = $this->initializeBundles();
5555
foreach ($bundles as $bundle) {
5656
if ($name === $bundle->getName()) {
57+
if (!$bundle->getContainerExtension()) {
58+
throw new \LogicException(sprintf('Bundle "%s" does not have a container extension.', $name));
59+
}
60+
5761
return $bundle->getContainerExtension();
5862
}
5963

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
596596
->fixXmlConfig('base_url')
597597
->children()
598598
->scalarNode('version')
599+
->defaultNull()
599600
->beforeNormalization()
600601
->ifTrue(function ($v) { return '' === $v; })
601602
->then(function ($v) { return; })

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class FrameworkExtension extends Extension
3838
private $translationConfigEnabled = false;
3939
private $sessionConfigEnabled = false;
4040

41+
/**
42+
* @var string|null
43+
*/
44+
private $kernelRootHash;
45+
4146
/**
4247
* Responds to the app.config configuration parameter.
4348
*
@@ -792,7 +797,7 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
792797
if (isset($config['cache'])) {
793798
$container->setParameter(
794799
'validator.mapping.cache.prefix',
795-
'validator_'.hash('sha256', $container->getParameter('kernel.root_dir'))
800+
'validator_'.$this->getKernelRootHash($container)
796801
);
797802

798803
$validatorBuilder->addMethodCall('setMetadataCache', array(new Reference($config['cache'])));
@@ -981,7 +986,7 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
981986
if (isset($config['cache']) && $config['cache']) {
982987
$container->setParameter(
983988
'serializer.mapping.cache.prefix',
984-
'serializer_'.hash('sha256', $container->getParameter('kernel.root_dir'))
989+
'serializer_'.$this->getKernelRootHash($container)
985990
);
986991

987992
$container->getDefinition('serializer.mapping.class_metadata_factory')->replaceArgument(
@@ -1016,6 +1021,22 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild
10161021
}
10171022
}
10181023

1024+
/**
1025+
* Gets a hash of the kernel root directory.
1026+
*
1027+
* @param ContainerBuilder $container
1028+
*
1029+
* @return string
1030+
*/
1031+
private function getKernelRootHash(ContainerBuilder $container)
1032+
{
1033+
if (!$this->kernelRootHash) {
1034+
$this->kernelRootHash = hash('sha256', $container->getParameter('kernel.root_dir'));
1035+
}
1036+
1037+
return $this->kernelRootHash;
1038+
}
1039+
10191040
/**
10201041
* Returns the base path for the XSD files.
10211042
*

src/Symfony/Component/Console/Tests/Descriptor/AbstractDescriptorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function getDescribeApplicationTestData()
8383
}
8484

8585
abstract protected function getDescriptor();
86+
8687
abstract protected function getFormat();
8788

8889
private function getDescriptionTestData(array $objects)

src/Symfony/Component/CssSelector/Tests/Node/AbstractNodeTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ public function testSpecificityValue(NodeInterface $node, $value)
2828
}
2929

3030
abstract public function getToStringConversionTestData();
31+
3132
abstract public function getSpecificityValueTestData();
3233
}

src/Symfony/Component/CssSelector/Tests/Parser/Handler/AbstractHandlerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public function testDontHandleValue($value)
4343
}
4444

4545
abstract public function getHandleValueTestData();
46+
4647
abstract public function getDontHandleValueTestData();
48+
4749
abstract protected function generateHandler();
4850

4951
protected function assertStreamEmpty(TokenStream $stream)

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public function addXmlContent($content, $charset = 'UTF-8')
230230
$dom->validateOnParse = true;
231231

232232
if ('' !== trim($content)) {
233-
@$dom->loadXML($content, LIBXML_NONET);
233+
@$dom->loadXML($content, LIBXML_NONET | (defined('LIBXML_PARSEHUGE') ? LIBXML_PARSEHUGE : 0));
234234
}
235235

236236
libxml_use_internal_errors($internalErrors);

src/Symfony/Component/Finder/Iterator/FilterIterator.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
namespace Symfony\Component\Finder\Iterator;
1313

1414
/**
15-
* This iterator just overrides the rewind method in order to correct a PHP bug.
15+
* This iterator just overrides the rewind method in order to correct a PHP bug,
16+
* which existed before version 5.5.23/5.6.7.
1617
*
17-
* @see https://bugs.php.net/bug.php?id=49104
18+
* @see https://bugs.php.net/68557
1819
*
1920
* @author Alex Bogomazov
2021
*/
@@ -28,18 +29,19 @@ abstract class FilterIterator extends \FilterIterator
2829
*/
2930
public function rewind()
3031
{
32+
if (PHP_VERSION_ID > 50607 || (PHP_VERSION_ID > 50523 && PHP_VERSION_ID < 50600)) {
33+
parent::rewind();
34+
35+
return;
36+
}
37+
3138
$iterator = $this;
3239
while ($iterator instanceof \OuterIterator) {
3340
$innerIterator = $iterator->getInnerIterator();
3441

35-
if ($innerIterator instanceof RecursiveDirectoryIterator) {
36-
if ($innerIterator->isRewindable()) {
37-
$innerIterator->next();
38-
$innerIterator->rewind();
39-
}
40-
} elseif ($iterator->getInnerIterator() instanceof \FilesystemIterator) {
41-
$iterator->getInnerIterator()->next();
42-
$iterator->getInnerIterator()->rewind();
42+
if ($innerIterator instanceof \FilesystemIterator) {
43+
$innerIterator->next();
44+
$innerIterator->rewind();
4345
}
4446
$iterator = $iterator->getInnerIterator();
4547
}

src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,10 @@ public function rewind()
118118
return;
119119
}
120120

121-
// @see https://bugs.php.net/bug.php?id=49104
122-
parent::next();
121+
// @see https://bugs.php.net/68557
122+
if (PHP_VERSION_ID < 50523 || PHP_VERSION_ID >= 50600 && PHP_VERSION_ID < 50607) {
123+
parent::next();
124+
}
123125

124126
parent::rewind();
125127
}

src/Symfony/Component/Finder/Tests/FinderTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ public function testNotContainsOnDirectory()
468468
* Searching in multiple locations involves AppendIterator which does an unnecessary rewind which leaves FilterIterator
469469
* with inner FilesystemIterator in an invalid state.
470470
*
471-
* @see https://bugs.php.net/bug.php?id=49104
471+
* @see https://bugs.php.net/68557
472472
*/
473473
public function testMultipleLocations()
474474
{
@@ -478,8 +478,12 @@ public function testMultipleLocations()
478478
);
479479

480480
// it is expected that there are test.py test.php in the tmpDir
481-
$finder = $this->buildFinder();
482-
$finder->in($locations)->depth('< 1')->name('test.php');
481+
$finder = new Finder();
482+
$finder->in($locations)
483+
// the default flag IGNORE_DOT_FILES fixes the problem indirectly
484+
// so we set it to false for better isolation
485+
->ignoreDotFiles(false)
486+
->depth('< 1')->name('test.php');
483487

484488
$this->assertCount(1, $finder);
485489
}
@@ -489,7 +493,7 @@ public function testMultipleLocations()
489493
* AppendIterator which does an unnecessary rewind which leaves
490494
* FilterIterator with inner FilesystemIterator in an invalid state.
491495
*
492-
* @see https://bugs.php.net/bug.php?id=49104
496+
* @see https://bugs.php.net/68557
493497
*/
494498
public function testMultipleLocationsWithSubDirectories()
495499
{

src/Symfony/Component/Finder/Tests/Iterator/FilterIteratorTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ public function testFilterFilesystemIterators()
4343
++$c;
4444
}
4545

46-
// This would fail with \FilterIterator but works with Symfony\Component\Finder\Iterator\FilterIterator
47-
// see https://bugs.php.net/bug.php?id=49104
46+
// This would fail in php older than 5.5.23/5.6.7 with \FilterIterator
47+
// but works with Symfony\Component\Finder\Iterator\FilterIterator
48+
// see https://bugs.php.net/68557
4849
$this->assertEquals(1, $c);
4950
}
5051
}

src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function validate($form, Constraint $constraint)
6262

6363
// Validate the data against the constraints defined
6464
// in the form
65-
$constraints = $config->getOption('constraints');
65+
$constraints = $config->getOption('constraints', array());
6666
foreach ($constraints as $constraint) {
6767
// For the "Valid" constraint, validate the data in all groups
6868
if ($constraint instanceof Valid) {

src/Symfony/Component/Form/Tests/Extension/HttpFoundation/HttpFoundationRequestHandlerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function testRequestShouldNotBeNull()
2828
{
2929
$this->requestHandler->handleRequest($this->getMockForm('name', 'GET'));
3030
}
31+
3132
/**
3233
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
3334
*/

src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ public function testDontValidateIfParentWithoutCascadeValidation()
176176
$this->assertNoViolation();
177177
}
178178

179+
public function testMissingConstraintIndex()
180+
{
181+
$object = new \stdClass();
182+
$form = new FormBuilder('name', '\stdClass', $this->dispatcher, $this->factory);
183+
$form = $form->setData($object)->getForm();
184+
185+
$this->validator->validate($form, new Form());
186+
187+
$this->assertNoViolation();
188+
}
189+
179190
public function testValidateConstraintsEvenIfNoCascadeValidation()
180191
{
181192
$object = $this->getMock('\stdClass');

0 commit comments

Comments
 (0)