PHP Classes

File: Falcraft/tests/Features/Configuration.php

Recommend this page to a friend!
  Classes of Asher Wolfstein   PHP Configuration Trait   Falcraft/tests/Features/Configuration.php   Download  
File: Falcraft/tests/Features/Configuration.php
Role: Example script
Content type: text/plain
Description: Test Script
Class: PHP Configuration Trait
Load and save class configuration using a trait
Author: By
Last change:
Date: 8 years ago
Size: 4,889 bytes
 

Contents

Class file image Download
<?php

require_once('../../Features/Configuration.php');

use
Falcraft\Features;

class
ConfiguredClass {
    use
Features\Configuration;
   
    public function
__construct($configure)
    {
       
$this->configure($configure);
    }
   
    public function
getChildNode1()
    {
        return
$this->conf->parentNode->childNode1;
    }
   
    public function
getChildNode2()
    {
        return
$this->conf->parentNode->childNode2;
    }
   
    public function
getSingleNode()
    {
        return
$this->conf->singleNode;
    }
   
    public function
getTestHost($branch)
    {
        return
$this->conf->$branch->database->params->host;
    }
   
    public function
getTestDBName($branch)
    {
        return
$this->conf->$branch->database->params->dbname;
    }
}

echo
"Falcraft\\Features\\Configuration.php Test\n";
echo
"----------------------------------------\n\n";

echo
"Instantiating Configuration Class With No Args -> ";

$success = true;

try {
   
$configuredObject = new ConfiguredClass(array());
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Instantiating Configuration Class with Array -> ";

$success = true;

try {
   
$configuredObject = new ConfiguredClass(array (
       
'parentNode' => array(
           
'childNode1' => 'data1',
           
'childNode2' => 'data2',),
       
'singleNode' => 'data3'));
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Checking Configuration Variables -> \n";

$success = true;

$childNode1 = $childNode2 = $singleNode = null;

try {
   
$childNode1 = $configuredObject->getChildNode1();
   
$childNode2 = $configuredObject->getChildNode2();
   
$singleNode = $configuredObject->getSingleNode();
    if (!
$childNode1 || !$childNode2 || !$singleNode) {
       
$success = false;
    }
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
" childNode1: $childNode1\n";
    echo
" childNode2: $childNode2\n";
    echo
" singleNode: $singleNode -- Success!\n";
} else {
    echo
" -- Failure!\n";
}

echo
"Instantiating Configuration Class with Ini File -> \n";

$success = true;

$childNode1 = $childNode2 = $singleNode = null;

try {
   
$configuredObject = new ConfiguredClass(array());
   
$configuredObject->configure(__DIR__ . '/test.ini');
   
$childNode1 = $configuredObject->getChildNode1();
   
$childNode2 = $configuredObject->getChildNode2();
   
$singleNode = $configuredObject->getSingleNode();
    if (!
$childNode1 || !$childNode2 || !$singleNode) {
       
$success = false;
    }
} catch (\
Exception $e) {
   
$success = false;
}

echo
" Checking Configuration Variables -> \n";

if (
$success) {
    echo
" childNode1: $childNode1\n";
    echo
" childNode2: $childNode2\n";
    echo
" singleNode: $singleNode -- Success!\n";
} else {
    echo
" -- Failure!\n";
}

echo
"Instantiating Configuration Class with XML File -> \n";

$success = true;

$testHostStaging = $testDBNameStaging = null;
$testHostProduction = null;

try {
   
$configuredObject = new ConfiguredClass(array());
   
$configuredObject->configure(__DIR__ . '/test.xml');
   
$testHostStaging = $configuredObject->getTestHost('staging');
   
$testDBNameStaging = $configuredObject->getTestDBName('staging');
   
$testHostProduction = $configuredObject->getTestHost('production');
} catch (\
Exception $e) {
   
$success = false;
}

echo
" Checking Configuration Variables -> \n";

if (
$success) {
    echo
" testHostStaging: $testHostStaging\n";
    echo
" testDBNameStaging: $testDBNameStaging\n";
    echo
" testHostProduction: $testHostProduction -- Success!\n\n";
} else {
    echo
" -- Failure!\n\n";
}

echo
"-- NOTE! testDBNameStaging may be empty, Zend may not be 'extending' properly\n\n";

echo
"Instantiating Configuration Class with JSON File -> \n";

$success = true;

$testHostStaging = $testDBNameStaging = null;
$testHostProduction = null;

//try {
   
$configuredObject = new ConfiguredClass(array());
   
$configuredObject->configure(__DIR__ . '/test.json');
   
   
$testHostStaging = $configuredObject->getTestHost('staging');
   
$testDBNameStaging = $configuredObject->getTestDBName('staging');
   
$testHostProduction = $configuredObject->getTestHost('production');
//} catch (\Exception $e) {
// $success = false;
//}

echo " Checking Configuration Variables -> \n";

if (
$success) {
    echo
" testHostStaging: $testHostStaging\n";
    echo
" testDBNameStaging: $testDBNameStaging\n";
    echo
" testHostProduction: $testHostProduction -- Success!\n\n";
} else {
    echo
" -- Failure!\n\n";
}

echo
"-- NOTE! testDBNameStaging may be empty, Zend may not be 'extending' properly\n\n";