Author: dr
Date: Tue Aug 28 10:06:57 2007
New Revision: 5955

Log:
- Added an XHTML visitor for tree representations.

Added:
    trunk/Tree/src/options/
    trunk/Tree/src/options/visitor_xhtml.php   (with props)
    trunk/Tree/src/visitors/xhtml.php   (with props)
    trunk/Tree/tests/visitor_options.php   (with props)
Modified:
    trunk/Tree/design/class_diagram.png
    trunk/Tree/src/tree_autoload.php
    trunk/Tree/src/tree_node.php
    trunk/Tree/tests/suite.php
    trunk/Tree/tests/visitor.php

Modified: trunk/Tree/design/class_diagram.png
==============================================================================
Binary files - no diff available.

Added: trunk/Tree/src/options/visitor_xhtml.php
==============================================================================
--- trunk/Tree/src/options/visitor_xhtml.php (added)
+++ trunk/Tree/src/options/visitor_xhtml.php [iso-8859-1] Tue Aug 28 10:06:57 
2007
@@ -1,0 +1,103 @@
+<?php
+/**
+ * File containing the ezcTreeVisitorXHTMLOptions class
+ *
+ * @package Tree
+ * @version //autogen//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * Class containing the options for the ezcTreeVisitorXHTMLOptions class.
+ *
+ * @property string $basePath
+ *           Which string to prefix the href-targets with.
+ * @property bool $addLinks
+ *           Whether links should be generated or not.
+ * @property bool $displayRootNode
+ *           Whether the root node should be displayed.
+ * @property string $xmlId
+ *           The ID that should be set on the top level &lt;ul&gt; tag.
+ * @property array(string) $highlightNodeIds
+ *           Which IDs should have the 'highlight' CSS class added.
+ *
+ * @package Tree
+ * @version //autogen//
+ */
+class ezcTreeVisitorXHTMLOptions extends ezcBaseOptions
+{
+    /**
+     * Constructs an object with the specified values.
+     *
+     * @throws ezcBasePropertyNotFoundException
+     *         if $options contains a property not defined
+     * @throws ezcBaseValueException
+     *         if $options contains a property with a value not allowed
+     * @param array(string=>mixed) $options
+     */
+    public function __construct( array $options = array() )
+    {
+        $this->basePath = '';
+        $this->addLinks = true;
+        $this->displayRootNode = false;
+        $this->xmlId = null;
+        $this->highlightNodeIds = array();
+
+        parent::__construct( $options );
+    }
+
+    /**
+     * Sets the option $name to $value.
+     *
+     * @throws ezcBasePropertyNotFoundException
+     *         if the property $name is not defined
+     * @throws ezcBaseValueException
+     *         if $value is not correct for the property $name
+     * @param string $name
+     * @param mixed $value
+     * @ignore
+     */
+    public function __set( $name, $value )
+    {
+        switch ( $name )
+        {
+            case 'basePath':
+                if ( !is_string( $value ) )
+                {
+                    throw new ezcBaseValueException( $name, $value, 'string' );
+                }
+                $this->properties[$name] = $value;
+                break;
+
+            case 'addLinks':
+            case 'displayRootNode':
+                if ( !is_bool( $value ) )
+                {
+                    throw new ezcBaseValueException( $name, $value, 'bool' );
+                }
+                $this->properties[$name] = $value;
+                break;
+
+            case 'highlightNodeIds':
+                if ( !is_array( $value ) )
+                {
+                    throw new ezcBaseValueException( $name, $value, 
'array(string)' );
+                }
+                $this->properties[$name] = $value;
+                break;
+
+            case 'xmlId':
+                if ( !is_null( $value ) && !is_string( $value ) )
+                {
+                    throw new ezcBaseValueException( $name, $value, 'null or 
string' );
+                }
+                $this->properties[$name] = $value;
+                break;
+
+            default:
+                throw new ezcBasePropertyNotFoundException( $name );
+        }
+    }
+}
+?>

Propchange: trunk/Tree/src/options/visitor_xhtml.php
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: trunk/Tree/src/tree_autoload.php
==============================================================================
--- trunk/Tree/src/tree_autoload.php [iso-8859-1] (original)
+++ trunk/Tree/src/tree_autoload.php [iso-8859-1] Tue Aug 28 10:06:57 2007
@@ -33,6 +33,8 @@
     'ezcTreeTransactionItem'                    => 
'Tree/structs/transaction_item.php',
     'ezcTreeVisitorGraphViz'                    => 
'Tree/visitors/graphviz.php',
     'ezcTreeVisitorPlainText'                   => 
'Tree/visitors/plain_text.php',
+    'ezcTreeVisitorXHTML'                       => 'Tree/visitors/xhtml.php',
+    'ezcTreeVisitorXHTMLOptions'                => 
'Tree/options/visitor_xhtml.php',
     'ezcTreeXml'                                => 'Tree/backends/xml.php',
     'ezcTreeXmlInternalDataStore'               => 
'Tree/stores/xml_internal.php',
 );

Modified: trunk/Tree/src/tree_node.php
==============================================================================
--- trunk/Tree/src/tree_node.php [iso-8859-1] (original)
+++ trunk/Tree/src/tree_node.php [iso-8859-1] Tue Aug 28 10:06:57 2007
@@ -310,5 +310,10 @@
     {
         return $this->tree->isSiblingOf( $this->id, $child2Node->id );
     }
+
+    public function __toString()
+    {
+        return $this->id;
+    }
 }
 ?>

Added: trunk/Tree/src/visitors/xhtml.php
==============================================================================
--- trunk/Tree/src/visitors/xhtml.php (added)
+++ trunk/Tree/src/visitors/xhtml.php [iso-8859-1] Tue Aug 28 10:06:57 2007
@@ -1,0 +1,233 @@
+<?php
+/**
+ * File containing the ezcTreeVisitorXHTML class.
+ *
+ * @package Tree
+ * @version //autogen//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * An implementation of the ezcTreeVisitor interface that generates
+ * an XHTML representatation of a tree structure.
+ *
+ * <code>
+ * <?php
+ *     $visitor = new ezcTreeVisitorXHTML( 'menu_tree', 'menu' );
+ *     $tree->accept( $visitor );
+ *     echo (string) $visitor; // print the plot
+ * ?>
+ * </code>
+ *
+ * Shows (something like):
+ * <code>
+ * </code>
+ *
+ * @package Tree
+ * @version //autogen//
+ */
+class ezcTreeVisitorXHTML implements ezcTreeVisitor
+{
+    /**
+     * Holds all the edges of the graph.
+     *
+     * @var array(string=>array(string))
+     */
+    protected $edges = array();
+
+    /**
+     * Holds the root ID
+     *
+     * @var string
+     */
+    protected $root = null;
+
+    /**
+     * Holds the XML ID
+     *
+     * @var string
+     */
+    protected $id;
+
+    /**
+     * Holds the XHTML class
+     *
+     * @var string
+     */
+    protected $class;
+
+    /**
+     * Whether the XML ID has been set
+     *
+     * @var bool
+     */
+    private $treeIdSet;
+
+    /**
+     * Constructs a new ezcTreeVisitorXHTML visualizer using $symbolCharset as 
character set
+     *
+     * This class only supports 'ascii' and 'utf-8' as character sets.
+     * @see SYMBOL_UTF8
+     * @see SYMBOL_ASCII
+     *
+     * @param int $symbolCharset
+     */
+    public function __construct( ezcTreeVisitorXHTMLOptions $options = null )
+    {
+        if ( $options === null )
+        {
+            $this->options = new ezcTreeVisitorXHTMLOptions;
+        }
+        else
+        {
+            $this->options = $options;
+        }
+    }
+
+    /**
+     * This method formats a node's data.
+     *
+     * It is just a simple method, that provide an easy way to change the way
+     * on how data is formatted when this class is extended.
+     *
+     * @param mixed $data
+     * @return string
+     */
+    protected function formatData( $data )
+    {
+        return $data;
+    }
+
+    /**
+     * Visits the node and sets the the member variables according to the node
+     * type and contents.
+     *
+     * @param ezcTreeVisitable $visitable
+     * @return boolean
+     */
+    public function visit( ezcTreeVisitable $visitable )
+    {
+        if ( $visitable instanceof ezcTree )
+        {
+        }
+
+        if ( $visitable instanceof ezcTreeNode )
+        {
+            if ( $this->root === null )
+            {
+                $this->root = $visitable->id;
+            }
+
+            $parent = $visitable->fetchParent();
+            if ( $parent )
+            {
+                $this->edges[$parent->id][] = array( $visitable->id, 
$visitable->data, $visitable->fetchPath() );
+            }
+        }
+
+        return true;
+    }
+
+    /**
+     * Loops over the children of the node with ID $id.
+     *
+     * This methods loops over all the node's children and adds the correct
+     * layout for each node depending on the state that is collected in the
+     * $level and $levelLast variables.
+     *
+     * @param string $id
+     * @param int    $level
+     * @param array(int=>bool) $levelLast
+     *
+     * @return string
+     */
+    protected function doChildren( $id, $level = 0, $levelLast = array() )
+    {
+        $text = '';
+
+        $children = $this->edges[$id];
+        $numChildren = count( $children );
+
+        if ( $numChildren > 0 )
+        {
+            $text .= str_repeat( '  ', $level + 1 );
+
+            $idPart = '';
+            if ( !$this->treeIdSet )
+            {
+                $idPart = $this->options->xmlId ? " 
id=\"{$this->options->xmlId}\"" : '';
+                $this->treeIdSet = true;
+            }
+            $text .= "<ul{$idPart}>\n";
+            foreach ( $children as $child )
+            {
+                $path = $child[2]->nodes;
+                if ( !$this->options->displayRootNode )
+                {
+                    array_shift( $path );
+                }
+                $path = htmlspecialchars( $this->options->basePath . '/' . 
join( '/', $path ) );
+                $text .= str_repeat( '  ', $level + 2 );
+
+                $data = htmlspecialchars( $this->formatData( $child[1] ) );
+
+                $linkStart = $linkEnd = '';
+                if ( $this->options->addLinks )
+                {
+                    $linkStart = "<a href=\"{$path}\">";
+                    $linkEnd   = "</a>";
+                }
+
+                $highlightPart = '';
+                if ( in_array( $child[0], $this->options->highlightNodeIds ) )
+                {
+                    $highlightPart = ' class="highlight"';
+                }
+
+                if ( isset( $this->edges[$child[0]] ) )
+                {
+                    $text .= 
"<li{$highlightPart}>{$linkStart}{$data}{$linkEnd}\n";
+                    $text .= $this->doChildren( $child[0], $level + 2, 
$levelLast );
+                    $text .= str_repeat( '  ', $level + 2 );
+                    $text .= "</li>\n";
+                }
+                else
+                {
+                    $text .= 
"<li{$highlightPart}>{$linkStart}{$data}{$linkEnd}</li>\n";
+                }
+            }
+            $text .= str_repeat( '  ', $level + 1);
+            $text .= "</ul>\n";
+        }
+
+        return $text;
+    }
+
+    /**
+     * Returns a text representatation of a tree.
+     *
+     * @return string
+     * @ignore
+     */
+    public function __toString()
+    {
+        $tree = '';
+        $this->treeIdSet = false;
+
+        if ( $this->options->displayRootNode )
+        {
+            $idPart = $this->options->xmlId ? " 
id=\"{$this->options->xmlId}\"" : '';
+            $tree .= "<ul{$idPart}>\n";
+            $tree .= "<li>{$this->root}</li>\n";
+            $this->treeIdSet = true;
+        }
+        $tree .= $this->doChildren( $this->root );
+        if ( $this->options->displayRootNode )
+        {
+            $tree .= "</ul>\n";
+        }
+        return $tree;
+    }
+}
+?>

Propchange: trunk/Tree/src/visitors/xhtml.php
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: trunk/Tree/tests/suite.php
==============================================================================
--- trunk/Tree/tests/suite.php [iso-8859-1] (original)
+++ trunk/Tree/tests/suite.php [iso-8859-1] Tue Aug 28 10:06:57 2007
@@ -17,6 +17,7 @@
 require_once 'tree_node_list_iterator.php';
 require_once 'memory_store.php';
 require_once 'visitor.php';
+require_once 'visitor_options.php';
 require_once 'memory_tree.php';
 require_once 'xml_tree.php';
 require_once 'copy_tree.php';
@@ -37,6 +38,7 @@
         $this->addTest( ezcTreeNodeListIteratorTest::suite() );
         $this->addTest( ezcTreeMemoryStoreTest::suite() );
         $this->addTest( ezcTreeVisitorTest::suite() );
+        $this->addTest( ezcTreeVisitorOptionsTest::suite() );
         $this->addTest( ezcTreeMemoryTest::suite() );
         $this->addTest( ezcTreeXmlTest::suite() );
         $this->addTest( ezcTreeCopyTest::suite() );

Modified: trunk/Tree/tests/visitor.php
==============================================================================
--- trunk/Tree/tests/visitor.php [iso-8859-1] (original)
+++ trunk/Tree/tests/visitor.php [iso-8859-1] Tue Aug 28 10:06:57 2007
@@ -212,6 +212,312 @@
         self::assertSame( $expected, (string) $visitor );
     }
 
+    public function testVisitorXHTMLDefault()
+    {
+        $tree = ezcTreeMemory::create( new ezcTreeMemoryDataStore() );
+        $this->addTestData( $tree );
+
+        $visitor = new ezcTreeVisitorXHTML();
+        $tree->accept( $visitor );
+        $expected = <<<END
+  <ul>
+    <li><a href="/Hylobatidae">Hylobatidae</a>
+      <ul>
+        <li><a href="/Hylobatidae/Hylobates">Hylobates</a>
+          <ul>
+            <li><a href="/Hylobatidae/Hylobates/Lar Gibbon">Lar Gibbon</a></li>
+            <li><a href="/Hylobatidae/Hylobates/Agile Gibbon">Agile 
Gibbon</a></li>
+            <li><a href="/Hylobatidae/Hylobates/Müller's Bornean 
Gibbon">Müller's Bornean Gibbon</a></li>
+            <li><a href="/Hylobatidae/Hylobates/Silvery Gibbon">Silvery 
Gibbon</a></li>
+            <li><a href="/Hylobatidae/Hylobates/Pileated Gibbon">Pileated 
Gibbon</a></li>
+            <li><a href="/Hylobatidae/Hylobates/Kloss's Gibbon">Kloss's 
Gibbon</a></li>
+          </ul>
+        </li>
+        <li><a href="/Hylobatidae/Hoolock">Hoolock</a>
+          <ul>
+            <li><a href="/Hylobatidae/Hoolock/Western Hoolock Gibbon">Western 
Hoolock Gibbon</a></li>
+            <li><a href="/Hylobatidae/Hoolock/Eastern Hoolock Gibbon">Eastern 
Hoolock Gibbon</a></li>
+          </ul>
+        </li>
+        <li><a href="/Hylobatidae/Symphalangus">Symphalangus</a></li>
+        <li><a href="/Hylobatidae/Nomascus">Nomascus</a>
+          <ul>
+            <li><a href="/Hylobatidae/Nomascus/Black Crested Gibbon">Black 
Crested Gibbon</a></li>
+            <li><a href="/Hylobatidae/Nomascus/Eastern Black Crested 
Gibbon">Eastern Black Crested Gibbon</a></li>
+            <li><a href="/Hylobatidae/Nomascus/White-cheecked Crested 
Gibbon">White-cheecked Crested Gibbon</a></li>
+            <li><a href="/Hylobatidae/Nomascus/Yellow-cheecked 
Gibbon">Yellow-cheecked Gibbon</a></li>
+          </ul>
+        </li>
+      </ul>
+    </li>
+    <li><a href="/Hominidae">Hominidae</a>
+      <ul>
+        <li><a href="/Hominidae/Pongo">Pongo</a>
+          <ul>
+            <li><a href="/Hominidae/Pongo/Bornean Orangutan">Bornean 
Orangutan</a></li>
+            <li><a href="/Hominidae/Pongo/Sumatran Orangutan">Sumatran 
Orangutan</a></li>
+          </ul>
+        </li>
+        <li><a href="/Hominidae/Gorilla">Gorilla</a>
+          <ul>
+            <li><a href="/Hominidae/Gorilla/Western Gorilla">Western 
Gorilla</a>
+              <ul>
+                <li><a href="/Hominidae/Gorilla/Western Gorilla/Western 
Lowland Gorilla">Western Lowland Gorilla</a></li>
+                <li><a href="/Hominidae/Gorilla/Western Gorilla/Cross River 
Gorilla">Cross River Gorilla</a></li>
+              </ul>
+            </li>
+            <li><a href="/Hominidae/Gorilla/Eastern Gorilla">Eastern 
Gorilla</a>
+              <ul>
+                <li><a href="/Hominidae/Gorilla/Eastern Gorilla/Mountain 
Gorilla">Mountain Gorilla</a></li>
+                <li><a href="/Hominidae/Gorilla/Eastern Gorilla/Eastern 
Lowland Gorilla">Eastern Lowland Gorilla</a></li>
+              </ul>
+            </li>
+          </ul>
+        </li>
+        <li><a href="/Hominidae/Homo">Homo</a>
+          <ul>
+            <li><a href="/Hominidae/Homo/Homo Sapiens">Homo Sapiens</a>
+              <ul>
+                <li><a href="/Hominidae/Homo/Homo Sapiens/Homo Sapiens 
Sapiens">Homo Sapiens Sapiens</a></li>
+                <li><a href="/Hominidae/Homo/Homo Sapiens/Homo Superior">Homo 
Superior</a></li>
+              </ul>
+            </li>
+          </ul>
+        </li>
+        <li><a href="/Hominidae/Pan">Pan</a>
+          <ul>
+            <li><a href="/Hominidae/Pan/Common Chimpanzee">Common 
Chimpanzee</a></li>
+            <li><a href="/Hominidae/Pan/Bonobo">Bonobo</a></li>
+          </ul>
+        </li>
+      </ul>
+    </li>
+  </ul>
+
+END;
+        self::assertSame( $expected, $visitor->__toString() );
+    }
+
+    public function testVisitorXHTMLDisplayRootNode()
+    {
+        $tree = ezcTreeMemory::create( new ezcTreeMemoryDataStore() );
+        $this->addTestData( $tree );
+
+        $visitor = new ezcTreeVisitorXHTML();
+        $visitor->options->displayRootNode = true;
+
+        $tree->accept( $visitor );
+        $expected = <<<END
+<ul>
+<li>Hominoidea</li>
+  <ul>
+    <li><a href="/Hominoidea/Hylobatidae">Hylobatidae</a>
+      <ul>
+        <li><a href="/Hominoidea/Hylobatidae/Hylobates">Hylobates</a>
+          <ul>
+            <li><a href="/Hominoidea/Hylobatidae/Hylobates/Lar Gibbon">Lar 
Gibbon</a></li>
+            <li><a href="/Hominoidea/Hylobatidae/Hylobates/Agile Gibbon">Agile 
Gibbon</a></li>
+            <li><a href="/Hominoidea/Hylobatidae/Hylobates/Müller's Bornean 
Gibbon">Müller's Bornean Gibbon</a></li>
+            <li><a href="/Hominoidea/Hylobatidae/Hylobates/Silvery 
Gibbon">Silvery Gibbon</a></li>
+            <li><a href="/Hominoidea/Hylobatidae/Hylobates/Pileated 
Gibbon">Pileated Gibbon</a></li>
+            <li><a href="/Hominoidea/Hylobatidae/Hylobates/Kloss's 
Gibbon">Kloss's Gibbon</a></li>
+          </ul>
+        </li>
+        <li><a href="/Hominoidea/Hylobatidae/Hoolock">Hoolock</a>
+          <ul>
+            <li><a href="/Hominoidea/Hylobatidae/Hoolock/Western Hoolock 
Gibbon">Western Hoolock Gibbon</a></li>
+            <li><a href="/Hominoidea/Hylobatidae/Hoolock/Eastern Hoolock 
Gibbon">Eastern Hoolock Gibbon</a></li>
+          </ul>
+        </li>
+        <li><a 
href="/Hominoidea/Hylobatidae/Symphalangus">Symphalangus</a></li>
+        <li><a href="/Hominoidea/Hylobatidae/Nomascus">Nomascus</a>
+          <ul>
+            <li><a href="/Hominoidea/Hylobatidae/Nomascus/Black Crested 
Gibbon">Black Crested Gibbon</a></li>
+            <li><a href="/Hominoidea/Hylobatidae/Nomascus/Eastern Black 
Crested Gibbon">Eastern Black Crested Gibbon</a></li>
+            <li><a href="/Hominoidea/Hylobatidae/Nomascus/White-cheecked 
Crested Gibbon">White-cheecked Crested Gibbon</a></li>
+            <li><a href="/Hominoidea/Hylobatidae/Nomascus/Yellow-cheecked 
Gibbon">Yellow-cheecked Gibbon</a></li>
+          </ul>
+        </li>
+      </ul>
+    </li>
+    <li><a href="/Hominoidea/Hominidae">Hominidae</a>
+      <ul>
+        <li><a href="/Hominoidea/Hominidae/Pongo">Pongo</a>
+          <ul>
+            <li><a href="/Hominoidea/Hominidae/Pongo/Bornean 
Orangutan">Bornean Orangutan</a></li>
+            <li><a href="/Hominoidea/Hominidae/Pongo/Sumatran 
Orangutan">Sumatran Orangutan</a></li>
+          </ul>
+        </li>
+        <li><a href="/Hominoidea/Hominidae/Gorilla">Gorilla</a>
+          <ul>
+            <li><a href="/Hominoidea/Hominidae/Gorilla/Western 
Gorilla">Western Gorilla</a>
+              <ul>
+                <li><a href="/Hominoidea/Hominidae/Gorilla/Western 
Gorilla/Western Lowland Gorilla">Western Lowland Gorilla</a></li>
+                <li><a href="/Hominoidea/Hominidae/Gorilla/Western 
Gorilla/Cross River Gorilla">Cross River Gorilla</a></li>
+              </ul>
+            </li>
+            <li><a href="/Hominoidea/Hominidae/Gorilla/Eastern 
Gorilla">Eastern Gorilla</a>
+              <ul>
+                <li><a href="/Hominoidea/Hominidae/Gorilla/Eastern 
Gorilla/Mountain Gorilla">Mountain Gorilla</a></li>
+                <li><a href="/Hominoidea/Hominidae/Gorilla/Eastern 
Gorilla/Eastern Lowland Gorilla">Eastern Lowland Gorilla</a></li>
+              </ul>
+            </li>
+          </ul>
+        </li>
+        <li><a href="/Hominoidea/Hominidae/Homo">Homo</a>
+          <ul>
+            <li><a href="/Hominoidea/Hominidae/Homo/Homo Sapiens">Homo 
Sapiens</a>
+              <ul>
+                <li><a href="/Hominoidea/Hominidae/Homo/Homo Sapiens/Homo 
Sapiens Sapiens">Homo Sapiens Sapiens</a></li>
+                <li><a href="/Hominoidea/Hominidae/Homo/Homo Sapiens/Homo 
Superior">Homo Superior</a></li>
+              </ul>
+            </li>
+          </ul>
+        </li>
+        <li><a href="/Hominoidea/Hominidae/Pan">Pan</a>
+          <ul>
+            <li><a href="/Hominoidea/Hominidae/Pan/Common Chimpanzee">Common 
Chimpanzee</a></li>
+            <li><a href="/Hominoidea/Hominidae/Pan/Bonobo">Bonobo</a></li>
+          </ul>
+        </li>
+      </ul>
+    </li>
+  </ul>
+</ul>
+
+END;
+        self::assertSame( $expected, $visitor->__toString() );
+    }
+
+    public function testVisitorXHTMLXmlId()
+    {
+        $tree = ezcTreeMemory::create( new ezcTreeMemoryDataStore() );
+        $this->addTestData( $tree );
+
+        $visitor = new ezcTreeVisitorXHTML();
+        $visitor->options->xmlId = 'tree_id';
+
+        $tree->fetchNodeById( 'Hylobatidae' )->accept( $visitor );
+        $expected = <<<END
+  <ul id="tree_id">
+    <li><a href="/Hylobatidae/Hylobates">Hylobates</a>
+      <ul>
+        <li><a href="/Hylobatidae/Hylobates/Lar Gibbon">Lar Gibbon</a></li>
+        <li><a href="/Hylobatidae/Hylobates/Agile Gibbon">Agile Gibbon</a></li>
+        <li><a href="/Hylobatidae/Hylobates/Müller's Bornean 
Gibbon">Müller's Bornean Gibbon</a></li>
+        <li><a href="/Hylobatidae/Hylobates/Silvery Gibbon">Silvery 
Gibbon</a></li>
+        <li><a href="/Hylobatidae/Hylobates/Pileated Gibbon">Pileated 
Gibbon</a></li>
+        <li><a href="/Hylobatidae/Hylobates/Kloss's Gibbon">Kloss's 
Gibbon</a></li>
+      </ul>
+    </li>
+    <li><a href="/Hylobatidae/Hoolock">Hoolock</a>
+      <ul>
+        <li><a href="/Hylobatidae/Hoolock/Western Hoolock Gibbon">Western 
Hoolock Gibbon</a></li>
+        <li><a href="/Hylobatidae/Hoolock/Eastern Hoolock Gibbon">Eastern 
Hoolock Gibbon</a></li>
+      </ul>
+    </li>
+    <li><a href="/Hylobatidae/Symphalangus">Symphalangus</a></li>
+    <li><a href="/Hylobatidae/Nomascus">Nomascus</a>
+      <ul>
+        <li><a href="/Hylobatidae/Nomascus/Black Crested Gibbon">Black Crested 
Gibbon</a></li>
+        <li><a href="/Hylobatidae/Nomascus/Eastern Black Crested 
Gibbon">Eastern Black Crested Gibbon</a></li>
+        <li><a href="/Hylobatidae/Nomascus/White-cheecked Crested 
Gibbon">White-cheecked Crested Gibbon</a></li>
+        <li><a href="/Hylobatidae/Nomascus/Yellow-cheecked 
Gibbon">Yellow-cheecked Gibbon</a></li>
+      </ul>
+    </li>
+  </ul>
+
+END;
+        self::assertSame( $expected, $visitor->__toString() );
+    }
+
+    public function testVisitorXHTMLNoLinks()
+    {
+        $tree = ezcTreeMemory::create( new ezcTreeMemoryDataStore() );
+        $this->addTestData( $tree );
+
+        $options = new ezcTreeVisitorXHTMLOptions;
+        $options->addLinks = false;
+        $visitor = new ezcTreeVisitorXHTML( $options );
+
+        $tree->fetchNodeById( 'Hylobatidae' )->accept( $visitor );
+        $expected = <<<END
+  <ul>
+    <li>Hylobates
+      <ul>
+        <li>Lar Gibbon</li>
+        <li>Agile Gibbon</li>
+        <li>Müller's Bornean Gibbon</li>
+        <li>Silvery Gibbon</li>
+        <li>Pileated Gibbon</li>
+        <li>Kloss's Gibbon</li>
+      </ul>
+    </li>
+    <li>Hoolock
+      <ul>
+        <li>Western Hoolock Gibbon</li>
+        <li>Eastern Hoolock Gibbon</li>
+      </ul>
+    </li>
+    <li>Symphalangus</li>
+    <li>Nomascus
+      <ul>
+        <li>Black Crested Gibbon</li>
+        <li>Eastern Black Crested Gibbon</li>
+        <li>White-cheecked Crested Gibbon</li>
+        <li>Yellow-cheecked Gibbon</li>
+      </ul>
+    </li>
+  </ul>
+
+END;
+        self::assertSame( $expected, $visitor->__toString() );
+    }
+
+    public function testVisitorXHTMLHighlightNodes()
+    {
+        $tree = ezcTreeMemory::create( new ezcTreeMemoryDataStore() );
+        $this->addTestData( $tree );
+
+        $options = new ezcTreeVisitorXHTMLOptions;
+        $options->highlightNodeIds = array( 'Nomascus', 'Eastern Black Crested 
Gibbon' );
+        $options->addLinks = false;
+        $visitor = new ezcTreeVisitorXHTML( $options );
+
+        $tree->fetchNodeById( 'Hylobatidae' )->accept( $visitor );
+        $expected = <<<END
+  <ul>
+    <li>Hylobates
+      <ul>
+        <li>Lar Gibbon</li>
+        <li>Agile Gibbon</li>
+        <li>Müller's Bornean Gibbon</li>
+        <li>Silvery Gibbon</li>
+        <li>Pileated Gibbon</li>
+        <li>Kloss's Gibbon</li>
+      </ul>
+    </li>
+    <li>Hoolock
+      <ul>
+        <li>Western Hoolock Gibbon</li>
+        <li>Eastern Hoolock Gibbon</li>
+      </ul>
+    </li>
+    <li>Symphalangus</li>
+    <li class="highlight">Nomascus
+      <ul>
+        <li>Black Crested Gibbon</li>
+        <li class="highlight">Eastern Black Crested Gibbon</li>
+        <li>White-cheecked Crested Gibbon</li>
+        <li>Yellow-cheecked Gibbon</li>
+      </ul>
+    </li>
+  </ul>
+
+END;
+        self::assertSame( $expected, $visitor->__toString() );
+    }
+
     public static function suite()
     {
          return new PHPUnit_Framework_TestSuite( "ezcTreeVisitorTest" );

Added: trunk/Tree/tests/visitor_options.php
==============================================================================
--- trunk/Tree/tests/visitor_options.php (added)
+++ trunk/Tree/tests/visitor_options.php [iso-8859-1] Tue Aug 28 10:06:57 2007
@@ -1,0 +1,169 @@
+<?php
+/**
+ * ezcTreeVisitorOptionsTest
+ * 
+ * @package Tree
+ * @version //autogen//
+ * @subpackage Tests
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * Tests for ezcTreeVisitorOptions class.
+ * 
+ * @package Tree
+ * @subpackage Tests
+ */
+class ezcTreeVisitorOptionsTest extends ezcTestCase
+{
+    public function testDefaultSettings()
+    {
+        $options = new ezcTreeVisitorXHTMLOptions;
+
+        self::assertSame( '', $options->basePath );
+        self::assertSame( true, $options->addLinks );
+        self::assertSame( false, $options->displayRootNode );
+        self::assertSame( null, $options->xmlId );
+        self::assertSame( array(), $options->highlightNodeIds );
+    }
+
+    public function testGetUnknownProperty()
+    {
+        $options = new ezcTreeVisitorXHTMLOptions;
+        try
+        {
+            $dummy = $options->unknown;
+            self::fail( "Expected exception not thrown." );
+        }
+        catch ( ezcBasePropertyNotFoundException $e )
+        {
+            self::assertSame( "No such property name 'unknown'.", 
$e->getMessage() );
+        }
+    }
+
+    public function testSetValidOptionValues1()
+    {
+        $options = new ezcTreeVisitorXHTMLOptions;
+
+        $options->basePath = '/view';
+        $options->addLinks = false;
+        $options->displayRootNode = true;
+        $options->xmlId = 'menu_tree';
+        $options->highlightNodeIds = array( 'root' );
+
+        self::assertSame( '/view', $options->basePath );
+        self::assertSame( false, $options->addLinks );
+        self::assertSame( true, $options->displayRootNode );
+        self::assertSame( 'menu_tree', $options->xmlId );
+        self::assertSame( array( 'root' ), $options->highlightNodeIds );
+    }
+
+    public function testSetValidOptionValues2()
+    {
+        $optionsArray = array();
+        $optionsArray['basePath'] = '/view';
+        $optionsArray['addLinks'] = false;
+        $optionsArray['displayRootNode'] = true;
+        $optionsArray['xmlId'] = 'menu_tree';
+        $optionsArray['highlightNodeIds'] = array( 'root' );
+
+        $options = new ezcTreeVisitorXHTMLOptions( $optionsArray );
+
+        self::assertSame( '/view', $options->basePath );
+        self::assertSame( false, $options->addLinks );
+        self::assertSame( true, $options->displayRootNode );
+        self::assertSame( 'menu_tree', $options->xmlId );
+        self::assertSame( array( 'root' ), $options->highlightNodeIds );
+    }
+
+    public function testSetInvalidBasePath()
+    {
+        $options = new ezcTreeVisitorXHTMLOptions;
+        try
+        {
+            $options->basePath = 42;
+            self::fail( "Expected exception not thrown." );
+        }
+        catch ( ezcBaseValueException $e )
+        {
+            self::assertSame( "The value '42' that you were trying to assign 
to setting 'basePath' is invalid. Allowed values are: string.", 
$e->getMessage() );
+        }
+    }
+
+    public function testSetInvalidAddLinks()
+    {
+        $options = new ezcTreeVisitorXHTMLOptions;
+        try
+        {
+            $options->addLinks = "no";
+            self::fail( "Expected exception not thrown." );
+        }
+        catch ( ezcBaseValueException $e )
+        {
+            self::assertSame( "The value 'no' that you were trying to assign 
to setting 'addLinks' is invalid. Allowed values are: bool.", $e->getMessage() 
);
+        }
+    }
+
+    public function testSetInvalidDisplayRootNode()
+    {
+        $options = new ezcTreeVisitorXHTMLOptions;
+        try
+        {
+            $options->displayRootNode = 42;
+            self::fail( "Expected exception not thrown." );
+        }
+        catch ( ezcBaseValueException $e )
+        {
+            self::assertSame( "The value '42' that you were trying to assign 
to setting 'displayRootNode' is invalid. Allowed values are: bool.", 
$e->getMessage() );
+        }
+    }
+
+    public function testSetInvalidXmlId()
+    {
+        $options = new ezcTreeVisitorXHTMLOptions;
+        try
+        {
+            $options->xmlId = 42;
+            self::fail( "Expected exception not thrown." );
+        }
+        catch ( ezcBaseValueException $e )
+        {
+            self::assertSame( "The value '42' that you were trying to assign 
to setting 'xmlId' is invalid. Allowed values are: null or string.", 
$e->getMessage() );
+        }
+    }
+
+    public function testSetInvalidHighlightNodes()
+    {
+        $options = new ezcTreeVisitorXHTMLOptions;
+        try
+        {
+            $options->highlightNodeIds = 42;
+            self::fail( "Expected exception not thrown." );
+        }
+        catch ( ezcBaseValueException $e )
+        {
+            self::assertSame( "The value '42' that you were trying to assign 
to setting 'highlightNodeIds' is invalid. Allowed values are: array(string).", 
$e->getMessage() );
+        }
+    }
+
+    public function testSetUnknownProperty()
+    {
+        $options = new ezcTreeVisitorXHTMLOptions;
+        try
+        {
+            $options->unknown = 42;
+            self::fail( "Expected exception not thrown." );
+        }
+        catch ( ezcBasePropertyNotFoundException $e )
+        {
+            self::assertSame( "No such property name 'unknown'.", 
$e->getMessage() );
+        }
+    }
+
+    public static function suite()
+       {
+               return new PHPUnit_Framework_TestSuite( 
"ezcTreeVisitorOptionsTest" );
+       }
+}
+?>

Propchange: trunk/Tree/tests/visitor_options.php
------------------------------------------------------------------------------
    svn:eol-style = native


-- 
svn-components mailing list
[EMAIL PROTECTED]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to