NormalizerTest::testGetObjectVarsNormalizer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Thunder\Serializard\Tests;
3
4
use Thunder\Serializard\Normalizer\CallbackNormalizer;
5
use Thunder\Serializard\Normalizer\ClosureBindNormalizer;
6
use Thunder\Serializard\Normalizer\GetObjectVarsNormalizer;
7
use Thunder\Serializard\Normalizer\ReflectionNormalizer;
8
use Thunder\Serializard\Tests\Fake\Inheritance\FakeClass;
9
use Thunder\Serializard\Tests\Fake\FakeTag;
10
use Thunder\Serializard\Tests\Fake\FakeUser;
11
use Thunder\Serializard\Tests\Fake\PropertyVisibility;
12
13
/**
14
 * @author Tomasz Kowalczyk <[email protected]>
15
 */
16
final class NormalizerTest extends AbstractTestCase
17
{
18
    public function testReflectionSkip()
19
    {
20
        $normalizer = new ReflectionNormalizer(['tag', 'tags']);
21
        $object = new FakeUser(12, 'XXX', new FakeTag(144, 'YYY'));
22
23
        $this->assertSame(['id' => 12, 'name' => 'XXX'], $normalizer($object));
24
    }
25
26
    public function testReflectionInheritance()
27
    {
28
        $normalizer = new ReflectionNormalizer();
29
30
        $this->assertSame([
31
            'property' => 'property',
32
            'parentProperty' => 'parent',
33
            'parentParentProperty' => 'parentParent',
34
        ], $normalizer(new FakeClass('parentParent', 'parent', 'property')));
35
    }
36
37
    public function testGetObjectVarsNormalizer()
38
    {
39
        $normalizer = new GetObjectVarsNormalizer();
40
41
        $this->assertSame(['public' => 'public'], $normalizer(new PropertyVisibility()));
42
    }
43
44
    public function testCallbackNormalizer()
45
    {
46
        $normalizer = new CallbackNormalizer(function(PropertyVisibility $pv) {
47
            return ['public' => $pv->public];
48
        });
49
50
        $this->assertSame(['public' => 'public'], $normalizer(new PropertyVisibility()));
51
    }
52
53
    public function testClosureBindNormalizer()
54
    {
55
        $normalizer = new ClosureBindNormalizer(function() {
56
            return [
57
                'id' => $this->id,
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
58
                'name' => $this->name,
0 ignored issues
show
Bug introduced by
The property name cannot be accessed from this context as it is declared private in class PHPUnit\Framework\TestCase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
59
                'tag' => $this->tag,
0 ignored issues
show
Bug introduced by
The property tag does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60
            ];
61
        });
62
63
        $this->assertEquals([
64
            'id' => 1,
65
            'name' => 'user',
66
            'tag' => new FakeTag(2, 'tag'),
67
        ], $normalizer(new FakeUser(1, 'user', new FakeTag(2, 'tag'))));
68
    }
69
}
70