Skip to content

Commit ab661ef

Browse files
authored
Merge pull request #5 from dlakomski/master
Fix deprecation notices from Symfony' InputBag
2 parents d77f2f5 + cb6c794 commit ab661ef

File tree

7 files changed

+70
-2
lines changed

7 files changed

+70
-2
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ install:
88
- composer install --dev
99

1010
script:
11-
- ./vendor/bin/phpunit tests
11+
- ./vendor/bin/phpunit

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"require-dev": {
1111
"phpunit/phpunit": "^8.1",
1212
"symfony/console": "^5.0",
13-
"symfony/http-kernel": "^5.0"
13+
"symfony/http-kernel": "^5.0",
14+
"symfony/error-handler": "^5.0"
1415
},
1516
"autoload": {
1617
"psr-4": {

phpunit.xml.dist

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<phpunit
3+
bootstrap="tests/bootstrap.php"
4+
>
5+
<testsuites>
6+
<testsuite name="GW\Safe">
7+
<directory>tests</directory>
8+
</testsuite>
9+
</testsuites>
10+
</phpunit>

src/SafeParameterBag.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace GW\Safe;
44

5+
use Symfony\Component\HttpFoundation\InputBag;
56
use Symfony\Component\HttpFoundation\ParameterBag;
67

78
final class SafeParameterBag
@@ -25,6 +26,10 @@ public static function from(ParameterBag $params): self
2526
*/
2627
public function value(string $key, $default)
2728
{
29+
if ($this->params instanceof InputBag) {
30+
return $this->params->all()[$key] ?? $default;
31+
}
32+
2833
return $this->params->get($key, $default);
2934
}
3035
}

tests/SafeParameterBagTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace tests\GW\Safe;
4+
5+
use GW\Safe\SafeParameterBag;
6+
use PHPUnit\Framework\TestCase;
7+
use Symfony\Component\HttpFoundation\InputBag;
8+
9+
class SafeParameterBagTest extends TestCase
10+
{
11+
public function test_InputBag()
12+
{
13+
$safeBag = SafeParameterBag::from(new InputBag(['test' => 1, 'array' => ['abc' => 'def']]));
14+
15+
self::assertEquals(1, $safeBag->value('test', null));
16+
self::assertEquals(2, $safeBag->value('not-exists', 2));
17+
self::assertEquals(['abc' => 'def'], $safeBag->value('array', []));
18+
}
19+
}

tests/SafeRequestTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace tests\GW\Safe;
4+
5+
use GW\Safe\SafeAssocArray;
6+
use GW\Safe\SafeRequest;
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\HttpFoundation\Request;
9+
10+
class SafeRequestTest extends TestCase
11+
{
12+
public function test_from()
13+
{
14+
$request = new Request(
15+
['query_param' => 1, 'query_array_param' => ['x' => 5]],
16+
['request_param' => 'test'],
17+
['attribute_param' => ['abc' => 'def']]
18+
);
19+
$safeRequest = SafeRequest::from($request);
20+
21+
self::assertEquals(1, $safeRequest->query()->int('query_param'));
22+
self::assertEquals(SafeAssocArray::from(['x' => 5]), $safeRequest->query()->array('query_array_param'));
23+
self::assertEquals(10, $safeRequest->query()->int('not_existent_param', 10));
24+
self::assertEquals('test', $safeRequest->post()->string('request_param'));
25+
self::assertEquals(
26+
SafeAssocArray::from(['abc' => 'def']),
27+
$safeRequest->attributes()->array('attribute_param')
28+
);
29+
}
30+
}

tests/bootstrap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
\Symfony\Component\ErrorHandler\Debug::enable();

0 commit comments

Comments
 (0)