EnumTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 35.59 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 21
loc 59
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testEnumMethods() 0 8 1
A testValidation() 0 7 1
A testCreateByName() 7 7 1
A testFailedCreateByName() 0 4 1
A testCreateByValue() 7 7 1
A testCreateByStaticFunction() 7 7 1
A testFailedCreateByValue() 0 4 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
3
namespace yii2mod\enum\tests;
4
5
use yii2mod\enum\tests\data\PostStatus;
6
7
/**
8
 * Class EnumTest
9
 *
10
 * @package yii2mod\enum\tests
11
 */
12
class EnumTest extends TestCase
13
{
14
    public function testEnumMethods()
15
    {
16
        $this->assertEquals(['PENDING', 'APPROVED', 'REJECTED', 'POSTPONED'], PostStatus::getConstantsByValue());
17
        $this->assertEquals(['PENDING' => 0, 'APPROVED' => 1, 'REJECTED' => 2, 'POSTPONED' => 3], PostStatus::getConstantsByName());
18
        $this->assertEquals(['Pending', 'Approved', 'Rejected', 'Postponed'], PostStatus::listData());
19
        $this->assertEquals('Pending', PostStatus::getLabel(PostStatus::PENDING));
20
        $this->assertEquals(1, PostStatus::getValueByName('Approved'));
21
    }
22
23
    public function testValidation()
24
    {
25
        $this->assertFalse(PostStatus::isValidName(1));
26
        $this->assertTrue(PostStatus::isValidName('APPROVED'));
27
        $this->assertTrue(PostStatus::isValidValue(1));
28
        $this->assertFalse(PostStatus::isValidValue('APPROVED'));
29
    }
30
31 View Code Duplication
    public function testCreateByName()
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...
32
    {
33
        $enum = PostStatus::createByName('APPROVED');
34
35
        $this->assertEquals(PostStatus::APPROVED, $enum->getValue());
36
        $this->assertTrue(array_key_exists($enum->getName(), PostStatus::getConstantsByName()));
37
    }
38
39
    /**
40
     * @expectedException \UnexpectedValueException
41
     */
42
    public function testFailedCreateByName()
43
    {
44
        PostStatus::createByName('not existing name');
45
    }
46
47 View Code Duplication
    public function testCreateByValue()
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...
48
    {
49
        $enum = PostStatus::createByValue(PostStatus::APPROVED);
50
51
        $this->assertEquals(PostStatus::APPROVED, $enum->getValue());
52
        $this->assertTrue(array_key_exists($enum->getName(), PostStatus::getConstantsByName()));
53
    }
54
55 View Code Duplication
    public function testCreateByStaticFunction()
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...
56
    {
57
        $enum = PostStatus::APPROVED();
58
59
        $this->assertEquals(PostStatus::APPROVED, $enum->getValue());
60
        $this->assertTrue(array_key_exists($enum->getName(), PostStatus::getConstantsByName()));
61
    }
62
63
    /**
64
     * @expectedException \UnexpectedValueException
65
     */
66
    public function testFailedCreateByValue()
67
    {
68
        PostStatus::createByValue('not existing value');
69
    }
70
}
71