Skip to content

Commit

Permalink
Support objects with integer keys. Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Marius Balčytis committed Oct 7, 2019
1 parent ae6ad50 commit 7edfce7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
25 changes: 25 additions & 0 deletions .scrutinizer.yml
@@ -0,0 +1,25 @@
checks:
php:
code_rating: true

filter:
excluded_paths:
- tests/*
- vendor/*

build:
environment:
php: '7.3'

dependencies:
before:
- composer install
- mkdir -p build/logs

tests:
override:
-
command: 'bin/phpunit --coverage-clover build/logs/clover.xml'
coverage:
file: 'build/logs/clover.xml'
format: 'clover'
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -20,7 +20,8 @@
"require-dev": {
"phpunit/phpunit": "^6.5",
"paysera/lib-php-cs-fixer-config": "^2.2.2",
"mockery/mockery": "^1.2"
"mockery/mockery": "^1.2",
"ext-json": "*"
},
"config": {
"bin-dir": "bin"
Expand Down
1 change: 1 addition & 0 deletions src/DataFilter.php
Expand Up @@ -37,6 +37,7 @@ private function filterObject($data, NormalizationContext $context)
{
$result = new stdClass();
foreach ($data as $key => $value) {
$key = (string)$key;
if ($value !== null && $context->isFieldIncluded($key)) {
$result->$key = $this->filterData($value, $context->createScopedContext($key));
}
Expand Down
44 changes: 44 additions & 0 deletions tests/DataFilterTest.php
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Paysera\Component\Normalization\Tests;

use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use Paysera\Component\Normalization\CoreNormalizer;
use Paysera\Component\Normalization\DataFilter;
use Paysera\Component\Normalization\NormalizationContext;
use stdClass;

class DataFilterTest extends MockeryTestCase
{

/**
* @dataProvider providerForFilterDataWithoutFiltering
* @param string $expected
* @param mixed $input
*/
public function testFilterDataWithoutFiltering(string $expected, $input)
{
$normalizer = Mockery::mock(CoreNormalizer::class);
$context = new NormalizationContext($normalizer);

$dataFilter = new DataFilter();

$output = $dataFilter->filterData($input, $context);
$this->assertEquals($expected, json_encode($output));
}

public function providerForFilterDataWithoutFiltering()
{
return [
['{}', new stdClass()],
['[]', []],
['{"a":"b"}', ['a' => 'b']],
['{"a":"b","1":2}', ['a' => 'b', 1 => 2]],
['[1,2]', [0 => 1, 1 => 2]],
['{"0":1,"2":2}', [0 => 1, 2 => 2]],
];
}
}

0 comments on commit 7edfce7

Please sign in to comment.