-
Notifications
You must be signed in to change notification settings - Fork 3
/
non-strict-value-comparison.phpt
88 lines (72 loc) · 2.41 KB
/
non-strict-value-comparison.phpt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
declare(strict_types=1);
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
/**
* @method static Enum1 VALUE1()
* @method static Enum1 VALUE2()
*/
final class Enum1 extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
protected const VALUE1 = 'value1';
protected const VALUE2 = 'value2';
}
$val1 = Enum1::VALUE1();
$val2 = Enum1::VALUE2();
/** intentionally non-strict @noinspection TypeUnsafeComparisonInspection PhpNonStrictObjectEqualityInspection */
Assert::true($val1 == Enum1::VALUE1());
/** intentionally non-strict @noinspection TypeUnsafeComparisonInspection PhpNonStrictObjectEqualityInspection */
Assert::true($val2 == Enum1::VALUE2());
/** intentionally non-strict @noinspection TypeUnsafeComparisonInspection PhpNonStrictObjectEqualityInspection */
Assert::true($val1 != Enum1::VALUE2());
/** intentionally non-strict @noinspection TypeUnsafeComparisonInspection PhpNonStrictObjectEqualityInspection */
Assert::true($val2 != Enum1::VALUE1());
$switchResult = 0;
switch ($val1) {
case Enum1::VALUE1():
$switchResult = 1;
break;
case Enum1::VALUE2():
$switchResult = 2;
break;
default:
$switchResult = 3;
break;
}
Assert::same(1, $switchResult);
// Check enums are handled well in in_array() function
Assert::true(
in_array(Enum1::VALUE1(), [ Enum1::VALUE1(), 'no-match' ], true),
'List of enums contains item - strict check'
);
Assert::true(
in_array(Enum1::VALUE1(), [ Enum1::VALUE1(), 'no-match' ], false),
'List of enums contains item - loose check'
);
Assert::false(
in_array(Enum1::VALUE1(), [ Enum1::VALUE2(), 'no-match' ], true),
'List of enums doesnt contain item - strict check'
);
Assert::false(
in_array(Enum1::VALUE1(), [ Enum1::VALUE2(), 'no-match' ], false),
'List of enums doesnt contain item - loose check'
);
/**
* @method static Enum2 ONE()
* @method static Enum2 TWO()
* @method static Enum2 THREE()
*/
final class Enum2 extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
protected const ONE = 'value1';
protected const TWO = 'value2';
protected const THREE = 'value3';
}
// array_intersect
$first_array = [Enum2::ONE(), Enum2::TWO()];
$second_array = [Enum2::TWO(), Enum2::THREE()];
Assert::equal([Enum2::TWO()], array_values(array_intersect($first_array, $second_array)));
Assert::equal([Enum2::TWO()], array_values(array_intersect($second_array, $first_array)));
Assert::equal($first_array, array_values(array_intersect($first_array, $first_array)));