PHP Classes

File: Falcraft/examples/Data/Types/Enumeration.php

Recommend this page to a friend!
  Classes of Asher Wolfstein   Abstract Data Types   Falcraft/examples/Data/Types/Enumeration.php   Download  
File: Falcraft/examples/Data/Types/Enumeration.php
Role: Example script
Content type: text/plain
Description: Enum Data Type Example
Class: Abstract Data Types
Set of abstract data types as pure PHP classes
Author: By
Last change:
Date: 9 years ago
Size: 5,031 bytes
 

Contents

Class file image Download
<?php

/* SOME TESTS ARE MEANT TO FAIL!
   If the entire script runs, it works. */

namespace Falcraft\Tests\Data\Types;

// Connects/stores vertices implementing interface
require_once('../../../Data/Types/Enumeration.php');

use
Falcraft\Data\Types;

echo
"Falcraft\\Data\\Types\\Enumeration.php Test\n";
echo
"----------------------------------------\n\n";

echo
"Build Enumerator via Object Methods -> ";

$success = true;

$testEnum = null;

try {
   
$testEnum = new Types\Enumeration('testEnumerationClass', array(
       
'TEST1' => 1,
       
'TEST2' => 2,)
    );
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"\nConstants of Enumerator Instantiated -> \n";
var_dump($testEnum->getConstants());

echo
"\nAdding Additional Constants ('ADD1', 'ADD2', 'REM') -> ";

$additionalConstants = false;

try {
   
$additionalConstants = array('ADD1' => 3, 'ADD2' => 4, 'REM' => 5);
   
$testEnum->addConstants($additionalConstants);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"\nConstants of Enumerator Instantiated -> \n";
var_dump($testEnum->getConstants());

echo
"\nRemove Constant ('REM') -> ";

$success = true;

try {
   
$testEnum->removeConstant('REM');
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"\nConstants of Enumerator Instantiated -> \n";
var_dump($testEnum->getConstants());

echo
"\nSet Erroneous Default ('REM') -> ";

$fail = true;

try {
   
$t = $testEnum->setDefault('REM');
} catch (\
Exception $e) {
   
$fail = false;
}

if (
$fail) {
    echo
"Failure!\n";
} else {
    echo
"Success...\n";
}

echo
"Set Correct Defaul ('ADD2') -> ";

$success = true;

try {
   
$t = $testEnum->setDefault('ADD2');
    if (!
$t) {
       
$success = false;
    }
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"Access Default Value -> ";

$success = true;

$default = $defaultValue = null;

try {
   
$default = $testEnum->getDefault();
   
$defaultValue = $testEnum->getDefaultValue();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success! $default ($defaultValue)\n";
} else {
    echo
"Failure...\n";
}

echo
"Establish Namespace -> ";

$success = true;

$namespace = null;

try {
   
$testEnum->setNamespace('Falcraft\\Tests\\Data\\Types');
   
$namespace = $testEnum->getNamespace();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success! $namespace\n";
} else {
    echo
"Failure...\n";
}

echo
"Bake Enumeration -> ";

$success = true;

try {
   
$testEnum->bake();
} catch (\
Exception $e) {
   
$success - false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"Change Enumeration After Baking -> ";

$fail = true;

try {
   
$testEnum->removeConstant('ADD1');
   
$fail = false;
} catch (\
Exception $e) {
   
}

if (
$fail) {
    echo
"Failure!\n";
} else {
    echo
"Success...\n";
}

echo
"Enumeration Internals -> \n\n";

var_dump($testEnum);

echo
"\nUsing An Enumerator (ADD1) ->";

$success = true;

$test = null;

try {
   
$test = testEnumerationClass::ADD1;
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success! ($test)\n";
} else {
    echo
"Failure...\n";
}

echo
"Independent Enumerator Generation -> ";

$fail = true;

$testRebakeEnum = null;

try {
    echo
" Same Qualifier: ";
   
$testRebakeEnum = new Types\Enumeration('testEnumerationClass', array(
       
'REBAKE1' => 1,
       
'REBAKE2' => 2,)
    );
   
   
$testRebakeEnum->setNamespace('Falcraft\\Tests\\Data\\Types');
   
$fail = false;
} catch (\
Exception $e) {
   
$fail = true;
}

if (
$fail) {
    echo
"Failure!\n";
} else {
    echo
"Success...\n";
}

echo
"Generate Instance From Enumeration Instance -> ";

$success = true;

$enumeratedValue = null;

try {
   
$enumeratedValue = $testEnum->getInstance(testEnumerationClass::ADD2);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
   
var_dump($enumeratedValue);
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
"Generate Instance From Enumeration Class -> ";

$success = true;

$enumeratedValue = null;

try {
   
$enumeratedValue = Types\Enumeration::createEnumeration(
       
'Falcraft\\Tests\\Data\\Types\\testEnumerationClass'
   
);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
   
var_dump($enumeratedValue);
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
"Generate Instance From Generated Class -> ";

$success = true;

$enumeratedValue = null;

try {
   
$enumeratedValue = new testEnumerationClass();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
   
var_dump($enumeratedValue);
    echo
"\n";
} else {
    echo
"Failure...\n\n";
}