-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
223 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.DS_Store | ||
nbproject/* | ||
.idea/* | ||
vendor/* | ||
composer.phar | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
language: php | ||
|
||
php: | ||
- 5.3 | ||
- 5.4 | ||
- 5.5 | ||
|
||
before_script: | ||
- wget http://getcomposer.org/composer.phar | ||
- php composer.phar install | ||
|
||
script: phpunit --configuration phpunit.xml tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
PHP Enum implementation inspired from SplEnum | ||
======== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "myc-sense/php-enum", | ||
"type": "library", | ||
"description": "PHP Enum implementation", | ||
"autoload": { | ||
"psr-0": { | ||
"Mycsense": "src/" | ||
} | ||
}, | ||
"require": { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
phpunit -c phpunit.xml | ||
--> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
bootstrap="./tests/bootstrap.php"> | ||
|
||
<testsuites> | ||
<testsuite name="Enum Test Suite"> | ||
<directory>./tests/UnitTest/Mycsense/Enum</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* @author Matthieu Napoli <[email protected]> | ||
*/ | ||
|
||
namespace Mycsense\Enum; | ||
|
||
/** | ||
* Base Enum class | ||
* | ||
* Create an enum by implementing this class and adding class constants. | ||
*/ | ||
abstract class Enum | ||
{ | ||
|
||
/** | ||
* Enum value | ||
* @var mixed | ||
*/ | ||
protected $value; | ||
|
||
/** | ||
* Creates a new value of some type | ||
* @param mixed $value | ||
* @throws \UnexpectedValueException if incompatible type is given. | ||
*/ | ||
public function __construct($value) | ||
{ | ||
$possibleValues = self::toArray(); | ||
if (! in_array($value, $possibleValues)) { | ||
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class()); | ||
} | ||
$this->value = $value; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getValue() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function __toString() | ||
{ | ||
return (string) $this->value; | ||
} | ||
|
||
/** | ||
* Returns all possible values as an array | ||
* @return array | ||
*/ | ||
public static function toArray() | ||
{ | ||
$reflection = new \ReflectionClass(get_called_class()); | ||
return $reflection->getConstants(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
/** | ||
* @author Matthieu Napoli <[email protected]> | ||
*/ | ||
|
||
namespace UnitTest\Mycsense\Enum\Enum; | ||
|
||
use Mycsense\Enum\Enum; | ||
|
||
/** | ||
* Enum test | ||
*/ | ||
class EnumTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
/** | ||
* getValue() | ||
*/ | ||
public function testGetValue() | ||
{ | ||
$value = new EnumFixture(EnumFixture::FOO); | ||
$this->assertEquals(EnumFixture::FOO, $value->getValue()); | ||
|
||
$value = new EnumFixture(EnumFixture::BAR); | ||
$this->assertEquals(EnumFixture::BAR, $value->getValue()); | ||
|
||
$value = new EnumFixture(EnumFixture::NUMBER); | ||
$this->assertEquals(EnumFixture::NUMBER, $value->getValue()); | ||
} | ||
|
||
/** | ||
* @expectedException \UnexpectedValueException | ||
*/ | ||
public function testInvalidValue1() | ||
{ | ||
new EnumFixture("test"); | ||
} | ||
|
||
/** | ||
* @expectedException \UnexpectedValueException | ||
*/ | ||
public function testInvalidValue2() | ||
{ | ||
new EnumFixture(1234); | ||
} | ||
|
||
/** | ||
* @expectedException \UnexpectedValueException | ||
*/ | ||
public function testInvalidValue3() | ||
{ | ||
new EnumFixture(null); | ||
} | ||
|
||
/** | ||
* __toString() | ||
*/ | ||
public function testToString() | ||
{ | ||
$value = new EnumFixture(EnumFixture::FOO); | ||
$this->assertEquals(EnumFixture::FOO, (string) $value); | ||
|
||
$value = new EnumFixture(EnumFixture::BAR); | ||
$this->assertEquals(EnumFixture::BAR, (string) $value); | ||
|
||
$value = new EnumFixture(EnumFixture::NUMBER); | ||
$this->assertEquals((string) EnumFixture::NUMBER, (string) $value); | ||
} | ||
|
||
/** | ||
* toArray() | ||
*/ | ||
public function testToArray() | ||
{ | ||
$values = EnumFixture::toArray(); | ||
$this->assertInternalType("array", $values); | ||
$expectedValues = [ | ||
"FOO" => EnumFixture::FOO, | ||
"BAR" => EnumFixture::BAR, | ||
"NUMBER" => EnumFixture::NUMBER, | ||
]; | ||
$this->assertEquals($expectedValues, $values); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Fixture class | ||
*/ | ||
class EnumFixture extends Enum | ||
{ | ||
|
||
const FOO = "foo"; | ||
const BAR = "bar"; | ||
const NUMBER = 42; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
/** | ||
* This file bootstraps the test environment | ||
*/ | ||
|
||
error_reporting(E_ALL | E_STRICT); | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; |