/**
  * @param MvcEvent $event
  * @return null|\ZF\ApiProblem\ApiProblemResponse
  */
 public function onRoute(MvcEvent $event)
 {
     $request = $event->getRequest();
     if ($request instanceof HttpRequest && $request->isPatch()) {
         $dataContainer = $event->getParam('ZFContentNegotiationParameterData', false);
         if ($dataContainer instanceof ParameterDataContainer) {
             $data = $dataContainer->getBodyParams();
             if (empty($data)) {
                 // For patch, only the provided data is filtered and validated.
                 // So when no data was provided, we don't need to filter at all.
                 return null;
             }
         }
     }
     return $this->wrappedListener->onRoute($event);
 }
    /**
     * @group event
     */
    public function testTriggeredEventBeforeValidateReturnsApiProblemResponseFromCallback()
    {
        $services = new ServiceManager();
        $factory  = new InputFilterFactory();
        $services->setService('FooValidator', $factory->createInputFilter(array(
            'foo' => array(
                'name' => 'foo',
                'validators' => array(
                    array('name' => 'Digits'),
                ),
            )
        )));
        $listener = new ContentValidationListener(array(
            'Foo' => array('input_filter' => 'FooValidator'),
        ), $services, array(
            'Foo' => 'foo_id',
        ));

        $eventManager = new EventManager();
        $listener->setEventManager($eventManager);

        $runner = $this;
        $hasRun = false;
        $eventManager->attach(
            ContentValidationListener::EVENT_BEFORE_VALIDATE,
            function (MvcEvent $e) use ($runner, &$hasRun) {
                $runner->assertInstanceOf(
                    'Zend\InputFilter\InputFilterInterface',
                    $e->getParam('ZF\ContentValidation\InputFilter')
                );
                $hasRun = true;
                return new ApiProblemResponse(new ApiProblem(422, 'Validation failed'));
            }
        );

        $request = new HttpRequest();
        $request->setMethod('PUT');

        $matches = new RouteMatch(array('controller' => 'Foo'));

        $params = array_fill(0, 10, array(
            'foo' => '123',
        ));

        $dataParams = new ParameterDataContainer();
        $dataParams->setBodyParams($params);

        $event   = new MvcEvent();
        $event->setRequest($request);
        $event->setRouteMatch($matches);
        $event->setParam('ZFContentNegotiationParameterData', $dataParams);

        $response = $listener->onRoute($event);
        $this->assertTrue($hasRun);
        $this->assertInstanceOf('ZF\ApiProblem\ApiProblemResponse', $response);
        $this->assertEquals(422, $response->getApiProblem()->status);
        $this->assertEquals('Validation failed', $response->getApiProblem()->detail);
    }
 /**
  * @group 20
  */
 public function testEmptyPostShouldReturnValidationError()
 {
     $services = new ServiceManager();
     $factory = new InputFilterFactory();
     $services->setService('FooValidator', $factory->createInputFilter(array('foo' => array('name' => 'foo', 'validators' => array(array('name' => 'Digits'))), 'bar' => array('name' => 'bar', 'validators' => array(array('name' => 'Regex', 'options' => array('pattern' => '/^[a-z]+/i')))))));
     $listener = new ContentValidationListener(array('Foo' => array('input_filter' => 'FooValidator')), $services, array('Foo' => 'foo_id'));
     $request = new HttpRequest();
     $request->setMethod('POST');
     $matches = new RouteMatch(array('controller' => 'Foo'));
     $dataParams = new ParameterDataContainer();
     $dataParams->setBodyParams(array());
     $event = new MvcEvent();
     $event->setRequest($request);
     $event->setRouteMatch($matches);
     $event->setParam('ZFContentNegotiationParameterData', $dataParams);
     $response = $listener->onRoute($event);
     $this->assertInstanceOf('ZF\\ApiProblem\\ApiProblemResponse', $response);
     $this->assertEquals(422, $response->getApiProblem()->status);
 }