Skip to content

Commit 618ef64

Browse files
committed
First commit
1 parent d0e275d commit 618ef64

File tree

10 files changed

+223
-1
lines changed

10 files changed

+223
-1
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
nbproject/*
3+
.idea/*
4+
vendor/*
5+
composer.phar
6+
composer.lock

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: php
2+
3+
php:
4+
- 5.3
5+
- 5.4
6+
- 5.5
7+
8+
before_script:
9+
- wget http://getcomposer.org/composer.phar
10+
- php composer.phar install
11+
12+
script: phpunit --configuration phpunit.xml tests

README

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PHP Enum implementation inspired from SplEnum
2+
========

composer.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "myc-sense/php-enum",
3+
"type": "library",
4+
"description": "PHP Enum implementation",
5+
"autoload": {
6+
"psr-0": {
7+
"Mycsense": "src/"
8+
}
9+
},
10+
"require": {
11+
}
12+
}

phpunit.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
phpunit -c phpunit.xml
4+
-->
5+
<phpunit backupGlobals="false"
6+
backupStaticAttributes="false"
7+
colors="true"
8+
convertErrorsToExceptions="true"
9+
convertNoticesToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
processIsolation="false"
12+
stopOnFailure="false"
13+
syntaxCheck="false"
14+
bootstrap="./tests/bootstrap.php">
15+
16+
<testsuites>
17+
<testsuite name="Enum Test Suite">
18+
<directory>./tests/UnitTest/Mycsense/Enum</directory>
19+
</testsuite>
20+
</testsuites>
21+
22+
</phpunit>

src/Mycsense/Enum/Enum.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* @author Matthieu Napoli <[email protected]>
4+
*/
5+
6+
namespace Mycsense\Enum;
7+
8+
/**
9+
* Base Enum class
10+
*
11+
* Create an enum by implementing this class and adding class constants.
12+
*/
13+
abstract class Enum
14+
{
15+
16+
/**
17+
* Enum value
18+
* @var mixed
19+
*/
20+
protected $value;
21+
22+
/**
23+
* Creates a new value of some type
24+
* @param mixed $value
25+
* @throws \UnexpectedValueException if incompatible type is given.
26+
*/
27+
public function __construct($value)
28+
{
29+
$possibleValues = self::toArray();
30+
if (! in_array($value, $possibleValues)) {
31+
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class());
32+
}
33+
$this->value = $value;
34+
}
35+
36+
/**
37+
* @return mixed
38+
*/
39+
public function getValue()
40+
{
41+
return $this->value;
42+
}
43+
44+
/**
45+
* @return string
46+
*/
47+
public function __toString()
48+
{
49+
return (string) $this->value;
50+
}
51+
52+
/**
53+
* Returns all possible values as an array
54+
* @return array
55+
*/
56+
public static function toArray()
57+
{
58+
$reflection = new \ReflectionClass(get_called_class());
59+
return $reflection->getConstants();
60+
}
61+
62+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* @author Matthieu Napoli <[email protected]>
4+
*/
5+
6+
namespace UnitTest\Mycsense\Enum\Enum;
7+
8+
use Mycsense\Enum\Enum;
9+
10+
/**
11+
* Enum test
12+
*/
13+
class EnumTest extends \PHPUnit_Framework_TestCase
14+
{
15+
16+
/**
17+
* getValue()
18+
*/
19+
public function testGetValue()
20+
{
21+
$value = new EnumFixture(EnumFixture::FOO);
22+
$this->assertEquals(EnumFixture::FOO, $value->getValue());
23+
24+
$value = new EnumFixture(EnumFixture::BAR);
25+
$this->assertEquals(EnumFixture::BAR, $value->getValue());
26+
27+
$value = new EnumFixture(EnumFixture::NUMBER);
28+
$this->assertEquals(EnumFixture::NUMBER, $value->getValue());
29+
}
30+
31+
/**
32+
* @expectedException \UnexpectedValueException
33+
*/
34+
public function testInvalidValue1()
35+
{
36+
new EnumFixture("test");
37+
}
38+
39+
/**
40+
* @expectedException \UnexpectedValueException
41+
*/
42+
public function testInvalidValue2()
43+
{
44+
new EnumFixture(1234);
45+
}
46+
47+
/**
48+
* @expectedException \UnexpectedValueException
49+
*/
50+
public function testInvalidValue3()
51+
{
52+
new EnumFixture(null);
53+
}
54+
55+
/**
56+
* __toString()
57+
*/
58+
public function testToString()
59+
{
60+
$value = new EnumFixture(EnumFixture::FOO);
61+
$this->assertEquals(EnumFixture::FOO, (string) $value);
62+
63+
$value = new EnumFixture(EnumFixture::BAR);
64+
$this->assertEquals(EnumFixture::BAR, (string) $value);
65+
66+
$value = new EnumFixture(EnumFixture::NUMBER);
67+
$this->assertEquals((string) EnumFixture::NUMBER, (string) $value);
68+
}
69+
70+
/**
71+
* toArray()
72+
*/
73+
public function testToArray()
74+
{
75+
$values = EnumFixture::toArray();
76+
$this->assertInternalType("array", $values);
77+
$expectedValues = [
78+
"FOO" => EnumFixture::FOO,
79+
"BAR" => EnumFixture::BAR,
80+
"NUMBER" => EnumFixture::NUMBER,
81+
];
82+
$this->assertEquals($expectedValues, $values);
83+
}
84+
85+
}
86+
87+
/**
88+
* Fixture class
89+
*/
90+
class EnumFixture extends Enum
91+
{
92+
93+
const FOO = "foo";
94+
const BAR = "bar";
95+
const NUMBER = 42;
96+
97+
}

tests/bootstrap.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* This file bootstraps the test environment
4+
*/
5+
6+
error_reporting(E_ALL | E_STRICT);
7+
8+
require_once __DIR__ . '/../vendor/autoload.php';

0 commit comments

Comments
 (0)