[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/tests/ezxml/ -> eztestxmlarrayserialization.php (source)

   1  <?php
   2  //
   3  // Definition of eZTestXMLArraySerialization class
   4  //
   5  // Created on: <22-Nov-2004 13:48:38 jb>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \file eztestxmlarrayserialization.php
  30  */
  31  
  32  /*!
  33    \class eZTestXMLArraySerialization eztestxmlarrayserialization.php
  34    \brief The class eZTestXMLArraySerialization does
  35  
  36  */
  37  
  38  class eZTestXMLArraySerialization extends eZTestCase
  39  {
  40      /*!
  41       Constructor
  42      */
  43      function eZTestXMLArraySerialization( $name = false )
  44      {
  45          $this->eZTestCase( $name );
  46  
  47          $this->addTest( 'testSerialize', 'Serialization of array to XML' );
  48  //         $this->addTest( 'testUnserialize', 'Unserialization of XML to array' );
  49          $this->addTest( 'testTwoWaySerialize', 'Serialize array to XML and then back again' );
  50      }
  51  
  52      /*!
  53       \return The test array.
  54      */
  55      function generateArray()
  56      {
  57          $array = array( 'key1' => array( 'file' => 'file1.txt',
  58                                           'path' => 'a/small/path' ),
  59                          'key2' => array( 'file' => 'file2.txt',
  60                                           'path' => 'another/path' ) );
  61          return $array;
  62      }
  63  
  64      /*!
  65       \return The test node structure as array.
  66      */
  67      function generateNodeMatchArray()
  68      {
  69          $key1Attributes = array( 0 => array( 'name' => 'file',
  70                                               'type' => 2,
  71                                               'children' => array(),
  72                                               'attributes' => array(),
  73                                               'content' => 'file1.txt' ),
  74                                   1 => array( 'name' => 'path',
  75                                               'type' => 2,
  76                                               'children' => array(),
  77                                               'attributes' => array(),
  78                                               'content' => 'a/small/path' ) );
  79          $key1 = array( 'name' => 'key1',
  80                         'type' => 1,
  81                         'children' => array(),
  82                         'attributes' => $key1Attributes,
  83                         'content' => '' );
  84          $key2Attributes = array( 0 => array( 'name' => 'file',
  85                                               'type' => 2,
  86                                               'children' => array(),
  87                                               'attributes' => array(),
  88                                               'content' => 'file2.txt' ),
  89                                   1 => array( 'name' => 'path',
  90                                               'type' => 2,
  91                                               'children' => array(),
  92                                               'attributes' => array(),
  93                                               'content' => 'another/path' ) );
  94          $key2 = array( 'name' => 'key2',
  95                         'type' => 1,
  96                         'children' => array(),
  97                         'attributes' => $key2Attributes,
  98                         'content' => '' );
  99          return array( 'name' => 'test-array',
 100                        'type' => 1,
 101                        'children' => array( 0 => $key1,
 102                                             1 => $key2 ),
 103                        'attributes' => array(),
 104                        'content' => '' );
 105      }
 106  
 107      function nodeToArray( $node )
 108      {
 109          $array = array( 'name' => $node->name(),
 110                          'type' => $node->type(),
 111                          'children' => array(),
 112                          'attributes' => array(),
 113                          'content' => $node->content() );
 114          $children =& $node->children();
 115          foreach ( $children as $child )
 116          {
 117              $array['children'][] = $this->nodeToArray( $child );
 118          }
 119          $attributes =& $node->attributes();
 120          foreach ( $attributes as $attribute )
 121          {
 122              $array['attributes'][] = $this->nodeToArray( $attribute );
 123          }
 124          return $array;
 125      }
 126  
 127      function matchNodeArray( $node, $nodeMatchArray )
 128      {
 129          $nodeArray = $this->nodeToArray( $node );
 130          return $this->arrayCompare( $nodeArray, $nodeMatchArray );
 131      }
 132  
 133      /*!
 134       \return \c true if \a $array1 is similar to \a $array2.
 135  
 136       It is considered similar if they have the same keys and with
 137       the same values (using == for compare).
 138      */
 139      function arrayCompare( $array1, $array2 )
 140      {
 141          foreach ( $array1 as $key => $value )
 142          {
 143              if ( is_array( $value ) )
 144              {
 145                  if ( !isset( $array2[$key] ) )
 146                      return false;
 147                  if ( !$this->arrayCompare( $value, $array2[$key] ) )
 148                      return false;
 149              }
 150              else
 151              {
 152                  if ( !isset( $array2[$key] ) )
 153                      return false;
 154                  if ( $value != $array2[$key] )
 155                      return false;
 156              }
 157          }
 158  
 159          foreach ( $array2 as $key => $value )
 160          {
 161              if ( is_array( $value ) )
 162              {
 163                  if ( !isset( $array1[$key] ) )
 164                      return false;
 165                  if ( !$this->arrayCompare( $value, $array1[$key] ) )
 166                      return false;
 167              }
 168              else
 169              {
 170                  if ( !isset( $array1[$key] ) )
 171                      return false;
 172                  if ( $value != $array1[$key] )
 173                      return false;
 174              }
 175          }
 176          return true;
 177      }
 178  
 179      /*!
 180       Serializes an array into a node structure and validates that against a given structure.
 181      */
 182      function testSerialize( &$tr )
 183      {
 184          include_once ( 'lib/ezxml/classes/ezxml.php' );
 185          $doc = new eZDomDocument();
 186          $array = $this->generateArray();
 187          $node = $doc->createElementNodeFromArray( 'test-array', $array );
 188          $nodeMatchArray = $this->generateNodeMatchArray();
 189          $tr->assert( $this->matchNodeArray( $node, $nodeMatchArray ) );
 190      }
 191  
 192      /*!
 193       Serializes an array into a node structure and then back again to an array.
 194       These two arrays are then compared against each other.
 195      */
 196      function testTwoWaySerialize( &$tr )
 197      {
 198          include_once ( 'lib/ezxml/classes/ezxml.php' );
 199          $doc = new eZDomDocument();
 200          $array1 = $this->generateArray();
 201          $node = $doc->createElementNodeFromArray( 'test-array', $array1 );
 202          $array2 = $doc->createArrayFromDOMNode( $node );
 203          $tr->assert( $this->arrayCompare( $array1, $array2 ) );
 204      }
 205  
 206  }
 207  
 208  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7