88
99/**
1010 * @author Matthieu Napoli <matthieu@mnapoli.fr>
11- * @author Daniel Costa <danielcosta@gmail.com
11+ * @author Daniel Costa <danielcosta@gmail.com>
12+ * @author Mirosław Filip <mirfilip@gmail.com>
1213 */
1314class EnumTest extends \PHPUnit_Framework_TestCase
1415{
@@ -38,42 +39,44 @@ public function testGetKey()
3839 }
3940
4041 /**
41- * @expectedException \UnexpectedValueException
42+ * @dataProvider invalidValueProvider
4243 */
43- public function testInvalidValueString ( )
44+ public function testCreatingEnumWithInvalidValue ( $ value )
4445 {
45- new EnumFixture ("test " );
46- }
46+ $ this ->setExpectedException (
47+ '\UnexpectedValueException ' ,
48+ 'Value \'' . $ value . '\' is not part of the enum MyCLabs\Tests\Enum\EnumFixture '
49+ );
4750
48- /**
49- * @expectedException \UnexpectedValueException
50- */
51- public function testInvalidValueInt ()
52- {
53- new EnumFixture (1234 );
51+ new EnumFixture ($ value );
5452 }
5553
5654 /**
57- * @expectedException \UnexpectedValueException
55+ * Contains values not existing in EnumFixture
56+ * @return array
5857 */
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+ );
6263 }
6364
6465 /**
6566 * __toString()
67+ * @dataProvider toStringProvider
6668 */
67- public function testToString ()
69+ public function testToString ($ expected , $ enumObject )
6870 {
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+ }
7473
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+ );
7780 }
7881
7982 /**
@@ -82,13 +85,17 @@ public function testToString()
8285 public function testKeys ()
8386 {
8487 $ values = EnumFixture::keys ();
85- $ this ->assertInternalType ("array " , $ values );
8688 $ expectedValues = array (
8789 "FOO " ,
8890 "BAR " ,
8991 "NUMBER " ,
92+ "PROBLEMATIC_NUMBER " ,
93+ "PROBLEMATIC_NULL " ,
94+ "PROBLEMATIC_EMPTY_STRING " ,
95+ "PROBLEMATIC_BOOLEAN_FALSE " ,
9096 );
91- $ this ->assertEquals ($ expectedValues , $ values );
97+
98+ $ this ->assertSame ($ expectedValues , $ values );
9299 }
93100
94101 /**
@@ -97,13 +104,17 @@ public function testKeys()
97104 public function testToArray ()
98105 {
99106 $ values = EnumFixture::toArray ();
100- $ this ->assertInternalType ("array " , $ values );
101107 $ 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 ,
105115 );
106- $ this ->assertEquals ($ expectedValues , $ values );
116+
117+ $ this ->assertSame ($ expectedValues , $ values );
107118 }
108119
109120 /**
@@ -128,11 +139,29 @@ public function testBadStaticAccess()
128139
129140 /**
130141 * isValid()
142+ * @dataProvider isValidProvider
131143 */
132- public function testIsValid ()
144+ public function testIsValid ($ value , $ isValid )
133145 {
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+ );
136165 }
137166
138167 /**
@@ -150,6 +179,9 @@ public function testIsValidKey()
150179 public function testSearch ()
151180 {
152181 $ 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 ));
154186 }
155187}
0 commit comments