PHP Classes
elePHPant
Icontem

File: tests/XmlHelperTest.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Richard Williams  >  XML Helper  >  tests/XmlHelperTest.php  >  Download  
File: tests/XmlHelperTest.php
Role: Unit test script
Content type: text/plain
Description: Unit Tests
Class: XML Helper
Extract XML data into an array and vice-versa
Author: By
Last change:
Date: 4 years ago
Size: 10,076 bytes
 

Contents

Class file image Download
<?php

require_once 'PHPUnit/Framework.php';

require_once
dirname(__FILE__) . '/../XmlHelper.class.php';

/**
 * Test class for XmlHelper.
 * Generated by PHPUnit on 2010-12-16 at 10:45:10.
 */
class XmlHelperTest extends PHPUnit_Framework_TestCase {

   
/**
     * @var XmlHelper
     */
   
protected $object;

   
/**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
   
protected function setUp() {
       
$this->object = new XmlHelper;
       
$this->testData = dirname(__FILE__) . '/testXmlData/';
    }

   
/**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
   
protected function tearDown() {

    }

   
/*
     * @covers XmlHelper
     */
   
public function testGetParserVals() {
       
$xmlString = file_get_contents("$this->testData/xmlbase.xml");
       
$helper = new XmlHelper();
       
$helper->setCaseFolding(false);
       
$data = $helper->xmlToArray ($xmlString);
       
$vals = $helper->getParserVals();
// print_r($vals);
       
$this->assertEquals('root', $vals[0][tag]);
       
$this->assertEquals('open', $vals[0][type]);
    }

   
/*
     * @covers XmlHelper
     */
   
public function testSetCaseFolding() {
       
$xmlString = file_get_contents("$this->testData/ibm.xml");
       
$helper = new XmlHelper();
       
$helper->setCaseFolding(true);
       
$data = $helper->xmlToArray ($xmlString);
// print_r($data);
       
$this->assertEquals(0, count($data[BOOK]));
    }

   
/*
     * @covers XmlHelper
     */
   
public function testSetTrimText() {
       
$xmlString = file_get_contents("$this->testData/55521_total_only.xml");
       
$helper = new XmlHelper();
       
$helper->setCaseFolding(false);
       
$helper->setTrimText(true);
       
$data = $helper->xmlToArray ($xmlString);
// print_r($data);
       
$this->assertType('array', $data);
       
$this->assertEquals(175.27, $data['Order']['Total']['Line'][3]['value']);

       
$xml = $helper->arrayToXml($data);
       
$save = $helper->xmlToArray($xml);
       
$this->assertEquals($data, $save);
    }

   
/*
     * @covers XmlHelper
     */
   
function testNoAttributesFlag() {
       
$xmlString = file_get_contents("$this->testData/endicia-status.xml");
       
$helper = new XmlHelper();
       
$helper->setNoAttributes(true);
       
$helper->setCaseFolding(false);
       
$data = $helper->xmlToArray ($xmlString);
// print_r($data);
       
$this->assertTrue(! isset($data['DAZzle']['Package'][0]['attributes']['ID']));
       
$this->assertEquals(20, count($data[DAZzle]['Package']));
       
$this->assertEquals('9101148008600102977907', $data[DAZzle][Package][19][PIC]);

       
$xml = $helper->arrayToXml($data);
       
$data = $helper->xmlToArray($xml);
       
$this->assertEquals('9101148008600102977907', $data[DAZzle][Package][19][PIC]);
    }

   
/*
     * @covers XmlHelper
     */
   
function test_8() {
        require
"$this->testData/large_array.php";
       
$helper = new XmlHelper();
       
$xml = $helper->arrayToXml($data);
// print $xml;
       
file_put_contents("$this->testData/large_array.xml", $xml);
       
$helper->setCaseFolding(false);
       
$save = $helper->xmlToArray($xml);
       
$this->assertEquals(count($save), count($data));
       
$this->assertEquals(count($save['data']['order']), count($data['data']['order']));
       
$this->assertEquals('167.188.175.7', $save['data']['order']['extra']['ip']['value']);
       
$this->assertEquals(count($save['data']['products']), count($data['data']['products'][0]));
       
$this->assertEquals(count($save['data']['userinfo']), count($data['data']['userinfo']));
       
$this->assertEquals(4, count($save['data']['userinfo']['field_sections']));
    }

   
/*
     * @covers XmlHelper
     */
   
function test_7() {
       
$xmlString = file_get_contents("$this->testData/endicia-status.xml");
       
$helper = new XmlHelper();
       
$helper->setCaseFolding(false);
       
$data = $helper->xmlToArray ($xmlString);
// print_r($data);
       
$this->assertEquals(20, count($data[DAZzle]['Package']));
       
$this->assertEquals('9101148008600102977907', $data[DAZzle][Package][19][PIC][value]);

       
$xml = $helper->arrayToXml($data);
       
$data = $helper->xmlToArray($xml);
       
$this->assertEquals('9101148008600102977907', $data[DAZzle][Package][19][PIC][value]);
    }

   
/*
     * @covers XmlHelper
     */
   
function test_6() {
       
$xmlString = file_get_contents("$this->testData/55521_cwr.xml");
       
$helper = new XmlHelper();
       
$helper->setCaseFolding(true);
       
$data = $helper->xmlToArray ($xmlString);
// print_r($data);
       
$this->assertEquals('rogue-55521', $data['ORDER']['ATTRIBUTES']['ID']);
       
$this->assertEquals('ship', $data['ORDER']['ADDRESSINFO'][0]['ATTRIBUTES']['TYPE']);

       
$xml = $helper->arrayToXml($data);
       
$save = $helper->xmlToArray($xml);
       
$this->assertEquals($data, $save);
    }

   
/*
     * @covers XmlHelper
     */
   
function test_5() {
       
$xmlString = file_get_contents("$this->testData/purchase-order-sample.xml");
       
$helper = new XmlHelper();
       
$helper->setCaseFolding(true);
       
$data = $helper->xmlToArray ($xmlString);
// print_r($data);
       
$this->assertEquals(1001, $data['PURCHASEORDER']['PURCHASEORDERNUMBER']['VALUE']);
       
$this->assertEquals('P3456SL', $data['PURCHASEORDER']
                [
'PURCHASEORDERITEMDETAIL'][0]['VENDORPRODUCTID']['VALUE']);

       
$xml = $helper->arrayToXml($data);
       
$save = $helper->xmlToArray($xml);
       
$this->assertEquals($data, $save);
    }

   
/*
     * @covers XmlHelper
     */
   
function test_4() {
       
$xmlString = file_get_contents("$this->testData/base_test.xml");
       
$helper = new XmlHelper();
       
$helper->setCaseFolding(false);
       
$data = $helper->xmlToArray ($xmlString);

// print_r($data);
       
$this->assertEquals(4, $data['LEVEL-0']['attributes']['yy']);
       
$this->assertTrue(strpos($data['LEVEL-0']['value'], 'somedata') > 0);
       
$this->assertEquals('abc', $data['LEVEL-0']['LEVEL-1']['LEVEL-2a']['value']);
       
$this->assertEquals(1, $data['LEVEL-0']['LEVEL-1']['LEVEL-2a']['attributes']['xx']);
       
$this->assertEquals('def', $data['LEVEL-0']['LEVEL-1']['LEVEL-2b'][0]['value']);
       
$this->assertEquals('dd', $data['LEVEL-0']['LEVEL-1']['LEVEL-2b'][0]['attributes']['bb']);
       
$this->assertEquals('ghi', $data['LEVEL-0']['LEVEL-1']['LEVEL-2b'][1]['value']);
       
$this->assertEquals('35', $data['LEVEL-0']['LEVEL-1']['LEVEL-2b'][1]['attributes']['aa']);

       
$xml = $helper->arrayToXml($data);
       
$save = $helper->xmlToArray($xml);
       
$this->assertEquals($data, $save);
    }

   
/*
     * @covers XmlHelper
     */
   
function test_3() {
       
$xmlString = file_get_contents("$this->testData/55521_total_only.xml");
       
$helper = new XmlHelper();
       
$helper->setCaseFolding(false);
       
$helper->setTrimText(true);
       
$data = $helper->xmlToArray ($xmlString);
// print_r($data);
       
$this->assertType('array', $data);
       
$this->assertEquals('USD', $data['Order']['attributes']['currency']);
       
$this->assertEquals('rogue-55521', $data['Order']['attributes']['id']);
       
$this->assertEquals('166.95', $data['Order']['Total']['Line'][0]['value']);
       
$this->assertEquals(8.32, $data['Order']['Total']['Line'][1]['value']);
       
$this->assertEquals(24, $data['Order']['Total']['Line'][2]['InnerLine']['value']);
       
$this->assertEquals(175.27, $data['Order']['Total']['Line'][3]['value']);
       
$this->assertEquals('Subtotal', $data['Order']['Total']['Line'][0]['attributes']['type']);
       
$this->assertEquals('Shipping', $data['Order']['Total']['Line'][1]['attributes']['type']);
       
$this->assertEquals('Tax', $data['Order']['Total']['Line'][2]['attributes']['type']);
       
$this->assertEquals('Total', $data['Order']['Total']['Line'][3]['attributes']['type']);
       
       
$xml = $helper->arrayToXml($data);
       
$save = $helper->xmlToArray($xml);
       
$this->assertEquals($data, $save);
    }

   
/*
     * @covers XmlHelper
     */
   
function test_2() {
       
$xmlString = file_get_contents("$this->testData/one_element.xml");
       
$helper = new XmlHelper();
       
$helper->setCaseFolding(false);
       
$helper->setTrimText(true);
       
$data = $helper->xmlToArray ($xmlString);
// print_r($data);
       
$this->assertEquals(123, $data['test']['value']);

       
$xml = $helper->arrayToXml($data);
       
$this->assertTrue(strpos($xml, '<test>123</test>') > 0);
       
$save = $helper->xmlToArray($xml);
       
$this->assertEquals($data, $save);
    }
   
   
/*
     * @covers XmlHelper
     */
   
function test_1() {
       
$xmlString = file_get_contents("$this->testData/55521_item_only.xml");
       
$helper = new XmlHelper();
       
$helper->setCaseFolding(false);
       
$helper->setTrimText(true);
       
$data = $helper->xmlToArray ($xmlString);
       
$save = $data;
// print_r($data);
       
$this->assertEquals('nvnhmplat6', $data[Order][Item][0][Id]['value']);
       
$this->assertEquals('NOURL', $data[Order][Item][0][Url]['value']);
       
$this->assertEquals('nvnhat6', $data[Order][Item][1][Id]['value']);
       
$this->assertEquals('Selection', $data[Order][Item][1][Option]['attributes']['name']);

       
$xml = $helper->arrayToXml($data);
       
$data = $helper->xmlToArray($xml);
       
$this->assertEquals($save, $data);
       
$this->assertEquals('nvnhmplat6', $data[Order][Item][0][Id]['value']);
       
$this->assertEquals('NOURL', $data[Order][Item][0][Url]['value']);
       
$this->assertEquals('nvnhat6', $data[Order][Item][1][Id]['value']);
       
$this->assertEquals('Selection', $data[Order][Item][1][Option]['attributes']['name']);
// print_r($data);
   
}
}

?>