Completed
Push — master ( fa5ac7...eb2a5f )
by Jan
06:08
created

EnumTest::testDifferentInstances()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
3
namespace SlevomatEET\Type;
4
5
class EnumTest extends \PHPUnit_Framework_TestCase
6
{
7
8
	public function testEqualsInstances()
9
	{
10
		$review1 = new StatusEnum(StatusEnum::REVIEW);
11
		$review2 = new StatusEnum(StatusEnum::REVIEW);
12
13
		$this->assertEquals($review1, $review2);
14
	}
15
16
	public function testDifferentInstances()
17
	{
18
		$review = new StatusEnum(StatusEnum::REVIEW);
19
		$draft = new StatusEnum(StatusEnum::DRAFT);
20
21
		$this->assertNotEquals($review, $draft);
22
	}
23
24
	public function testGetValue()
25
	{
26
		$review = new StatusEnum(StatusEnum::REVIEW);
27
28
		$this->assertSame(StatusEnum::REVIEW, $review->getValue());
29
	}
30
31
	public function testEqualsValue()
32
	{
33
		$review = new StatusEnum(StatusEnum::REVIEW);
34
		$draft = new StatusEnum(StatusEnum::DRAFT);
35
36
		$this->assertTrue($review->equalsValue(StatusEnum::REVIEW));
37
		$this->assertFalse($draft->equalsValue(StatusEnum::REVIEW));
38
	}
39
40
	public function testEquals()
41
	{
42
		$review1 = new StatusEnum(StatusEnum::REVIEW);
43
		$review2 = new StatusEnum(StatusEnum::REVIEW);
44
		$draft = new StatusEnum(StatusEnum::DRAFT);
45
46
		$this->assertTrue($review1->equals($review2));
0 ignored issues
show
Documentation introduced by
$review2 is of type object<SlevomatEET\Type\StatusEnum>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
		$this->assertFalse($review1->equals($draft));
0 ignored issues
show
Documentation introduced by
$draft is of type object<SlevomatEET\Type\StatusEnum>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
49
		$admin = new RoleEnum(RoleEnum::ADMIN);
50
		try {
51
			$review1->equals($admin);
0 ignored issues
show
Documentation introduced by
$admin is of type object<SlevomatEET\Type\RoleEnum>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
			$this->fail();
53
54
		} catch (InvalidEnumTypeException $e) {
55
			$this->assertSame($admin, $e->getEnum());
56
			$this->assertSame(get_class($review1), $e->getExpectedClass());
57
		}
58
	}
59
60
	public function testInvalidEnumValue()
61
	{
62
		try {
63
			new StatusEnum(10);
64
			$this->fail();
65
66
		} catch (InvalidEnumValueException $e) {
67
			$this->assertSame('Invalid enum value \'10\' (integer). Available values: 1, 2, 3', $e->getMessage());
68
			$this->assertSame(10, $e->getValue());
69
			$this->assertEquals([
70
				'DRAFT' => StatusEnum::DRAFT,
71
				'REVIEW' => StatusEnum::REVIEW,
72
				'PUBLISHED' => StatusEnum::PUBLISHED,
73
			], $e->getAvailableValues());
74
		}
75
	}
76
77
	public function testInvalidEnumValueType()
78
	{
79
		try {
80
			new StatusEnum('10');
81
			$this->fail();
82
83
		} catch (InvalidEnumValueException $e) {
84
			$this->assertSame('Invalid enum value \'10\' (string). Available values: 1, 2, 3', $e->getMessage());
85
			$this->assertSame('10', $e->getValue());
86
		}
87
	}
88
89
}
90