Completed
Pull Request — master (#6)
by Tomasz
07:02
created

NormalizerContainerTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 37.63 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
lcom 2
cbo 2
dl 35
loc 93
c 1
b 0
f 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testAlias() 9 9 1
A testInterface() 8 8 1
A testInheritance() 9 9 1
A testMultipleInterfacesException() 9 9 1
A testNoDefaultHandler() 0 11 1
A testDefaultHandlerFallback() 0 11 1
A testExceptionOnInvalidDefaultHandler() 0 6 1
A testInvalidClassOrInterfaceName() 0 6 1
A testAliasForInvalidClass() 0 6 1
A testInvalidHandler() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Thunder\Serializard\Tests;
3
4
use Thunder\Serializard\Exception\InvalidClassNameException;
5
use Thunder\Serializard\Exception\InvalidNormalizerException;
6
use Thunder\Serializard\Exception\NormalizerConflictException;
7
use Thunder\Serializard\Exception\NormalizerNotFoundException;
8
use Thunder\Serializard\NormalizerContainer\FallbackNormalizerContainer;
9
use Thunder\Serializard\Tests\Fake\FakeUser;
10
use Thunder\Serializard\Tests\Fake\FakeUserParent;
11
use Thunder\Serializard\Tests\Fake\FakeUserParentParent;
12
use Thunder\Serializard\Tests\Fake\Interfaces\AnotherTypeInterface;
13
use Thunder\Serializard\Tests\Fake\Interfaces\TypeA;
14
use Thunder\Serializard\Tests\Fake\Interfaces\TypeB;
15
use Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface;
16
use Thunder\Serializard\Tests\Fake\Interfaces\TypeMultiple;
17
18
/**
19
 * @author Tomasz Kowalczyk <[email protected]>
20
 */
21
final class NormalizerContainerTest extends AbstractTestCase
22
{
23 View Code Duplication
    public function testAlias()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    {
25
        $normalizers = new FallbackNormalizerContainer();
26
        $normalizers->add(\stdClass::class, function() { return 'value'; });
27
        $normalizers->addAlias(\DateTime::class, \stdClass::class);
28
29
        $this->assertSame('value', call_user_func($normalizers->getHandler(\stdClass::class)));
30
        $this->assertSame('value', call_user_func($normalizers->getHandler(\DateTime::class)));
31
    }
32
33 View Code Duplication
    public function testInterface()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        $normalizers = new FallbackNormalizerContainer();
36
        $normalizers->add(TypeInterface::class, function() { return 'type'; });
37
38
        $this->assertSame('type', call_user_func($normalizers->getHandler(TypeA::class)));
39
        $this->assertSame('type', call_user_func($normalizers->getHandler(TypeB::class)));
40
    }
41
42 View Code Duplication
    public function testInheritance()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        $normalizers = new FallbackNormalizerContainer();
45
        $normalizers->add(FakeUserParentParent::class, function() { return 'ancestor'; });
46
47
        $this->assertSame('ancestor', call_user_func($normalizers->getHandler(FakeUserParentParent::class)));
48
        $this->assertSame('ancestor', call_user_func($normalizers->getHandler(FakeUserParent::class)));
49
        $this->assertSame('ancestor', call_user_func($normalizers->getHandler(FakeUser::class)));
50
    }
51
52 View Code Duplication
    public function testMultipleInterfacesException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $normalizers = new FallbackNormalizerContainer();
55
        $normalizers->add(TypeInterface::class, function() { return 'multiple'; });
56
        $normalizers->add(AnotherTypeInterface::class, function() { return 'multiple'; });
57
58
        $this->expectExceptionClass(NormalizerConflictException::class);
59
        $normalizers->getHandler(TypeMultiple::class);
60
    }
61
62
    public function testNoDefaultHandler()
63
    {
64
        $normalizers = new FallbackNormalizerContainer();
65
        $this->assertFalse($normalizers->hasDefault());
66
        $this->assertNull($normalizers->getDefault());
67
68
        $handler = function() {};
69
        $normalizers->setDefault($handler);
70
        $this->assertTrue($normalizers->hasDefault());
71
        $this->assertSame($handler, $normalizers->getDefault());
72
    }
73
74
    public function testDefaultHandlerFallback()
75
    {
76
        $direct = function() {};
77
        $fallback = function() {};
78
        $normalizers = new FallbackNormalizerContainer();
79
        $normalizers->add(\DateTime::class, $direct);
80
        $normalizers->setDefault($fallback);
81
82
        $this->assertSame($direct, $normalizers->getHandler(\DateTime::class));
83
        $this->assertSame($fallback, $normalizers->getHandler(\DateTimeImmutable::class));
84
    }
85
86
    public function testExceptionOnInvalidDefaultHandler()
87
    {
88
        $normalizers = new FallbackNormalizerContainer();
89
        $this->expectExceptionClass(InvalidNormalizerException::class);
90
        $normalizers->setDefault('invalid');
91
    }
92
93
    public function testInvalidClassOrInterfaceName()
94
    {
95
        $normalizers = new FallbackNormalizerContainer();
96
        $this->expectExceptionClass(InvalidClassNameException::class);
97
        $normalizers->add('invalid', function() {});
98
    }
99
100
    public function testAliasForInvalidClass()
101
    {
102
        $normalizers = new FallbackNormalizerContainer();
103
        $this->expectExceptionClass(NormalizerNotFoundException::class);
104
        $normalizers->addAlias(\stdClass::class, \DateTime::class);
105
    }
106
107
    public function testInvalidHandler()
108
    {
109
        $normalizers = new FallbackNormalizerContainer();
110
        $this->expectExceptionClass(InvalidNormalizerException::class);
111
        $normalizers->add(\stdClass::class, 'invalid');
112
    }
113
}
114