Skip to content

Commit 77972c9

Browse files
committed
'__toString()' magic method returns the current value as string
removed '__invoke()' magic method
1 parent 0d4051e commit 77972c9

File tree

2 files changed

+95
-108
lines changed

2 files changed

+95
-108
lines changed

src/Mabe/Enum.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ abstract class Mabe_Enum
1414
* @var mixed
1515
*/
1616
protected $value = null;
17-
17+
1818
/**
1919
* An array of available constants
2020
* @var array
@@ -31,7 +31,7 @@ final public function __construct($value = null)
3131
{
3232
$reflectionClass = new ReflectionClass($this);
3333
$this->constants = $reflectionClass->getConstants();
34-
34+
3535
if (func_num_args() > 0) {
3636
$this->setValue($value);
3737
} elseif (!in_array($this->value, $this->constants, true)) {
@@ -47,7 +47,7 @@ final public function getConstants()
4747
{
4848
return $this->constants;
4949
}
50-
50+
5151
/**
5252
* Select a new value
5353
* @param mixed $value
@@ -99,16 +99,6 @@ final public function getName()
9999
*/
100100
final public function __toString()
101101
{
102-
return $this->getName();
103-
}
104-
105-
/**
106-
* Get the current selected value
107-
* @return mixed
108-
* @see getValue()
109-
*/
110-
final public function __invoke()
111-
{
112-
return $this->getValue();
102+
return (string) $this->value;
113103
}
114104
}

tests/MabeTest/EnumTest.php

Lines changed: 91 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -20,117 +20,114 @@
2020

2121
class MabeTest_EnumTest extends PHPUnit_Framework_TestCase
2222
{
23-
24-
public function testEnumWithDefaultValue()
25-
{
26-
$enum = new EnumWithDefaultValue();
27-
28-
$this->assertSame(array(
29-
'ONE' => 1,
30-
'TWO' => 2,
31-
), $enum->getConstants());
32-
33-
$this->assertSame(1, $enum->getValue());
34-
$this->assertSame(1, $enum->__invoke());
35-
36-
$this->assertSame('ONE', $enum->getName());
37-
$this->assertSame('ONE', $enum->__toString());
38-
}
39-
40-
public function testEnumWithNullAsDefaultValue()
41-
{
42-
$enum = new EnumWithNullAsDefaultValue();
43-
44-
$this->assertSame(array(
45-
'NONE' => null,
46-
'ONE' => 1,
47-
'TWO' => 2,
48-
), $enum->getConstants());
49-
50-
$this->assertNull($enum->getValue());
51-
$this->assertNull($enum->__invoke());
52-
53-
$this->assertSame('NONE', $enum->getName());
54-
$this->assertSame('NONE', $enum->__toString());
55-
}
56-
57-
public function testEnumWithoutDefaultValue()
58-
{
59-
$this->setExpectedException('InvalidArgumentException');
60-
new EnumWithoutDefaultValue();
61-
}
62-
63-
public function testEnumInheritance()
64-
{
65-
$enum = new EnumInheritance(EnumInheritance::ONE);
66-
$this->assertSame(EnumInheritance::ONE, $enum->getValue());
67-
68-
$enum = new EnumInheritance(EnumInheritance::INHERITACE);
69-
$this->assertSame(EnumInheritance::INHERITACE, $enum->getValue());
70-
}
71-
72-
public function testChangeValueOnConstructor()
73-
{
74-
$enum = new EnumWithoutDefaultValue(1);
75-
76-
$this->assertSame(1, $enum->getValue());
77-
$this->assertSame(1, $enum->__invoke());
78-
79-
$this->assertSame('ONE', $enum->getName());
80-
$this->assertSame('ONE', $enum->__toString());
81-
}
82-
83-
public function testChangeValueOnConstructorThrowsInvalidArgumentExceptionOnStrictComparison()
84-
{
85-
$this->setExpectedException('InvalidArgumentException');
86-
$enum = new EnumWithoutDefaultValue('1');
87-
}
88-
89-
public function testSetValue()
90-
{
91-
$enum = new EnumWithDefaultValue();
92-
$enum->setValue(2);
93-
94-
$this->assertSame(2, $enum->getValue());
95-
$this->assertSame(2, $enum->__invoke());
96-
97-
$this->assertSame('TWO', $enum->getName());
98-
$this->assertSame('TWO', $enum->__toString());
99-
}
100-
101-
public function testSetValueThrowsInvalidArgumentExceptionOnStrictComparison()
102-
{
103-
$this->setExpectedException('InvalidArgumentException');
104-
$enum = new EnumWithDefaultValue();
105-
$enum->setValue('2');
106-
}
107-
23+
24+
public function testEnumWithDefaultValue()
25+
{
26+
$enum = new EnumWithDefaultValue();
27+
28+
$this->assertSame(array(
29+
'ONE' => 1,
30+
'TWO' => 2,
31+
), $enum->getConstants());
32+
33+
$this->assertSame(1, $enum->getValue());
34+
$this->assertSame('1', $enum->__toString());
35+
36+
$this->assertSame('ONE', $enum->getName());
37+
}
38+
39+
public function testGetNameReturnsConstantNameOfCurrentValue()
40+
{
41+
$enum = new EnumWithoutDefaultValue(EnumWithoutDefaultValue::ONE);
42+
$this->assertSame('ONE', $enum->getName());
43+
}
44+
45+
public function testToStringMagicMethodReturnsValueAsString()
46+
{
47+
$enum = new EnumWithoutDefaultValue(EnumWithoutDefaultValue::ONE);
48+
$this->assertSame('1', $enum->__toString());
49+
}
50+
51+
public function testEnumWithNullAsDefaultValue()
52+
{
53+
$enum = new EnumWithNullAsDefaultValue();
54+
55+
$this->assertSame(array(
56+
'NONE' => null,
57+
'ONE' => 1,
58+
'TWO' => 2,
59+
), $enum->getConstants());
60+
61+
$this->assertNull($enum->getValue());
62+
}
63+
64+
public function testEnumWithoutDefaultValue()
65+
{
66+
$this->setExpectedException('InvalidArgumentException');
67+
new EnumWithoutDefaultValue();
68+
}
69+
70+
public function testEnumInheritance()
71+
{
72+
$enum = new EnumInheritance(EnumInheritance::ONE);
73+
$this->assertSame(EnumInheritance::ONE, $enum->getValue());
74+
75+
$enum = new EnumInheritance(EnumInheritance::INHERITACE);
76+
$this->assertSame(EnumInheritance::INHERITACE, $enum->getValue());
77+
}
78+
79+
public function testChangeValueOnConstructor()
80+
{
81+
$enum = new EnumWithoutDefaultValue(1);
82+
$this->assertSame(1, $enum->getValue());
83+
}
84+
85+
public function testChangeValueOnConstructorThrowsInvalidArgumentExceptionOnStrictComparison()
86+
{
87+
$this->setExpectedException('InvalidArgumentException');
88+
$enum = new EnumWithoutDefaultValue('1');
89+
}
90+
91+
public function testSetValue()
92+
{
93+
$enum = new EnumWithDefaultValue();
94+
$enum->setValue(2);
95+
96+
$this->assertSame(2, $enum->getValue());
97+
}
98+
99+
public function testSetValueThrowsInvalidArgumentExceptionOnStrictComparison()
100+
{
101+
$this->setExpectedException('InvalidArgumentException');
102+
$enum = new EnumWithDefaultValue();
103+
$enum->setValue('2');
104+
}
108105
}
109106

110107
class EnumWithDefaultValue extends Mabe_Enum
111108
{
112-
const ONE = 1;
113-
const TWO = 2;
114-
protected $value = 1;
109+
const ONE = 1;
110+
const TWO = 2;
111+
protected $value = 1;
115112
}
116113

117114
class EnumWithNullAsDefaultValue extends Mabe_Enum
118115
{
119-
const NONE = null;
120-
const ONE = 1;
121-
const TWO = 2;
116+
const NONE = null;
117+
const ONE = 1;
118+
const TWO = 2;
122119
}
123120

124121

125122
class EnumWithoutDefaultValue extends Mabe_Enum
126123
{
127-
const ONE = 1;
128-
const TWO = 2;
124+
const ONE = 1;
125+
const TWO = 2;
129126
}
130127

131128
class EnumInheritance extends EnumWithoutDefaultValue
132129
{
133-
const INHERITACE = 'Inheritance';
130+
const INHERITACE = 'Inheritance';
134131
}
135132

136133
class EmptyEnum extends Mabe_Enum

0 commit comments

Comments
 (0)