Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Commit

Permalink
Implemented convenience methods. Closes #4.
Browse files Browse the repository at this point in the history
  • Loading branch information
Erin Millard committed Mar 24, 2012
1 parent 651f401 commit 7dd8697
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 22 deletions.
58 changes: 43 additions & 15 deletions src/Eloquent/Enumeration/Enumeration.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,47 @@ public static final function byName($name)
}

/**
* @param string $name
* @param array $arguments
* @param scalar $value
*
* @return Enumeration
*/
public static final function __callStatic($name, array $arguments)
public static final function byValue($value)
{
return static::byName($name);
return static::byName(static::nameByValue($value));
}

/**
* @param string $name
*
* @return scalar
*/
public static final function valueByName($name)
{
$values = static::values();
if (!array_key_exists($name, $values))
{
throw new Exception\UndefinedEnumerationException(get_called_class(), $name);
}

return $values[$name];
}

/**
* @param scalar $value
*
* @return string
*/
public static final function nameByValue($value)
{
foreach (static::values() as $name => $thisValue)
{
if ($thisValue === $value)
{
return $name;
}
}

throw new Exception\UndefinedEnumerationValueException(get_called_class(), $value);
}

/**
Expand All @@ -62,18 +95,13 @@ public static final function values()

/**
* @param string $name
* @param array $arguments
*
* @return scalar
* @return Enumeration
*/
public static final function valueByName($name)
public static final function __callStatic($name, array $arguments)
{
$values = static::values();
if (!array_key_exists($name, $values))
{
throw new Exception\UndefinedEnumerationException(get_called_class(), $name);
}

return $values[$name];
return static::byName($name);
}

protected static function initialize() {}
Expand All @@ -100,15 +128,15 @@ protected function __construct($name)
/**
* @return string
*/
public function name()
public final function name()
{
return $this->name;
}

/**
* @return scalar
*/
public function value()
public final function value()
{
return $this->value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Enumeration package.
*
* Copyright © 2011 Erin Millard
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Eloquent\Enumeration\Exception;

final class UndefinedEnumerationValueException extends LogicException
{
/**
* @param string $class
* @param scalar $value
* @param \Exception $previous
*/
public function __construct($class, $value, \Exception $previous = null)
{
$message =
"No enumeration with value "
.var_export($value, true)
." defined in class '"
.$class
."'."
;

parent::__construct($message, $previous);
}
}
116 changes: 109 additions & 7 deletions test/suite/src/Eloquent/Enumeration/EnumerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,31 @@ protected function setUp()
parent::setUp();

TestEnumeration::resetCalls();

$reflector = new \ReflectionClass(__NAMESPACE__.'\Enumeration');

$enumerationsProperty = $reflector->getProperty('enumerations');
$enumerationsProperty->setAccessible(true);
$enumerationsProperty->setValue(null, array());

$valuesProperty = $reflector->getProperty('values');
$valuesProperty->setAccessible(true);
$valuesProperty->setValue(null, array());
}

/**
* @covers Eloquent\Enumeration\Enumeration
* @group core
*/
public function testCallStatic()
public function testByName()
{
$foo = TestEnumeration::FOO();
$bar = TestEnumeration::BAR();
$foo = TestEnumeration::byName('FOO');
$bar = TestEnumeration::byName('BAR');

$this->assertInstanceOf('Eloquent\Enumeration\Test\Fixture\TestEnumeration', $foo);
$this->assertInstanceOf('Eloquent\Enumeration\Test\Fixture\TestEnumeration', $bar);
$this->assertSame($foo, TestEnumeration::FOO());
$this->assertSame($bar, TestEnumeration::BAR());
$this->assertSame($foo, TestEnumeration::byName('FOO'));
$this->assertSame($bar, TestEnumeration::byName('BAR'));
$this->assertNotEquals($foo, $bar);

$this->assertSame(array(
Expand All @@ -51,10 +61,76 @@ public function testCallStatic()
* @covers Eloquent\Enumeration\Enumeration
* @group core
*/
public function testCallStaticFailure()
public function testByNameFailure()
{
$this->setExpectedException('Eloquent\Enumeration\Exception\UndefinedEnumerationException');
TestEnumeration::QUX();
TestEnumeration::byName('QUX');
}

/**
* @covers Eloquent\Enumeration\Enumeration
* @group core
*/
public function testByValue()
{
$foo = TestEnumeration::byValue('oof');
$bar = TestEnumeration::byValue('rab');

$this->assertInstanceOf('Eloquent\Enumeration\Test\Fixture\TestEnumeration', $foo);
$this->assertInstanceOf('Eloquent\Enumeration\Test\Fixture\TestEnumeration', $bar);
$this->assertSame($foo, TestEnumeration::byValue('oof'));
$this->assertSame($bar, TestEnumeration::byValue('rab'));
$this->assertNotEquals($foo, $bar);
}

/**
* @covers Eloquent\Enumeration\Enumeration
* @group core
*/
public function testByValueFailure()
{
$this->setExpectedException('Eloquent\Enumeration\Exception\UndefinedEnumerationValueException');
TestEnumeration::byValue('xuq');
}

/**
* @covers Eloquent\Enumeration\Enumeration
* @group core
*/
public function testValueByName()
{
$this->assertSame('oof', TestEnumeration::valueByName('FOO'));
$this->assertSame('rab', TestEnumeration::valueByName('BAR'));
}

/**
* @covers Eloquent\Enumeration\Enumeration
* @group core
*/
public function testValueByNameFailure()
{
$this->setExpectedException('Eloquent\Enumeration\Exception\UndefinedEnumerationException');
TestEnumeration::valueByName('QUX');
}

/**
* @covers Eloquent\Enumeration\Enumeration
* @group core
*/
public function testNameByValue()
{
$this->assertSame('FOO', TestEnumeration::nameByValue('oof'));
$this->assertSame('BAR', TestEnumeration::nameByValue('rab'));
}

/**
* @covers Eloquent\Enumeration\Enumeration
* @group core
*/
public function testNameByValueFailure()
{
$this->setExpectedException('Eloquent\Enumeration\Exception\UndefinedEnumerationValueException');
TestEnumeration::nameByValue('xuq');
}

/**
Expand All @@ -77,6 +153,32 @@ public function testValues()
), ExtendedTestEnumeration::values());
}

/**
* @covers Eloquent\Enumeration\Enumeration
* @group core
*/
public function testCallStatic()
{
$foo = TestEnumeration::FOO();
$bar = TestEnumeration::BAR();

$this->assertInstanceOf('Eloquent\Enumeration\Test\Fixture\TestEnumeration', $foo);
$this->assertInstanceOf('Eloquent\Enumeration\Test\Fixture\TestEnumeration', $bar);
$this->assertSame($foo, TestEnumeration::FOO());
$this->assertSame($bar, TestEnumeration::BAR());
$this->assertNotEquals($foo, $bar);
}

/**
* @covers Eloquent\Enumeration\Enumeration
* @group core
*/
public function testCallStaticFailure()
{
$this->setExpectedException('Eloquent\Enumeration\Exception\UndefinedEnumerationException');
TestEnumeration::QUX();
}

/**
* @covers Eloquent\Enumeration\Enumeration
* @group core
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Enumeration package.
*
* Copyright © 2011 Erin Millard
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Eloquent\Enumeration\Exception;

class UndefinedEnumerationValueExceptionTest extends \Eloquent\Enumeration\Test\TestCase
{
/**
* @covers Eloquent\Enumeration\Exception\UndefinedEnumerationValueException
* @covers Eloquent\Enumeration\Exception\LogicException
* @covers Eloquent\Enumeration\Exception\Exception
* @group exceptions
* @group core
*/
public function testException()
{
$class = 'foo';
$value = 'bar';
$exception = new UndefinedEnumerationValueException($class, $value);
$expectedMessage = "No enumeration with value 'bar' defined in class 'foo'.";

$this->assertSame($expectedMessage, $exception->getMessage());
}
}

0 comments on commit 7dd8697

Please sign in to comment.