コード例 #1
0
 /**
  * @expectedException \RuntimeException
  * @expectedExceptionMessage The ParamFetcher requirements feature requires the symfony/validator component.
  */
 public function testEmptyValidator()
 {
     $param = $this->createMockedParam('none', null, array(), false, null, array('constraint'));
     $this->setParams([$param]);
     $fetcher = new ParamFetcher($this->paramReader, $this->requestStack, $this->violationFormatter, null);
     $fetcher->setContainer($this->container);
     $fetcher->setController($this->controller);
     $fetcher->get('none', '42');
 }
コード例 #2
0
 public function testCustomErrorMessage()
 {
     $param = new QueryParam();
     $param->name = 'fero';
     $errorMessage = "variable must be an integer";
     $param->requirements = array('rule' => '\\d+', 'error_message' => $errorMessage);
     $param->description = 'integer value';
     $param->strict = true;
     $request = new Request(array('fero' => 'foobar'), array(), array('_controller' => __CLASS__ . '::stubAction'));
     $reader = $this->getMockBuilder('FOS\\RestBundle\\Request\\ParamReader')->disableOriginalConstructor()->getMock();
     $reader->expects($this->any())->method('read')->will($this->returnValue(array('fero' => $param)));
     $constraint = new Regex(array('pattern' => '#^(?:\\d+)$#xsu', 'message' => $errorMessage));
     $errors = new ConstraintViolationList(array(new ConstraintViolation($errorMessage, null, array(), null, null, null)));
     $this->validator->expects($this->once())->method('validateValue')->with('foobar', $constraint)->will($this->returnValue($errors));
     $requestStack = new RequestStack();
     $requestStack->push($request);
     $queryFetcher = new ParamFetcher($reader, $requestStack, $this->violationFormatter, $this->validator);
     $queryFetcher->setContainer($this->container);
     $queryFetcher->setController($this->controller);
     try {
         $queryFetcher->get('fero');
         $this->fail('Fetching get() in strict mode with no default value did not throw an exception');
     } catch (HttpException $httpException) {
         $this->assertEquals($errorMessage, $httpException->getMessage());
     }
 }