22
33namespace PetrKnap \Php \Enum \Test ;
44
5- use PetrKnap \Php \Enum \EnumException ;
5+ use PetrKnap \Php \Enum \Enum ;
6+ use PetrKnap \Php \Enum \Exception \EnumException ;
7+ use PetrKnap \Php \Enum \Exception \EnumNotFoundException ;
68use PetrKnap \Php \Enum \Test \EnumTest \MyBoolean ;
79
810class EnumTest extends \PHPUnit_Framework_TestCase
911{
10- public function goodKeyProvider ()
12+ /**
13+ * @dataProvider dataCallStaticsWorks
14+ * @param string $name
15+ * @param mixed|EnumException $expectedValue
16+ */
17+ public function testCallStaticsWorks ($ name , $ expectedValue )
1118 {
12- return array (array ("MY_TRUE " , 1 ), array ("MY_FALSE " , 2 ));
19+ if ($ expectedValue instanceof EnumException) {
20+ $ this ->setExpectedException (get_class ($ expectedValue ));
21+ }
22+
23+ $ this ->assertSame ($ expectedValue , MyBoolean::__callStatic ($ name , array ())->getValue ());
1324 }
1425
15- public function wrongKeyProvider ()
26+ public function dataCallStaticsWorks ()
1627 {
17- return array (array ("MY_NULL " ), array ("MY_VOID " ));
28+ return array (
29+ array ("MY_TRUE " , 1 ),
30+ array ("MY_FALSE " , 2 ),
31+ array ("MY_NULL " , new EnumNotFoundException ())
32+ );
1833 }
1934
2035 /**
21- * @covers EnumMock::__callStatic
22- * @dataProvider goodKeyProvider
23- *
24- * @param string $name
36+ * @dataProvider dataGetEnumByValueWorks
2537 * @param mixed $value
38+ * @param Enum|EnumException $expectedEnum
2639 */
27- public function testMagicConstruction_GoodKey ( $ name , $ value )
40+ public function testGetEnumByValueWorks ( $ value , $ expectedEnum )
2841 {
29- /** @var MyBoolean $enum */
30- $ enum = MyBoolean::$ name ();
42+ if ($ expectedEnum instanceof EnumException) {
43+ $ this ->setExpectedException (get_class ($ expectedEnum ));
44+ }
3145
32- $ this ->assertInstanceOf (MyBoolean::getClass (), $ enum );
33- $ this ->assertSame ($ name , $ enum ->getName ());
34- $ this ->assertSame ($ value , $ enum ->getValue ());
46+ $ this ->assertSame ($ expectedEnum , MyBoolean::getEnumByValue ($ value ));
3547 }
3648
37- /**
38- * @covers EnumMock::__callStatic
39- * @dataProvider wrongKeyProvider
40- *
41- * @param string $name
42- */
43- public function testMagicConstruction_WrongKey ($ name )
49+ public function dataGetEnumByValueWorks ()
4450 {
45- $ this -> setExpectedException (
46- get_class ( new EnumException ()),
47- "" ,
48- EnumException:: OUT_OF_RANGE
51+ return array (
52+ array ( 1 , MyBoolean:: MY_TRUE ()),
53+ array ( 2 , MyBoolean:: MY_FALSE ()) ,
54+ array ( 3 , new EnumNotFoundException ())
4955 );
50-
51- MyBoolean::$ name ();
5256 }
5357
54- /**
55- * @covers EnumMock::__callStatic
56- */
57- public function testComparable ()
58+ public function testComparableWorks ()
5859 {
5960 $ this ->assertSame (MyBoolean::MY_TRUE (), MyBoolean::MY_TRUE ());
6061 $ this ->assertNotSame (MyBoolean::MY_TRUE (), MyBoolean::MY_FALSE ());
@@ -63,41 +64,32 @@ public function testComparable()
6364 $ this ->assertFalse (MyBoolean::MY_TRUE () == MyBoolean::MY_FALSE ());
6465 }
6566
66- /**
67- * @covers EnumMock::getMembers
68- * @runInSeparateProcess
69- */
70- public function testGetMembers ()
67+ public function testGetMembersWorks ()
7168 {
72- $ members = MyBoolean::getMembers ();
73-
74- $ this ->assertInternalType ("array " , $ members );
75- $ this ->assertCount (2 , $ members );
76- $ this ->assertArrayHasKey ("MY_TRUE " , $ members );
77- $ this ->assertEquals (1 , $ members ["MY_TRUE " ]);
78- $ this ->assertArrayHasKey ("MY_FALSE " , $ members );
79- $ this ->assertEquals (2 , $ members ["MY_FALSE " ]);
69+ $ this ->assertEquals (array (
70+ "MY_TRUE " => 1 ,
71+ "MY_FALSE " => 2
72+ ), MyBoolean::getMembers ());
8073 }
8174
8275 /**
83- * @dataProvider dataFindByValue
84- * @param mixed $value
85- * @param mixed $expected
76+ * @dataProvider dataToStringWorks
77+ * @param Enum $enum
78+ * @param string $expectedString
8679 */
87- public function testFindByValue ( $ value , $ expected )
80+ public function testToStringWorks ( Enum $ enum , $ expectedString )
8881 {
89- if ( $ expected instanceof \Exception) {
90- $ this ->setExpectedException ( get_class ( $ expected ) );
91- }
92- $ this ->assertSame ($ expected , MyBoolean:: findByValue ( $ value ) );
82+ $ this -> assertSame ( $ expectedString , $ enum -> __toString ());
83+ $ this ->assertSame ( $ expectedString , ( string ) $ enum );
84+ $ this -> assertSame ( $ expectedString , "{ $ enum }" );
85+ $ this ->assertSame ($ expectedString , $ enum . "" );
9386 }
9487
95- public function dataFindByValue ()
88+ public function dataToStringWorks ()
9689 {
97- return [
98- [1 , MyBoolean::MY_TRUE ()],
99- [2 , MyBoolean::MY_FALSE ()],
100- [3 , new EnumException ()]
101- ];
90+ return array (
91+ array (MyBoolean::MY_TRUE (), MyBoolean::getClass () . "::MY_TRUE " ),
92+ array (MyBoolean::MY_FALSE (), MyBoolean::getClass () . "::MY_FALSE " )
93+ );
10294 }
10395}
0 commit comments