Skip to content
This repository was archived by the owner on Dec 26, 2023. It is now read-only.

Commit dc44a57

Browse files
author
Petr Knap
committed
Added __toString
1 parent 1e63f47 commit dc44a57

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Enum/AbstractEnum.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,12 @@ public static function getEnumByValue($value)
174174
)
175175
);
176176
}
177+
178+
/**
179+
* @return string
180+
*/
181+
public function __toString()
182+
{
183+
return sprintf("%s::%s", get_called_class(), $this->getName());
184+
}
177185
}

tests/Enum/EnumTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,25 @@ public function testGetMembersWorks()
6969
"MY_FALSE" => 2
7070
], MyBoolean::getMembers());
7171
}
72+
73+
/**
74+
* @dataProvider dataToStringWorks
75+
* @param mixed $enum
76+
* @param mixed $expectedString
77+
*/
78+
public function testToStringWorks($enum, $expectedString)
79+
{
80+
$this->assertSame($expectedString, $enum->__toString());
81+
$this->assertSame($expectedString, (string) $enum);
82+
$this->assertSame($expectedString, "{$enum}");
83+
$this->assertSame($expectedString, $enum . "");
84+
}
85+
86+
public function dataToStringWorks()
87+
{
88+
return [
89+
[MyBoolean::MY_TRUE(), MyBoolean::getClass() . "::MY_TRUE"],
90+
[MyBoolean::MY_FALSE(), MyBoolean::getClass() . "::MY_FALSE"],
91+
];
92+
}
7293
}

0 commit comments

Comments
 (0)