Skip to content

Commit

Permalink
Added ParamConverter test
Browse files Browse the repository at this point in the history
  • Loading branch information
kisztof committed Oct 24, 2023
1 parent 12d9547 commit b1689ab
Showing 1 changed file with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Bundle\Search\Request\ParamConverter;

use Ibexa\Bundle\Search\Request\ParamConverter\SuggestionQueryParamConverter;
use Ibexa\Search\Model\SuggestionQuery;
use PHPUnit\Framework\TestCase;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Request;

final class SuggestionQueryParamConverterTest extends TestCase
{
private const DEFAULT_LIMIT = 10;

private SuggestionQueryParamConverter $converter;

protected function setUp(): void
{
$this->converter = new SuggestionQueryParamConverter(self::DEFAULT_LIMIT);
}

/**
* @dataProvider provideSupportsTestData
*/
public function testSupports(string $class, bool $expectedResult): void
{
$configuration = new ParamConverter([]);
$configuration->setClass($class);

$this->assertSame($expectedResult, $this->converter->supports($configuration));
}

public function provideSupportsTestData(): array
{
return [
'Supports SuggestionQuery' => [SuggestionQuery::class, true],
'Does not support other classes' => [\stdClass::class, false],
];
}

/**
* @dataProvider provideApplyTestData
*/
public function testApply(array $requestData, SuggestionQuery $expectedSuggestionQuery): void
{
$configuration = new ParamConverter([]);
$configuration->setName('suggestion');
$configuration->setClass(SuggestionQuery::class);

$request = new Request([], [], [], [], [], [], []);
$request->query->add($requestData);

$this->converter->apply($request, $configuration);

/** @var \Ibexa\Search\Model\SuggestionQuery $suggestionQuery */
$suggestionQuery = $request->attributes->get('suggestion');

$this->assertInstanceOf(SuggestionQuery::class, $suggestionQuery);
$this->assertEquals($expectedSuggestionQuery, $suggestionQuery);
}

public function provideApplyTestData(): array
{
return [
'All parameters provided' => [
['query' => 'test', 'limit' => 5, 'language' => 'en'],
new SuggestionQuery('test', 5, 'en'),
],
'Only query provided' => [
['query' => 'test'],
new SuggestionQuery('test', self::DEFAULT_LIMIT, null),
],
'Limit falls back to default' => [
['query' => 'test', 'language' => 'en'],
new SuggestionQuery('test', self::DEFAULT_LIMIT, 'en'),
],
];
}
}

0 comments on commit b1689ab

Please sign in to comment.