Selaa lähdekoodia

Added unit tests for Block class.

Taddeus Kroes 14 vuotta sitten
vanhempi
sitoutus
51f500d95c
2 muutettua tiedostoa jossa 156 lisäystä ja 25 poistoa
  1. 131 0
      test/utils/block.php
  2. 25 25
      utils/block.php

+ 131 - 0
test/utils/block.php

@@ -0,0 +1,131 @@
+<?php
+
+include_once __DIR__.'/../config.php';
+__p::load_utils('block');
+
+class BlockTest extends PHPUnit_Framework_TestCase {
+	var $block;
+	
+	function setUp() {
+		$this->block = new Block('foo');
+	}
+	
+	function test_constructor() {
+		$count = Block::$count;
+		$block = new Block('foo');
+		$this->assertEquals('foo', $block->name);
+		$this->assertEquals($count + 1, Block::$count);
+	}
+	
+	function test_set_single() {
+		$block = $this->block->set('bar', 'baz');
+		$this->assertEquals('baz', $this->block->vars['bar']);
+		$this->assertSame($this->block, $block);
+	}
+	
+	function test_set_multiple() {
+		$data = array('bar' => 'baz', 'bar2' => 'baz2');
+		$block = $this->block->set($data);
+		$this->assertEquals($data, $this->block->vars);
+		$this->assertSame($this->block, $block);
+	}
+	
+	/**
+	 * @depends test_set_single
+	 */
+	function test_get_simple() {
+		$this->block->set('bar', 'baz');
+		$this->assertEquals('baz', $this->block->get('bar'));
+	}
+	
+	function test_get_null() {
+		$this->assertNull($this->block->get('bar'));
+	}
+	
+	/**
+	 * @depends test_get_simple
+	 */
+	function test_getter() {
+		$this->block->set('bar', 'baz');
+		$this->assertEquals('baz', $this->block->bar);
+	}
+	
+	/**
+	 * @depends test_set_multiple
+	 */
+	function test_add_empty() {
+		$block = $this->block->add('bar');
+		$this->assertInstanceOf('Block', $block);
+		$this->assertEquals('bar', $block->name);
+	}
+	
+	/**
+	 * @depends test_get_simple
+	 * @depends test_add_empty
+	 */
+	function test_add_data() {
+		$block = $this->block->add('bar', array('baz' => 'foo'));
+		$this->assertEquals('foo', $block->get('baz'));
+	}
+	
+	/**
+	 * @depends test_add_empty
+	 * @depends test_get_simple
+	 */
+	function test_get_parent() {
+		$block = $this->block->set('bar', 'baz')->add('new-foo');
+		$this->assertEquals('baz', $block->get('bar'));
+	}
+	
+	/**
+	 * @depends test_add_empty
+	 */
+	function test_find_single() {
+		$block = $this->block->add('bar');
+		$this->block->add('baz');
+		$this->assertSame(array($block), $this->block->find('bar'));
+	}
+	
+	/**
+	 * @depends test_add_empty
+	 */
+	function test_find_multiple() {
+		$block0 = $this->block->add('bar');
+		$block1 = $this->block->add('bar');
+		$this->block->add('baz');
+		$this->assertSame(array($block0, $block1), $this->block->find('bar'));
+	}
+	
+	/**
+	 * @depends test_add_empty
+	 */
+	function test_find_none() {
+		$this->block->add('bar');
+		$this->block->add('baz');
+		$this->assertSame(array(), $this->block->find('foo'));
+	}
+	
+	/**
+	 * @depends test_add_empty
+	 */
+	function test_remove_child() {
+		$block0 = $this->block->add('bar');
+		$block1 = $this->block->add('baz');
+		$ret = $this->block->remove_child($block0);
+		$this->assertSame(array($block1), $this->block->children);
+		$this->assertSame($this->block, $ret);
+	}
+	
+	/**
+	 * @depends test_remove_child
+	 */
+	function test_remove() {
+		$block0 = $this->block->add('bar');
+		$block1 = $this->block->add('baz');
+		$ret = $block0->remove();
+		$this->assertSame(array($block1), $this->block->children);
+		$this->assertSame($block0, $ret);
+	}
+}
+
+?>

+ 25 - 25
utils/block.php

@@ -70,19 +70,6 @@ class Block {
 		$this->parent = $parent;
 	}
 	
-	/**
-	 * Add a child block.
-	 * 
-	 * @param string $name The name of the block to add.
-	 * @param array $data Data to add to the created block (optional).
-	 * @returns Block The created block.
-	 */
-	function add($name, $data=array()) {
-		array_push($this->children, $block = new self($name, $this));
-		
-		return $block->set($data);
-	}
-	
 	/**
 	 * Set the value of one or more variables in the block.
 	 * 
@@ -100,18 +87,6 @@ class Block {
 		return $this;
 	}
 	
-	/**
-	 * Get the value of a variable.
-	 * 
-	 * This method is an equivalent of {@link get()}.
-	 * 
-	 * @param string $name The name of the variable to get the value of.
-	 * @return mixed The value of the variable if it exists, NULL otherwise.
-	 */
-	function __get($name) {
-		return $this->get($name);
-	}
-	
 	/**
 	 * Get the value of a variable.
 	 * 
@@ -131,6 +106,31 @@ class Block {
 		return null;
 	}
 	
+	/**
+	 * Get the value of a variable.
+	 * 
+	 * This method is an equivalent of {@link get()}.
+	 * 
+	 * @param string $name The name of the variable to get the value of.
+	 * @return mixed The value of the variable if it exists, NULL otherwise.
+	 */
+	function __get($name) {
+		return $this->get($name);
+	}
+	
+	/**
+	 * Add a child block.
+	 * 
+	 * @param string $name The name of the block to add.
+	 * @param array $data Data to add to the created block (optional).
+	 * @returns Block The created block.
+	 */
+	function add($name, $data=array()) {
+		array_push($this->children, $block = new self($name, $this));
+		
+		return $block->set($data);
+	}
+	
 	/**
 	 * Find all child blocks with a specified name.
 	 *