Source for file treemap_recorder.php

Documentation is available at treemap_recorder.php

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

Documentation generated on Sun, 31 Oct 2010 16:32:49 -0500 by phpDocumentor 1.4.3