PHP Classes

File: wrapping_test.php

Recommend this page to a friend!
  Classes of Adrian Miu   Wrapping Factory   wrapping_test.php   Download  
File: wrapping_test.php
Role: Unit test script
Content type: text/plain
Description: Testing file
Class: Wrapping Factory
Wrap objects to add behavior of other classes
Author: By
Last change:
Date: 12 years ago
Size: 4,101 bytes
 

Contents

Class file image Download
<?php

include ('wrapping.php');


class
Product {
    public
$title;
    public
$description;
    public
$price;
   
    function
getTax() {
        return
$this->price * $this->taxRate;
    }
}

// this wrapper changes how the title is displayed
class ProductTitleWrapping extends WrappingAbstract {
    function
__get($attr) {
        if (
$attr == 'title') {
            return
'!!' . $this->_wrappedObject->$attr . '!!';
        }
        return
parent::__get($attr);
    }
}

// this wrapper creates 2 new read only attributes: 'price_with_tax', 'price_without_tax'
class ProductTaxWrapping extends WrappingAbstract {
    public
$priceIncludesTax = true;
    public
$taxRate = 0.1;
   
    function
__get($attr) {
        if (
$attr == 'price_with_tax') {
            if (
$this->priceIncludesTax) {
                return
$this->_wrappedObject->price;
            } else {
                return
$this->_wrappedObject->price * (1 + $this->taxRate);
            }
        }
        if (
$attr == 'price_without_tax') {
            if (!
$this->priceIncludesTax) {
                return
$this->_wrappedObject->price;
            } else {
                return
$this->_wrappedObject->price / (1 + $this->taxRate);
            }
        }
        return
parent::__get($attr);
    }
   
}

// this wrapper simulates what would happen if you were to sell the product to a different location
// where the tax rate is different
class ProductTaxZoneWrapping extends WrappingAbstract {
    function
__construct($object, $params = null) {
       
$object->taxRate = 0.2;
       
parent::__construct($object, $params);
    }
}

function
printTestResult($message, $correctValue, $outputValue) {
    if (
$correctValue == $outputValue) {
        echo
'<strong style="color: #090">TRUE</strong>: ';
    } else {
        echo
'<strong style="color: #c00">FALSE</strong>: ';
    }
    if (
$correctValue === null) {
       
$correctValue = 'null';
    }
    if (
$correctValue === true) {
       
$correctValue = 'true';
    }
    if (
$correctValue === false) {
       
$correctValue = 'false';
    }
    if (
$outputValue === null) {
       
$outputValue = 'null';
    }
    if (
$outputValue === true) {
       
$outputValue = 'true';
    }
    if (
$outputValue === false) {
       
$outputValue = 'false';
    }
   
printf($message, $correctValue, $outputValue);
    echo
'<br>';
}

$wf = new WrappingFactory;
$wf->addWrapping('Product', 'ProductTitleWrapping');
$wf->addWrapping('Product', 'ProductTaxWrapping');
$wf->addWrapping('Product', 'ProductTaxZoneWrapping');

echo
'<h1>Wrap an existing object</h1>';
// create the product first and then wrap it
$product = new Product;
$product->price = 50;
$product->title = 'iPhone 5';
$product = $wf->wrap($product);

echo
'<h2>Test product title</h2>';
printTestResult('Title should be <strong>%s</strong> and is <strong>%s</strong>.', '!!iPhone 5!!', $product->title);
$product->title = 'The new iPhone 5';
printTestResult('After the title was changed it should be <strong>%s</strong> and is <strong>%s</strong>.', '!!The new iPhone 5!!', $product->title);
printTestResult('The title of the BASE object should be <strong>%s</strong> and is <strong>%s</strong>.', 'The new iPhone 5', $product->_getBaseAttribute('title'));

echo
'<h2>Test product price</h2>';
printTestResult('Price should be <strong>%s</strong> and is <strong>%s</strong>.', 50, $product->price);
$product->price = 40;
printTestResult('After the price was changed the price should be <strong>%s</strong> and is <strong>%s</strong>.', 40, $product->price);
printTestResult('The price of the BASE object should be <strong>%s</strong> and is <strong>%s</strong>.', 40, $product->_getBaseAttribute('price'));

echo
'<h2>Test new attributes (price_with_tax and price_without_tax)</h2>';
printTestResult('Price WITH tax should be <strong>%s</strong> and is <strong>%s</strong>.', 40, $product->price_with_tax);
printTestResult('Price WITHOUT tax should be <strong>%s</strong> and is <strong>%s</strong>.', 40/1.2, $product->price_without_tax);
printTestResult('Tax rate should be <strong>%s</strong> and is <strong>%s</strong>.', 0.2, $product->taxRate);
printTestResult('Tax rate of the BASE object should be <strong>%s</strong> and is <strong>%s</strong>.', null, $product->_getBaseAttribute('taxRate'));