8
8
9
9
/**
10
10
* @author Matthieu Napoli <[email protected] >
11
- * @author Daniel Costa <[email protected]
11
+ * @author Daniel Costa <[email protected] >
12
+ * @author Mirosław Filip <[email protected] >
12
13
*/
13
14
class EnumTest extends \PHPUnit_Framework_TestCase
14
15
{
@@ -38,42 +39,44 @@ public function testGetKey()
38
39
}
39
40
40
41
/**
41
- * @expectedException \UnexpectedValueException
42
+ * @dataProvider invalidValueProvider
42
43
*/
43
- public function testInvalidValueString ( )
44
+ public function testCreatingEnumWithInvalidValue ( $ value )
44
45
{
45
- new EnumFixture ("test " );
46
- }
46
+ $ this ->setExpectedException (
47
+ '\UnexpectedValueException ' ,
48
+ 'Value \'' . $ value . '\' is not part of the enum MyCLabs\Tests\Enum\EnumFixture '
49
+ );
47
50
48
- /**
49
- * @expectedException \UnexpectedValueException
50
- */
51
- public function testInvalidValueInt ()
52
- {
53
- new EnumFixture (1234 );
51
+ new EnumFixture ($ value );
54
52
}
55
53
56
54
/**
57
- * @expectedException \UnexpectedValueException
55
+ * Contains values not existing in EnumFixture
56
+ * @return array
58
57
*/
59
- public function testInvalidValueEmpty ()
60
- {
61
- new EnumFixture (null );
58
+ public function invalidValueProvider () {
59
+ return array (
60
+ "string " => array ('test ' ),
61
+ "int " => array (1234 ),
62
+ );
62
63
}
63
64
64
65
/**
65
66
* __toString()
67
+ * @dataProvider toStringProvider
66
68
*/
67
- public function testToString ()
69
+ public function testToString ($ expected , $ enumObject )
68
70
{
69
- $ value = new EnumFixture (EnumFixture::FOO );
70
- $ this ->assertEquals (EnumFixture::FOO , (string ) $ value );
71
-
72
- $ value = new EnumFixture (EnumFixture::BAR );
73
- $ this ->assertEquals (EnumFixture::BAR , (string ) $ value );
71
+ $ this ->assertSame ($ expected , (string ) $ enumObject );
72
+ }
74
73
75
- $ value = new EnumFixture (EnumFixture::NUMBER );
76
- $ this ->assertEquals ((string ) EnumFixture::NUMBER , (string ) $ value );
74
+ public function toStringProvider () {
75
+ return array (
76
+ array (EnumFixture::FOO , new EnumFixture (EnumFixture::FOO )),
77
+ array (EnumFixture::BAR , new EnumFixture (EnumFixture::BAR )),
78
+ array ((string ) EnumFixture::NUMBER , new EnumFixture (EnumFixture::NUMBER )),
79
+ );
77
80
}
78
81
79
82
/**
@@ -82,13 +85,17 @@ public function testToString()
82
85
public function testKeys ()
83
86
{
84
87
$ values = EnumFixture::keys ();
85
- $ this ->assertInternalType ("array " , $ values );
86
88
$ expectedValues = array (
87
89
"FOO " ,
88
90
"BAR " ,
89
91
"NUMBER " ,
92
+ "PROBLEMATIC_NUMBER " ,
93
+ "PROBLEMATIC_NULL " ,
94
+ "PROBLEMATIC_EMPTY_STRING " ,
95
+ "PROBLEMATIC_BOOLEAN_FALSE " ,
90
96
);
91
- $ this ->assertEquals ($ expectedValues , $ values );
97
+
98
+ $ this ->assertSame ($ expectedValues , $ values );
92
99
}
93
100
94
101
/**
@@ -97,13 +104,17 @@ public function testKeys()
97
104
public function testToArray ()
98
105
{
99
106
$ values = EnumFixture::toArray ();
100
- $ this ->assertInternalType ("array " , $ values );
101
107
$ expectedValues = array (
102
- "FOO " => EnumFixture::FOO ,
103
- "BAR " => EnumFixture::BAR ,
104
- "NUMBER " => EnumFixture::NUMBER ,
108
+ "FOO " => EnumFixture::FOO ,
109
+ "BAR " => EnumFixture::BAR ,
110
+ "NUMBER " => EnumFixture::NUMBER ,
111
+ "PROBLEMATIC_NUMBER " => EnumFixture::PROBLEMATIC_NUMBER ,
112
+ "PROBLEMATIC_NULL " => EnumFixture::PROBLEMATIC_NULL ,
113
+ "PROBLEMATIC_EMPTY_STRING " => EnumFixture::PROBLEMATIC_EMPTY_STRING ,
114
+ "PROBLEMATIC_BOOLEAN_FALSE " => EnumFixture::PROBLEMATIC_BOOLEAN_FALSE ,
105
115
);
106
- $ this ->assertEquals ($ expectedValues , $ values );
116
+
117
+ $ this ->assertSame ($ expectedValues , $ values );
107
118
}
108
119
109
120
/**
@@ -128,11 +139,29 @@ public function testBadStaticAccess()
128
139
129
140
/**
130
141
* isValid()
142
+ * @dataProvider isValidProvider
131
143
*/
132
- public function testIsValid ()
144
+ public function testIsValid ($ value , $ isValid )
133
145
{
134
- $ this ->assertTrue (EnumFixture::isValid ('foo ' ));
135
- $ this ->assertFalse (EnumFixture::isValid ('baz ' ));
146
+ $ this ->assertSame ($ isValid , EnumFixture::isValid ($ value ));
147
+ }
148
+
149
+ public function isValidProvider () {
150
+ return array (
151
+ /**
152
+ * Valid values
153
+ */
154
+ array ('foo ' , true ),
155
+ array (42 , true ),
156
+ array (null , true ),
157
+ array (0 , true ),
158
+ array ('' , true ),
159
+ array (false , true ),
160
+ /**
161
+ * Invalid values
162
+ */
163
+ array ('baz ' , false )
164
+ );
136
165
}
137
166
138
167
/**
@@ -150,6 +179,9 @@ public function testIsValidKey()
150
179
public function testSearch ()
151
180
{
152
181
$ this ->assertEquals ('FOO ' , EnumFixture::search ('foo ' ));
153
- $ this ->assertNotEquals ('FOO ' , EnumFixture::isValidKey ('baz ' ));
182
+ /**
183
+ * @see https://github.com/myclabs/php-enum/issues/9
184
+ */
185
+ $ this ->assertEquals (EnumFixture::PROBLEMATIC_NUMBER , EnumFixture::search (1 ));
154
186
}
155
187
}
0 commit comments