Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Commit

Permalink
Add type hints and test cases for ParameterBagUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
scheb committed Oct 25, 2018
1 parent 5296fb0 commit 8c1d371
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Security/Http/ParameterBagUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ParameterBagUtils
*
* @throws \InvalidArgumentException when the given path is malformed
*/
public static function getRequestParameterValue(Request $request, $path)
public static function getRequestParameterValue(Request $request, string $path): ?string
{
if (false === $pos = strpos($path, '[')) {
return $request->get($path);
Expand All @@ -33,7 +33,7 @@ public static function getRequestParameterValue(Request $request, $path)
$root = substr($path, 0, $pos);

if (null === $value = $request->get($root)) {
return;
return null;
}

if (null === self::$propertyAccessor) {
Expand All @@ -43,7 +43,7 @@ public static function getRequestParameterValue(Request $request, $path)
try {
return self::$propertyAccessor->getValue($value, substr($path, $pos));
} catch (AccessException $e) {
return;
return null;
}
}
}
51 changes: 51 additions & 0 deletions Tests/Security/Http/ParameterBagUtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Scheb\TwoFactorBundle\Tests\Security\Http;

use PHPUnit\Framework\MockObject\MockObject;
use Scheb\TwoFactorBundle\Security\Http\ParameterBagUtils;
use Scheb\TwoFactorBundle\Tests\TestCase;
use Symfony\Component\HttpFoundation\Request;

class ParameterBagUtilsTest extends TestCase
{
/**
* @var MockObject|Request
*/
private $request;

protected function setUp()
{
$this->request = new Request([
'topLevelField' => 'topLevelValue',
'arrayArrayField' => ['nestedField' => 'nestedValue'],
]);
}

/**
* @test
*/
public function getRequestParameterValue_nonExistentField_returnNull(): void
{
$returnValue = ParameterBagUtils::getRequestParameterValue($this->request, 'nonExistentField');
$this->assertNull($returnValue);
}

/**
* @test
*/
public function getRequestParameterValue_topLevelField_returnValue(): void
{
$returnValue = ParameterBagUtils::getRequestParameterValue($this->request, 'topLevelField');
$this->assertEquals('topLevelValue', $returnValue);
}

/**
* @test
*/
public function getRequestParameterValue_arrayArrayField_returnValue(): void
{
$returnValue = ParameterBagUtils::getRequestParameterValue($this->request, 'arrayArrayField[nestedField]');
$this->assertEquals('nestedValue', $returnValue);
}
}

0 comments on commit 8c1d371

Please sign in to comment.