Skip to content

Commit

Permalink
Merge fed8f97 into cb294a4
Browse files Browse the repository at this point in the history
  • Loading branch information
einorler committed May 11, 2016
2 parents cb294a4 + fed8f97 commit fe940b8
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Event/ExceptionListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ONGR\FilterManagerBundle\Event;

use Doctrine\Common\Proxy\Exception\InvalidArgumentException;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Response;

class ExceptionListener
{
/**
* @param Event $event
*
* @return Response
*/
public function onKernelException(Event $event)
{
$exception = $event->getException();
if ($exception instanceof InvalidArgumentException &&
$exception->getMessage() == 'Given value must not be an array'
) {
$response = new Response('Given value to the single choice filter must not be an array', 406);
$event->setResponse($response);
}
}
}
4 changes: 4 additions & 0 deletions Filter/Widget/Choice/SingleTermChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace ONGR\FilterManagerBundle\Filter\Widget\Choice;

use Doctrine\Common\Proxy\Exception\InvalidArgumentException;
use ONGR\ElasticsearchDSL\Aggregation\FilterAggregation;
use ONGR\ElasticsearchDSL\Aggregation\TermsAggregation;
use ONGR\ElasticsearchDSL\Query\TermQuery;
Expand Down Expand Up @@ -60,6 +61,9 @@ public function getSortType()
public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null)
{
if ($state && $state->isActive()) {
if (is_array($state->getValue())) {
throw new InvalidArgumentException('Given value must not be an array');
}
$search->addPostFilter(new TermQuery($this->getField(), $state->getValue()));
}
}
Expand Down
4 changes: 4 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ services:
- '@router'
tags:
- { name: twig.extension }
ongr_filter_manager.event.exception_listener:
class: ONGR\FilterManagerBundle\Event\ExceptionListener
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
14 changes: 14 additions & 0 deletions Tests/Functional/Filter/Widget/Choice/SingleTermChoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,18 @@ public function testChoicesSize()

$this->assertEquals(11, count($result->getChoices()));
}

/**
* Tests bad request sent to manager
*/
public function testArrayInRequest()
{
$client = static::createClient();
$crawler = $client->request('GET', '/array_test?single_choice[]=blue+black');
$this->assertEquals(406, $client->getResponse()->getStatusCode());
$this->assertContains(
'Given value to the single choice filter must not be an array',
$crawler->html()
);
}
}
3 changes: 3 additions & 0 deletions Tests/app/config/config_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ ongr_filter_manager:
range_filters:
filters: ['date']
repository: 'es.manager.default.product'
single_choice:
filters: ['single_choice']
repository: 'es.manager.default.product'
filters:
match:
phrase:
Expand Down
5 changes: 5 additions & 0 deletions Tests/app/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ app_list_json:

test_page:
path: /

test_bad_request:
path: /array_test
defaults:
_controller: TestBundle:Test:testSingleChoiceException
31 changes: 31 additions & 0 deletions Tests/app/fixture/TestBundle/Controller/TestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\FilterManagerBundle\Tests\app\fixture\TestBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class TestController extends Controller
{
/**
* @param Request $request
*
* @return Response
*/
public function testSingleChoiceExceptionAction(Request $request)
{
$manager = $this->get('ongr_filter_manager.single_choice');
$result = $manager->handleRequest($request);
return new Response($result);
}
}

0 comments on commit fe940b8

Please sign in to comment.