Source for file treemap_recorder.php

Documentation is available at treemap_recorder.php

  1. <?php
  2. /**
  3.  *    extension file for SimpleTest
  4.  *    @version    $Id: treemap_recorder.php 1579 2007-11-02 04:17:44Z maetl_ $
  5.  */
  6. require_once(dirname(__FILE__'/../../scorer.php');
  7.  
  8. /**
  9.  * Collects SimpleReporter messages and constructs a
  10.  * TreemapNode graph.
  11.  */
  12. class TreemapRecorder extends SimpleReporter {
  13.     var $_graph;
  14.     var $_stack;
  15.     var $_title;
  16.  
  17.     function TreemapRecorder({
  18.         $this->SimpleReporter();
  19.         $this->_stack = new TreemapStack();
  20.         $this->_graph = null;
  21.     }
  22.  
  23.     /**
  24.      * returns a reference to the root node of the
  25.      * collected treemap graph
  26.      */
  27.     function getGraph({
  28.         return $this->_graph;
  29.     }
  30.     
  31.     /**
  32.      * is this test run finished?
  33.      */
  34.     function isComplete({
  35.         return ($this->_graph != null);
  36.     }
  37.     
  38.     /**
  39.      * returns the title of the test
  40.      */
  41.     function getTitle({
  42.         return $this->_title;
  43.     }
  44.     
  45.     /**
  46.      * stashes the title of the test
  47.      */
  48.     function paintHeader($title{
  49.         $this->_title = $title;
  50.     }
  51.     
  52.     function paintFormattedMessage({
  53.     }
  54.     
  55.     /**
  56.      * acceptor for start of test group node
  57.      */
  58.     function paintGroupStart($message$size{
  59.         parent::paintGroupStart($message$size);
  60.         $node new TreemapNode("Group"$message);
  61.         $this->_stack->push($node);
  62.     }
  63.     
  64.     /**
  65.      * acceptor for start of test case node
  66.      */
  67.     function paintCaseStart($message{
  68.         parent::paintCaseStart($message);
  69.         $node new TreemapNode("TestCase"$message);
  70.         $this->_stack->push($node);
  71.     }
  72.     
  73.     /**
  74.      * acceptor for start of test method node
  75.      */
  76.     function paintMethodStart($message{
  77.         parent::paintMethodStart($message);
  78.         $node new TreemapNode("Method"$message);
  79.         $this->_stack->push($node);
  80.     }
  81.  
  82.     /**
  83.      * acceptor for passing assertion node
  84.      */
  85.     function paintPass($message{
  86.         parent::paintPass($message);
  87.         $node new TreemapNode("Assertion"$messagetrue);
  88.         $current $this->_stack->peek();
  89.         if ($current{
  90.             $current->putChild($node);
  91.         else {
  92.             echo "no current node";
  93.         }
  94.     }
  95.  
  96.     
  97.     /**
  98.      * acceptor for failing assertion node
  99.      */
  100.  
  101.     function paintFail($message{
  102.         parent::paintFail($message);
  103.         $node new TreemapNode("Assertion"$messagefalse);
  104.         $current $this->_stack->peek();
  105.         $current->putChild($node);
  106.         $current->fail();
  107.     }
  108.  
  109.     /**
  110.      * acceptor for end of method node
  111.      */
  112.     function paintMethodEnd($message{
  113.         parent::paintCaseEnd($message);
  114.         $node $this->_stack->pop();
  115.         $current $this->_stack->peek();
  116.         if ($node->isFailed()) $current->fail();
  117.         $current->putChild($node);
  118.     }
  119.  
  120.     /**
  121.      * acceptor for end of test case
  122.      */
  123.     function paintCaseEnd($message{
  124.         parent::paintCaseEnd($message);
  125.         $node $this->_stack->pop();
  126.         $current $this->_stack->peek();
  127.         if ($node->isFailed()) $current->fail();
  128.         $current->putChild($node);
  129.     }
  130.     
  131.     /**
  132.      * acceptor for end of test group. final group
  133.      * pops the collected treemap nodes and assigns
  134.      * it to the internal graph property.
  135.      */
  136.     function paintGroupEnd($message{
  137.         $node $this->_stack->pop();
  138.         $current $this->_stack->peek();
  139.         if ($current{
  140.             if ($node->isFailed()) $current->fail();
  141.             $current->putChild($node);
  142.         else {
  143.             $this->_graph = $node;
  144.         }
  145.         parent::paintGroupEnd($message);
  146.     }
  147.  
  148. }
  149.  
  150. /**
  151.  * Creates a treemap graph, representing
  152.  * each node in a test visualization.
  153.  */
  154. class TreemapNode {
  155.     var $_name;
  156.     var $_description;
  157.     var $_status;
  158.     var $_parent;
  159.     var $_size;
  160.     
  161.     function TreemapNode($name$description$status=true{
  162.         $this->_name = $name;
  163.         $this->_description = $description;
  164.         $this->_status = $status;
  165.         $this->_children array();
  166.     }
  167.     
  168.     /**
  169.      * @return string label of this node
  170.      */
  171.     function getName({
  172.         return $this->_name;
  173.     }
  174.     
  175.     /**
  176.      * @return string description of this node
  177.      */
  178.     function getDescription({
  179.         return $this->_description;
  180.     }
  181.     
  182.     /**
  183.      * @return string status class string
  184.      */
  185.     function getStatus({
  186.         return ($this->_status"pass" "fail";
  187.     }
  188.     
  189.     /** 
  190.        * Return list of child nodes from direct edges.
  191.      */
  192.     function getChildren({
  193.         uksort($this->_childrenarray($this'compareChildren'));
  194.         return $this->_children;
  195.     }
  196.  
  197.     /**
  198.       * Comparator method to rank child nodes by total weight.
  199.      */
  200.     function compareChildren($a$b{
  201.         if ($this->_children[$a]->getTotalSize($this->_children[$b]->getTotalSize()) {
  202.             $node_a $this->_children[$a];
  203.             $node_b $this->_children[$b];
  204.             $this->_children[$a$node_b;
  205.             $this->_children[$b$node_a;
  206.         }
  207.     }
  208.     
  209.     /** 
  210.       * Gets the number of immediate child edges from this node.
  211.      */
  212.     function getSize({
  213.         return count($this->_children);
  214.     }
  215.     
  216.     /** 
  217.      * depth first search to get the total number of nodes
  218.      * that are descendants of this node.
  219.      */
  220.     function getTotalSize({
  221.         if (!isset($this->_size)) {
  222.             $size $this->getSize();
  223.             if (!$this->isLeaf()) {
  224.                 foreach($this->getChildren(as $child{
  225.                     $size += $child->getTotalSize();
  226.                 }
  227.             }
  228.             $this->_size = $size;
  229.         }
  230.         return $this->_size;
  231.     }
  232.     
  233.     /**
  234.      * Fail this node.
  235.      * @return void 
  236.      */
  237.     function fail({
  238.         $this->_status = false;
  239.     }
  240.     
  241.     /** Is this node failed? */
  242.     function isFailed({
  243.         return ($this->_status == false);
  244.     }
  245.     
  246.     /** Add an edge to a child node */
  247.     function putChild($node{
  248.         $this->_children[$node;
  249.     }
  250.     
  251.     /** Is this node a leaf node? */
  252.     function isLeaf({
  253.         return (count($this->_children== 0);
  254.     }
  255.     
  256. }
  257.  
  258. /**
  259.  * provides LIFO stack semantics
  260.  */
  261. class TreemapStack {
  262.     var $_list;
  263.  
  264.     function TreemapStack({
  265.         $this->_list = array();
  266.     }
  267.  
  268.     /**
  269.      * Push an element onto the stack.
  270.      */
  271.     function push($node{
  272.         $this->_list[$node;
  273.     }
  274.     
  275.     /**
  276.      * Number of elements in the stack.
  277.      */
  278.     function size({
  279.         return count($this->_list);
  280.     }
  281.     
  282.     /**
  283.      * Take a peek at the top element on the
  284.      * stack.
  285.      */
  286.     function peek({
  287.         return end($this->_list);
  288.     }
  289.     
  290.     /**
  291.      * Pops an element off the stack.
  292.      */
  293.     function pop({
  294.         return array_pop($this->_list);
  295.     }
  296.  
  297. }
  298.  
  299. ?>

Documentation generated on Sun, 04 May 2008 09:22:20 -0500 by phpDocumentor 1.3.0