Completed
Push — master ( 81b739...8e6e04 )
by Matthieu
01:47
created

tests/DeepCopyTest/Filter/ReplaceFilterTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace DeepCopyTest\Filter;
4
5
use DeepCopy\DeepCopy;
6
use DeepCopy\Filter\ReplaceFilter;
7
use DeepCopy\Matcher\PropertyMatcher;
8
9
class ReplaceFilterTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * @dataProvider handlerProvider
13
     */
14
    public function testApply(callable $callback, array $expected)
15
    {
16
        $object = new \stdClass();
17
        $object->data = ['foo' => 'bar', 'baz' => 'foo'];
18
19
        $filter = new ReplaceFilter($callback);
20
21
        $filter->apply($object, 'data', function () {
22
            return null;
23
        });
24
25
        $this->assertEquals($expected, $object->data);
26
    }
27
28
    public function handlerProvider()
29
    {
30
        $closure = function ($data) {
31
            unset($data['baz']);
32
            return $data;
33
        };
34
35
        return [
36
            [$closure, ['foo' => 'bar']],
37
            ['DeepCopyTest\Filter\Callback::copy', ['foo' => 'bar', 'baz' => 'foo', 'foobar' => 'baz']],
38
            [[new Callback(), 'callback'], ['foo' => 'foo']]
39
        ];
40
    }
41
42
    public function testIntegration()
43
    {
44
        // Prepare object to copy
45
        $object = new \StdClass();
46
        $object->data = [
47
            'foo' => 'bar',
48
            'baz' => ['bar' => 'foo'],
49
            'bar' => 'foo'
50
        ];
51
52
        // Copy
53
        $deepCopy = new DeepCopy();
54
        $deepCopy->addFilter(new ReplaceFilter(function ($data) {
55
            $data['baz']['bar'] = 'dummy_change';
56
            unset($data['bar']);
57
58
            return $data;
59
        }), new PropertyMatcher(get_class($object), 'data'));
60
61
        $copied = $deepCopy->copy($object);
62
63
        // Check copied
64
        $this->assertEquals([
65
            'foo' => 'bar',
66
            'baz' => ['bar' => 'dummy_change']
67
        ], $copied->data);
68
69
        // Check original object is unchanged
70
        $this->assertEquals(
71
            ['foo' => 'bar', 'baz' => ['bar' => 'foo'], 'bar' => 'foo'],
72
            $object->data
73
        );
74
    }
75
}
76
77
class Callback
78
{
79
    public static function copy($data)
80
    {
81
        $data['foobar'] = 'baz';
82
        return $data;
83
    }
84
85
    public function callback($data)
0 ignored issues
show
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Using PHP4-style constructors that are named like the class is not recommend; better use the more explicit __construct method.
Loading history...
86
    {
87
        return ['foo' => 'foo'];
88
    }
89
}
90