Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Jan 23, 2013
1 parent d0e275d commit 618ef64
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
nbproject/*
.idea/*
vendor/*
composer.phar
composer.lock
12 changes: 12 additions & 0 deletions .travis.yml
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
1 change: 0 additions & 1 deletion README

This file was deleted.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PHP Enum implementation inspired from SplEnum
========
12 changes: 12 additions & 0 deletions composer.json
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": {
}
}
22 changes: 22 additions & 0 deletions phpunit.xml
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>
62 changes: 62 additions & 0 deletions src/Mycsense/Enum/Enum.php
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();
}

}
97 changes: 97 additions & 0 deletions tests/UnitTest/Mycsense/Enum/EnumTest.php
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;

}
8 changes: 8 additions & 0 deletions tests/bootstrap.php
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';

0 comments on commit 618ef64

Please sign in to comment.