@@ -49,11 +49,6 @@ class ParamFetcherTest extends \PHPUnit_Framework_TestCase
4949 */
5050 private $ validatorMethod ;
5151
52- /**
53- * @var \PHPUnit_Framework_MockObject_MockObject
54- */
55- private $ violationFormatter ;
56-
5752 /**
5853 * @var \PHPUnit_Framework_MockObject_MockObject
5954 */
@@ -155,8 +150,6 @@ public function setup()
155150 $ this ->validator = $ this ->getMock ('Symfony\Component\Validator\ValidatorInterface ' );
156151 $ this ->validatorMethod = 'validateValue ' ;
157152 }
158-
159- $ this ->violationFormatter = $ this ->getMock ('FOS\RestBundle\Util\ViolationFormatterInterface ' );
160153 }
161154
162155 /**
@@ -180,7 +173,7 @@ public function getParamFetcher($query = array(), $request = array(), $attribute
180173 $ req = $ requestStack ;
181174 }
182175
183- return new ParamFetcher ($ this ->paramReader , $ req , $ this ->violationFormatter , $ this -> validator );
176+ return new ParamFetcher ($ this ->paramReader , $ req , $ this ->validator );
184177 }
185178
186179 /**
@@ -507,7 +500,7 @@ public function testExceptionOnRequestWithoutController()
507500 $ requestStack = new RequestStack ();
508501 $ requestStack ->push (new Request ());
509502
510- $ queryFetcher = new ParamFetcher ($ this ->paramReader , $ requestStack , $ this ->violationFormatter , $ this -> validator );
503+ $ queryFetcher = new ParamFetcher ($ this ->paramReader , $ requestStack , $ this ->validator );
511504 $ queryFetcher ->get ('none ' , '42 ' );
512505 }
513506
@@ -553,11 +546,13 @@ public function testKeyPrecedenceOverName()
553546
554547 /**
555548 * Test an Exception is thrown in strict mode.
549+ *
550+ * @group legacy
556551 */
557- public function testConstraintThrowExceptionInStrictMode ()
552+ public function testConstraintThrowsExceptionWithExpectedMessageInStrictModeIfViolationFormatterIsGiven ()
558553 {
559554 if (!class_exists ('Symfony\Component\HttpFoundation\RequestStack ' )) {
560- $ this ->markTestSkipped ('RequestStack unvailable . ' );
555+ $ this ->markTestSkipped ('RequestStack unavailable . ' );
561556 }
562557
563558 $ errors = new ConstraintViolationList (array (
@@ -586,16 +581,56 @@ public function testConstraintThrowExceptionInStrictMode()
586581 ->method ('read ' )
587582 ->will ($ this ->returnValue (array ('bizoo ' => $ param )));
588583
589- $ this ->violationFormatter ->expects ($ this ->once ())
584+ $ violationFormatter = $ this ->getMock ('FOS\RestBundle\Util\ViolationFormatterInterface ' );
585+ $ violationFormatter ->expects ($ this ->once ())
590586 ->method ('formatList ' )
591587 ->will ($ this ->returnValue ('foobar ' ));
592588
593589 $ this ->setExpectedException (
594- '\\ Symfony \\ Component \\ HttpKernel \\ Exception \\ BadRequestHttpException ' ,
595- " Parameter \" bizoo \" of value \"\" violated a constraint \" expected message 1 \"\n Parameter \" bizoo \" of value \"\" violated a constraint \" expected message 2 \""
590+ '\FOS\RestBundle\ Exception\InvalidParameterException ' ,
591+ ' foobar '
596592 );
597593
598- $ queryFetcher = new ParamFetcher ($ reader , $ requestStack , $ this ->violationFormatter , $ this ->validator );
594+ $ queryFetcher = new ParamFetcher ($ reader , $ requestStack , $ violationFormatter , $ this ->validator );
595+ $ queryFetcher ->setController ($ this ->controller );
596+ $ queryFetcher ->get ('bizoo ' );
597+ }
598+
599+ /**
600+ * @expectedException \FOS\RestBundle\Exception\InvalidParameterException
601+ */
602+ public function testConstraintThrowsExceptionInStrictMode ()
603+ {
604+ if (!class_exists ('Symfony\Component\HttpFoundation\RequestStack ' )) {
605+ $ this ->markTestSkipped ('RequestStack unavailable. ' );
606+ }
607+
608+ $ errors = new ConstraintViolationList (array (
609+ new ConstraintViolation ('expected message 1 ' , null , array (), null , null , null ),
610+ new ConstraintViolation ('expected message 2 ' , null , array (), null , null , null ),
611+ ));
612+
613+ $ this ->validator ->expects ($ this ->once ())
614+ ->method ($ this ->validatorMethod )
615+ ->with ('foobar ' , $ this ->constraint )
616+ ->will ($ this ->returnValue ($ errors ));
617+
618+ $ param = new QueryParam ();
619+ $ param ->name = 'bizoo ' ;
620+ $ param ->strict = true ;
621+ $ param ->requirements = $ this ->constraint ;
622+ $ param ->description = 'A requirements param ' ;
623+
624+ $ reader = $ this ->getMockBuilder ('FOS\RestBundle\Request\ParamReader ' )
625+ ->disableOriginalConstructor ()
626+ ->getMock ();
627+ $ reader ->expects ($ this ->any ())
628+ ->method ('read ' )
629+ ->will ($ this ->returnValue (array ('bizoo ' => $ param )));
630+ $ requestStack = new RequestStack ();
631+ $ requestStack ->push (new Request (array ('bizoo ' => 'foobar ' ), array (), array ('_controller ' => __CLASS__ .'::stubAction ' )));
632+
633+ $ queryFetcher = new ParamFetcher ($ reader , $ requestStack , $ this ->validator );
599634 $ queryFetcher ->setController ($ this ->controller );
600635 $ queryFetcher ->get ('bizoo ' );
601636 }
@@ -636,7 +671,7 @@ public function testConstraintReturnDefaultInSafeMode()
636671 ->method ('read ' )
637672 ->will ($ this ->returnValue (array ('bizoo ' => $ param )));
638673
639- $ queryFetcher = new ParamFetcher ($ reader , $ requestStack , $ this ->violationFormatter , $ this -> validator );
674+ $ queryFetcher = new ParamFetcher ($ reader , $ requestStack , $ this ->validator );
640675 $ queryFetcher ->setController ($ this ->controller );
641676 $ this ->assertEquals ('expected ' , $ queryFetcher ->get ('bizoo ' ));
642677 }
@@ -671,7 +706,7 @@ public function testConstraintOk()
671706 ->method ('read ' )
672707 ->will ($ this ->returnValue (array ('bizoo ' => $ param )));
673708
674- $ queryFetcher = new ParamFetcher ($ reader , $ requestStack , $ this ->violationFormatter , $ this -> validator );
709+ $ queryFetcher = new ParamFetcher ($ reader , $ requestStack , $ this ->validator );
675710 $ queryFetcher ->setController ($ this ->controller );
676711 $ this ->assertEquals ('foobar ' , $ queryFetcher ->get ('bizoo ' ));
677712 }
@@ -706,7 +741,7 @@ public function testDeepArrayAllowedWithConstraint()
706741 ->method ('read ' )
707742 ->will ($ this ->returnValue (array ('bizoo ' => $ param )));
708743
709- $ queryFetcher = new ParamFetcher ($ reader , $ requestStack , $ this ->violationFormatter , $ this -> validator );
744+ $ queryFetcher = new ParamFetcher ($ reader , $ requestStack , $ this ->validator );
710745 $ queryFetcher ->setController ($ this ->controller );
711746 $ this ->assertSame (array ('foo ' => array ('b ' , 'a ' , 'r ' )), $ queryFetcher ->get ('bizoo ' ));
712747 }
@@ -737,7 +772,7 @@ public function testNullValidatorWithRequirements()
737772 ->method ('read ' )
738773 ->will ($ this ->returnValue (array ('bizoo ' => $ param )));
739774
740- $ queryFetcher = new ParamFetcher ($ reader , $ requestStack, $ this -> violationFormatter );
775+ $ queryFetcher = new ParamFetcher ($ reader , $ requestStack );
741776 $ queryFetcher ->setController ($ this ->controller );
742777 $ queryFetcher ->get ('bizoo ' );
743778 }
@@ -763,11 +798,15 @@ public function testNullValidatorWithoutRequirements()
763798 ->method ('read ' )
764799 ->will ($ this ->returnValue (array ('bizoo ' => $ param )));
765800
766- $ queryFetcher = new ParamFetcher ($ reader , $ requestStack, $ this -> violationFormatter );
801+ $ queryFetcher = new ParamFetcher ($ reader , $ requestStack );
767802 $ queryFetcher ->setController ($ this ->controller );
768803 $ this ->assertEquals ('foobar ' , $ queryFetcher ->get ('bizoo ' ));
769804 }
770805
806+ /**
807+ * @expectedException \FOS\RestBundle\Exception\InvalidParameterException
808+ * @expectedExceptionMessage Parameter "fero" of value "foobar" violated a constraint "variable must be an integer"
809+ */
771810 public function testCustomErrorMessage ()
772811 {
773812 if (!class_exists ('Symfony\Component\HttpFoundation\RequestStack ' )) {
@@ -797,22 +836,16 @@ public function testCustomErrorMessage()
797836 ));
798837
799838 $ errors = new ConstraintViolationList (array (
800- new ConstraintViolation ($ errorMessage , null , array (), null , null , null ),
839+ new ConstraintViolation ($ errorMessage , null , array (), null , null , ' foobar ' ),
801840 ));
802841
803842 $ this ->validator ->expects ($ this ->once ())
804843 ->method ($ this ->validatorMethod )
805844 ->with ('foobar ' , $ constraint )
806845 ->will ($ this ->returnValue ($ errors ));
807846
808- $ queryFetcher = new ParamFetcher ($ reader , $ requestStack , $ this ->violationFormatter , $ this -> validator );
847+ $ queryFetcher = new ParamFetcher ($ reader , $ requestStack , $ this ->validator );
809848 $ queryFetcher ->setController ($ this ->controller );
810-
811- try {
812- $ queryFetcher ->get ('fero ' );
813- $ this ->fail ('Fetching get() in strict mode with no default value did not throw an exception ' );
814- } catch (HttpException $ httpException ) {
815- $ this ->assertEquals ('Parameter "fero" of value "" violated a constraint "variable must be an integer" ' , $ httpException ->getMessage ());
816- }
849+ $ queryFetcher ->get ('fero ' );
817850 }
818851}
0 commit comments