|
| 1 | +<?php |
| 2 | +namespace Particle\Filter\Tests\FilterRule; |
| 3 | + |
| 4 | +use Particle\Filter\Filter; |
| 5 | + |
| 6 | +class DecodeJSONTest extends \PHPUnit_Framework_TestCase |
| 7 | +{ |
| 8 | + /** |
| 9 | + * @var Filter |
| 10 | + */ |
| 11 | + protected $filter; |
| 12 | + |
| 13 | + /** |
| 14 | + * Prepare the filter |
| 15 | + */ |
| 16 | + public function setUp() |
| 17 | + { |
| 18 | + $this->filter = new Filter(); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * @dataProvider getTestParams |
| 23 | + * @param mixed $value |
| 24 | + * @param bool $assoc |
| 25 | + * @param int $depth |
| 26 | + * @param int $options |
| 27 | + * @param mixed $filteredValue |
| 28 | + */ |
| 29 | + public function testDecodeJSONFilterRule($value, $assoc, $depth, $options, $filteredValue) |
| 30 | + { |
| 31 | + $this->filter->value('test')->decodeJSON($assoc, $depth, $options); |
| 32 | + |
| 33 | + $result = $this->filter->filter([ |
| 34 | + 'test' => $value |
| 35 | + ]); |
| 36 | + |
| 37 | + $this->assertEquals($filteredValue, $result['test']); |
| 38 | + } |
| 39 | + |
| 40 | + public function testDecodeJSONFilterRuleWithBigInt() |
| 41 | + { |
| 42 | + $this->filter->value('test1')->decodeJSON(false, 100, 0); |
| 43 | + $this->filter->value('test2')->decodeJSON(false, 100, JSON_BIGINT_AS_STRING); |
| 44 | + |
| 45 | + $result = $this->filter->filter([ |
| 46 | + 'test1' => '1000000000000000000000000000000', |
| 47 | + 'test2' => '1000000000000000000000000000000' |
| 48 | + ]); |
| 49 | + |
| 50 | + $this->assertInternalType('float', $result['test1']); |
| 51 | + $this->assertInternalType('string', $result['test2']); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @return array |
| 56 | + */ |
| 57 | + public function getTestParams() |
| 58 | + { |
| 59 | + return [ |
| 60 | + // Ordinary JSON |
| 61 | + [ |
| 62 | + '{"string": "foo", "int": 100500, "array": [false, true, null]}', |
| 63 | + false, |
| 64 | + 100, |
| 65 | + 0, |
| 66 | + (object)['string' => 'foo', 'int' => 100500, 'array' => [false, true, null]] |
| 67 | + ], |
| 68 | + ['"Hello"', false, 100, 0, 'Hello'], |
| 69 | + ['1999', false, 100, 0, 1999], |
| 70 | + |
| 71 | + // Using the assoc parameter |
| 72 | + ['{"string": "foo", "int": 100500}', true, 100, 0, ['string' => 'foo', 'int' => 100500]], |
| 73 | + |
| 74 | + // Using the depth parameter |
| 75 | + ['{"1": {"2": {"3": {"4": "the end"}}}}', true, 100, 0, [1 => [2 => [3 => [4 => "the end"]]]]], |
| 76 | + ['{"1": {"2": {"3": {"4": "the end"}}}}', true, 3, 0, null], |
| 77 | + |
| 78 | + // Incorrect JSON |
| 79 | + ['I am not a JSON', false, 100, 0, null], |
| 80 | + ['', false, 100, 0, null], |
| 81 | + |
| 82 | + // Not a string |
| 83 | + [null, false, 100, 0, null], |
| 84 | + [3456, false, 100, 0, 3456], |
| 85 | + ]; |
| 86 | + } |
| 87 | +} |
0 commit comments