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() |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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
|
|
|
|
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.