Skip to content

Commit a96e829

Browse files
committed
Merge pull request #1246 from FriendsOfSymfony/allow_symfony_3_0
allow Symfony 3.0
2 parents bc26a6a + ed752bb commit a96e829

File tree

8 files changed

+138
-52
lines changed

8 files changed

+138
-52
lines changed

.travis.yml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ php:
55
- 5.4
66
- 5.5
77
- 5.6
8-
- nightly
8+
- 7.0
99
- hhvm
1010

1111
sudo: false
@@ -26,16 +26,14 @@ matrix:
2626
env: COMPOSER_FLAGS="--prefer-lowest" SYMFONY_DEPRECATIONS_HELPER=weak
2727
- php: 5.5
2828
env: SYMFONY_VERSION='2.3.* symfony/expression-language:2.4.*' SYMFONY_DEPRECATIONS_HELPER=weak
29-
- php: 5.5
30-
env: SYMFONY_VERSION=2.4.*
3129
- php: 5.5
3230
env: SYMFONY_VERSION=2.6.*
3331
- php: 5.5
34-
env: SYMFONY_VERSION='2.7.*'
32+
env: SYMFONY_VERSION=2.7.*
33+
- php: 5.5
34+
env: SYMFONY_VERSION=2.8.*
3535
- php: 5.5
36-
env: SYMFONY_VERSION='2.8.*@dev symfony/security-acl:2.8.*@dev'
37-
allow_failures:
38-
- php: nightly
36+
env: SYMFONY_VERSION=3.0.*
3937

4038
before_install:
4139
- composer self-update

Controller/ExceptionController.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
use FOS\RestBundle\Util\StopFormatListenerException;
1515
use FOS\RestBundle\View\ExceptionWrapperHandlerInterface;
1616
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
17+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
18+
use Symfony\Component\DependencyInjection\ContainerInterface;
1719
use Symfony\Component\HttpKernel\Exception\FlattenException as HttpFlattenException;
1820
use Symfony\Component\Debug\Exception\FlattenException as DebugFlattenException;
19-
use Symfony\Component\DependencyInjection\ContainerAware;
2021
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
2122
use Symfony\Component\HttpFoundation\Request;
2223
use Symfony\Component\HttpFoundation\Response;
@@ -28,8 +29,23 @@
2829
/**
2930
* Custom ExceptionController that uses the view layer and supports HTTP response status code mapping.
3031
*/
31-
class ExceptionController extends ContainerAware
32+
class ExceptionController implements ContainerAwareInterface
3233
{
34+
/**
35+
* @var ContainerInterface
36+
*/
37+
protected $container;
38+
39+
/**
40+
* Sets the Container associated with this Controller.
41+
*
42+
* @param ContainerInterface $container A ContainerInterface instance
43+
*/
44+
public function setContainer(ContainerInterface $container = null)
45+
{
46+
$this->container = $container;
47+
}
48+
3349
/**
3450
* Creates a new ExceptionWrapper instance that can be overwritten by a custom
3551
* ExceptionController class.
@@ -87,7 +103,7 @@ public function showAction(Request $request, $exception, DebugLoggerInterface $l
87103
return $this->createPlainResponse($message, Codes::HTTP_NOT_ACCEPTABLE, $exception->getHeaders());
88104
}
89105

90-
$currentContent = $this->getAndCleanOutputBuffering();
106+
$currentContent = $this->getAndCleanOutputBuffering($request);
91107
$code = $this->getStatusCode($exception);
92108
$viewHandler = $this->container->get('fos_rest.view_handler');
93109
$parameters = $this->getParameters($viewHandler, $currentContent, $code, $exception, $logger, $format);
@@ -137,11 +153,13 @@ private function createPlainResponse($content, $status = 200, $headers = array()
137153
* This code comes from Symfony and should be synchronized on a regular basis
138154
* see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php
139155
*
156+
* @param Request $request
157+
*
140158
* @return string
141159
*/
142-
protected function getAndCleanOutputBuffering()
160+
protected function getAndCleanOutputBuffering(Request $request)
143161
{
144-
$startObLevel = $this->container->get('request')->headers->get('X-Php-Ob-Level', -1);
162+
$startObLevel = $request->headers->get('X-Php-Ob-Level', -1);
145163

146164
// ob_get_level() never returns 0 on some Windows configurations, so if
147165
// the level is the same two times in a row, the loop should be stopped.

Decoder/ContainerDecoderProvider.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,23 @@
1111

1212
namespace FOS\RestBundle\Decoder;
1313

14-
use Symfony\Component\DependencyInjection\ContainerAware;
14+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15+
use Symfony\Component\DependencyInjection\ContainerInterface;
1516

1617
/**
1718
* Provides encoders through the Symfony2 DIC.
1819
*
1920
* @author Igor Wiedler <[email protected]>
2021
*/
21-
class ContainerDecoderProvider extends ContainerAware implements DecoderProviderInterface
22+
class ContainerDecoderProvider implements DecoderProviderInterface, ContainerAwareInterface
2223
{
2324
private $decoders;
2425

26+
/**
27+
* @var ContainerInterface
28+
*/
29+
protected $container;
30+
2531
/**
2632
* Constructor.
2733
*
@@ -40,6 +46,16 @@ public function supports($format)
4046
return isset($this->decoders[$format]);
4147
}
4248

49+
/**
50+
* Sets the Container associated with this Controller.
51+
*
52+
* @param ContainerInterface $container A ContainerInterface instance
53+
*/
54+
public function setContainer(ContainerInterface $container = null)
55+
{
56+
$this->container = $container;
57+
}
58+
4359
/**
4460
* {@inheritdoc}
4561
*/

Request/ParamFetcher.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
use FOS\RestBundle\Controller\Annotations\RequestParam;
1717
use FOS\RestBundle\Util\ViolationFormatterInterface;
1818
use Doctrine\Common\Util\ClassUtils;
19-
use Symfony\Component\DependencyInjection\ContainerAware;
19+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
20+
use Symfony\Component\DependencyInjection\ContainerInterface;
2021
use Symfony\Component\HttpFoundation\Request;
2122
use Symfony\Component\HttpFoundation\RequestStack;
2223
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -33,7 +34,7 @@
3334
* @author Jordi Boggiano <[email protected]>
3435
* @author Boris Guéry <[email protected]>
3536
*/
36-
class ParamFetcher extends ContainerAware implements ParamFetcherInterface
37+
class ParamFetcher implements ParamFetcherInterface, ContainerAwareInterface
3738
{
3839
private $paramReader;
3940
private $requestStack;
@@ -46,6 +47,11 @@ class ParamFetcher extends ContainerAware implements ParamFetcherInterface
4647
*/
4748
private $controller;
4849

50+
/**
51+
* @var ContainerInterface
52+
*/
53+
protected $container;
54+
4955
/**
5056
* Initializes fetcher.
5157
*
@@ -77,6 +83,16 @@ public function __construct(ParamReader $paramReader, $requestStack = null, Viol
7783
}
7884
}
7985

86+
/**
87+
* Sets the Container associated with this Controller.
88+
*
89+
* @param ContainerInterface $container A ContainerInterface instance
90+
*/
91+
public function setContainer(ContainerInterface $container = null)
92+
{
93+
$this->container = $container;
94+
}
95+
8096
/**
8197
* {@inheritdoc}
8298
*/

Tests/Request/ParamFetcherTest.php

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class ParamFetcherTest extends \PHPUnit_Framework_TestCase
4444
*/
4545
private $validator;
4646

47+
/**
48+
* @var string
49+
*/
50+
private $validatorMethod;
51+
4752
/**
4853
* @var \PHPUnit_Framework_MockObject_MockObject
4954
*/
@@ -143,7 +148,14 @@ public function setup()
143148
->method('read')
144149
->will($this->returnValue($annotations));
145150

146-
$this->validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
151+
if (interface_exists('Symfony\Component\Validator\Validator\ValidatorInterface')) {
152+
$this->validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
153+
$this->validatorMethod = 'validate';
154+
} else {
155+
$this->validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
156+
$this->validatorMethod = 'validateValue';
157+
}
158+
147159
$this->violationFormatter = $this->getMock('FOS\RestBundle\Util\ViolationFormatterInterface');
148160
}
149161

@@ -188,7 +200,7 @@ public function testValidatesConfiguredParam($param, $expected, $expectedAll, $q
188200
if (null !== $callback) {
189201
$self = $this;
190202
$validator = $this->validator;
191-
$callback($validator, $self);
203+
$callback($validator, $this->validatorMethod, $self);
192204
}
193205

194206
$queryFetcher = $this->getParamFetcher($query, $request);
@@ -226,17 +238,17 @@ public static function validatesConfiguredParamDataProvider()
226238
array('foo' => '1', 'bar' => '1', 'baz' => '4', 'buzz' => array(1), 'boo' => array(), 'boozz' => null, 'biz' => null, 'arr' => array(), 'arr_null_strict' => array(), 'moo' => null, 'i_cant_be_with_moo' => null),
227239
array('foo' => 'bar'),
228240
array('bar' => '1', 'baz' => '4', 'arr' => array()),
229-
function (\PHPUnit_Framework_MockObject_MockObject $validator, \PHPUnit_Framework_TestCase $self) {
241+
function (\PHPUnit_Framework_MockObject_MockObject $validator, $validatorMethod, \PHPUnit_Framework_TestCase $self) {
230242
$errors = new ConstraintViolationList(array(
231243
new ConstraintViolation('expected error', null, array(), null, null, null),
232244
));
233245

234246
$validator->expects($self->at(0))
235-
->method('validateValue')
247+
->method($validatorMethod)
236248
->with('bar', new Regex(array('pattern' => '#^\\d+$#xsu', 'message' => "Query parameter value 'bar', does not match requirements '\\d+'")), null)
237249
->will($self->returnValue($errors));
238250
$validator->expects($self->at(1))
239-
->method('validateValue')
251+
->method($validatorMethod)
240252
->with('bar', new Regex(array('pattern' => '#^\\d+$#xsu', 'message' => "Query parameter value 'bar', does not match requirements '\\d+'")), null)
241253
->will($self->returnValue($errors));
242254

@@ -277,18 +289,18 @@ function (\PHPUnit_Framework_MockObject_MockObject $validator, \PHPUnit_Framewor
277289
array('foo' => '1', 'bar' => '1', 'baz' => '4', 'buzz' => array(2, 1, 4), 'boo' => array(), 'boozz' => null, 'biz' => null, 'arr' => array(), 'arr_null_strict' => array(), 'moo' => null, 'i_cant_be_with_moo' => null),
278290
array('buzz' => array(2, 'invaliddata', 4)),
279291
array('bar' => '1', 'baz' => '4', 'arr' => array()),
280-
function (\PHPUnit_Framework_MockObject_MockObject $validator, \PHPUnit_Framework_TestCase $self) {
292+
function (\PHPUnit_Framework_MockObject_MockObject $validator, $validatorMethod, \PHPUnit_Framework_TestCase $self) {
281293
$errors = new ConstraintViolationList(array(
282294
new ConstraintViolation('expected error', null, array(), null, null, null),
283295
));
284296

285297
$validator->expects($self->at(1))
286-
->method('validateValue')
298+
->method($validatorMethod)
287299
->with('invaliddata', new Regex(array('pattern' => '#^\\d+$#xsu', 'message' => "Query parameter value 'invaliddata', does not match requirements '\\d+'")), null)
288300
->will($self->returnValue($errors));
289301

290302
$validator->expects($self->at(6))
291-
->method('validateValue')
303+
->method($validatorMethod)
292304
->with('invaliddata', new Regex(array('pattern' => '#^\\d+$#xsu', 'message' => "Query parameter value 'invaliddata', does not match requirements '\\d+'")), null)
293305
->will($self->returnValue($errors));
294306
},
@@ -369,7 +381,7 @@ public function testValidatesConfiguredParamStrictly()
369381
));
370382

371383
$this->validator->expects($this->once())
372-
->method('validateValue')
384+
->method($this->validatorMethod)
373385
->with('354', $constraint)
374386
;
375387

@@ -400,7 +412,7 @@ public function testExceptionOnValidatesFailure($query, $request, $param, \Closu
400412
if (null !== $callback) {
401413
$self = $this;
402414
$validator = $this->validator;
403-
$callback($validator, $self);
415+
$callback($validator, $this->validatorMethod, $self);
404416
}
405417

406418
$queryFetcher = $this->getParamFetcher($query, $request);
@@ -448,18 +460,18 @@ public static function exceptionOnValidatesFailureDataProvider()
448460
array(),
449461
array('bar' => 'foo'),
450462
'bar',
451-
function (\PHPUnit_Framework_MockObject_MockObject $validator, \PHPUnit_Framework_TestCase $self) {
463+
function (\PHPUnit_Framework_MockObject_MockObject $validator, $validatorMethod, \PHPUnit_Framework_TestCase $self) {
452464
$errors = new ConstraintViolationList(array(
453465
new ConstraintViolation('expected error', null, array(), null, null, null),
454466
));
455467

456468
$validator->expects($self->at(0))
457-
->method('validateValue')
469+
->method($validatorMethod)
458470
->with('foo', new Regex(array('pattern' => '#^\\d+$#xsu', 'message' => "Request parameter value 'foo', does not match requirements '\\d+'")), null)
459471
->will($self->returnValue($errors));
460472

461473
$validator->expects($self->at(1))
462-
->method('validateValue')
474+
->method($validatorMethod)
463475
->with('foo', new Regex(array('pattern' => '#^\\d+$#xsu', 'message' => "Request parameter value 'foo', does not match requirements '\\d+'")), null)
464476
->will($self->returnValue($errors));
465477
},
@@ -468,13 +480,13 @@ function (\PHPUnit_Framework_MockObject_MockObject $validator, \PHPUnit_Framewor
468480
array(),
469481
array('baz' => 'foo'),
470482
'baz',
471-
function (\PHPUnit_Framework_MockObject_MockObject $validator, \PHPUnit_Framework_TestCase $self) {
483+
function (\PHPUnit_Framework_MockObject_MockObject $validator, $validatorMethod, \PHPUnit_Framework_TestCase $self) {
472484
$errors = new ConstraintViolationList(array(
473485
new ConstraintViolation('expected error', null, array(), null, null, null),
474486
));
475487

476488
$validator->expects($self->at(0))
477-
->method('validateValue')
489+
->method($validatorMethod)
478490
->with('foo', new Regex(array('pattern' => '#^\\d?$#xsu', 'message' => "Request parameter value 'foo', does not match requirements '\\d?'")), null)
479491
->will($self->returnValue($errors));
480492
},
@@ -551,7 +563,7 @@ public function testConstraintThrowExceptionInStrictMode()
551563
));
552564

553565
$this->validator->expects($this->once())
554-
->method('validateValue')
566+
->method($this->validatorMethod)
555567
->with('foobar', $this->constraint)
556568
->will($this->returnValue($errors));
557569

@@ -601,7 +613,7 @@ public function testConstraintReturnDefaultInSafeMode()
601613
$violation1->expects($this->never())->method('getMessage');
602614

603615
$this->validator->expects($this->once())
604-
->method('validateValue')
616+
->method($this->validatorMethod)
605617
->with('foobar', $this->constraint)
606618
->will($this->returnValue(array($violation1)));
607619

@@ -636,7 +648,7 @@ public function testConstraintOk()
636648
}
637649

638650
$this->validator->expects($this->once())
639-
->method('validateValue')
651+
->method($this->validatorMethod)
640652
->with('foobar', $this->constraint)
641653
->will($this->returnValue(array()));
642654

@@ -671,7 +683,7 @@ public function testDeepArrayAllowedWithConstraint()
671683
}
672684

673685
$this->validator->expects($this->once())
674-
->method('validateValue')
686+
->method($this->validatorMethod)
675687
->with(array('foo' => array('b', 'a', 'r')), $this->constraint)
676688
->will($this->returnValue(array()));
677689

@@ -786,7 +798,7 @@ public function testCustomErrorMessage()
786798
));
787799

788800
$this->validator->expects($this->once())
789-
->method('validateValue')
801+
->method($this->validatorMethod)
790802
->with('foobar', $constraint)
791803
->will($this->returnValue($errors));
792804

Tests/Routing/Loader/RestRouteLoaderTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ public function testAnnotatedUsersFixture()
102102
$route = $collection->get($name);
103103

104104
// Symfony sets _method to keep BC, should be removed in 3.0
105-
$params['requirements']['_method'] = implode('|', $params['methods']);
105+
if (method_exists('\Symfony\Component\Routing\Route', 'getPattern')) {
106+
$params['requirements']['_method'] = implode('|', $params['methods']);
107+
} elseif (!isset($params['requirements'])) {
108+
$params['requirements'] = array();
109+
}
106110

107111
$this->assertNotNull($route, "no route found for '$name'");
108112
$this->assertEquals($params['path'], $route->getPath(), 'path failed to match for '.$name);
@@ -140,8 +144,12 @@ public function testAnnotatedConditionalUsersFixture()
140144
foreach ($etalonRoutes as $name => $params) {
141145
$route = $collection->get($name);
142146

143-
// Symfony sets _method to keep BC, should be removed with 3.0
144-
$params['requirements']['_method'] = implode('|', $params['methods']);
147+
// Symfony sets _method to keep BC, should be removed in 3.0
148+
if (method_exists('\Symfony\Component\Routing\Route', 'getPattern')) {
149+
$params['requirements']['_method'] = implode('|', $params['methods']);
150+
} elseif (!isset($params['requirements'])) {
151+
$params['requirements'] = array();
152+
}
145153

146154
$this->assertNotNull($route, "no route found for '$name'");
147155
$this->assertEquals($params['path'], $route->getPath(), 'path failed to match for '.$name);

0 commit comments

Comments
 (0)