DBUIHelpers_PHPTest.php 6.63 KB
<?php
//require_once 'PHPUnit/Framework.php';
require_once 'basepath.inc.php';

require_once $__BASEPATH_EX . 'include/config.php';
require_once $__BASEPATH_EX . 'include/Lib/Tools/mcClasses/mcPUIX/class_mcPUIX_PHP.inc.php';

/**
 * Test class for mcPUIX_PHP.
 * Generated by PHPUnit on 2010-07-13 at 14:12:12.
 */
class mcPUIX_PHPTest extends PHPUnit_Framework_TestCase {

	/**
	 * Sets up the fixture, for example, opens a network connection.
	 * This method is called before a test is executed.
	 */
	protected function setUp() {

	}

	/**
	 * Tears down the fixture, for example, closes a network connection.
	 * This method is called after a test is executed.
	 */
	protected function tearDown() {

	}


	public function testPrepareArrayForDOMElement() {
		$ValueArray = array(
			mcPUIX::PUIXArray => array(
				array(	'key1' => 'value11',
						'key2' => 'value12',
						'key3' => 'value13',
						'key4' => 'value14',
				),
				array(	'key1' => 'value21',
						'key2' => 'value22',
						'key3' => 'value23',
						'key4' => 'value24',
				),				
			)

		);
		$ConfigArray = array(
				'key1' => array('edit'=>true,	'show'=>true,	'type'=>'text'),
				'key2' => array('edit'=>true,	'show'=>false,	'type'=>'text'),
				'key3' => array('edit'=>true,	'show'=>true,	'type'=>'text'),
				'key4' => array('edit'=>false,	'show'=>true,	'type'=>'text'),
		);
		/**
		 * default case
		 */
			$result = mcPUIX_DOM::prepareArrayForDOMElement($ConfigArray, $ValueArray);
			$HaveToResult = array(
				array(
					'key1' => array('edit'=>true,	'show'=>true,	'type'=>'text', 'value'=>'value11'),
					'key2' => array('edit'=>true,	'show'=>false,	'type'=>'text', 'value'=>'value12'),
					'key3' => array('edit'=>true,	'show'=>true,	'type'=>'text', 'value'=>'value13'),
					'key4' => array('edit'=>false,	'show'=>true,	'type'=>'text', 'value'=>'value14'),
				),
				array(
					'key1' => array('edit'=>true,	'show'=>true,	'type'=>'text', 'value'=>'value21'),
					'key2' => array('edit'=>true,	'show'=>false,	'type'=>'text', 'value'=>'value22'),
					'key3' => array('edit'=>true,	'show'=>true,	'type'=>'text', 'value'=>'value23'),
					'key4' => array('edit'=>false,	'show'=>true,	'type'=>'text', 'value'=>'value24'),
				),
			);

			$this->checkArrayDiff($result, $HaveToResult);

		/**
		 * case but more $param_ConfigArray then $param_ValueArray
		 */
			$ConfigArray['key5']	= array('edit'=>false,	'show'=>true,	'type'=>'checkbox');
			$HaveToResult[0]['key5']= array('edit'=>false,	'show'=>true,	'type'=>'checkbox');
			$HaveToResult[1]['key5']= array('edit'=>false,	'show'=>true,	'type'=>'checkbox');
			$result = mcPUIX_DOM::prepareArrayForDOMElement($ConfigArray,$ValueArray);
			$this->checkArrayDiff($result, $HaveToResult);
			unset($ConfigArray['key5']);
			unset($HaveToResult[0]['key5']);
			unset($HaveToResult[1]['key5']);
		
		/**
		 * default case with $param_SetDefault
		 */
			$SetDefault = array(
				'*' => array('type' => 'checkbox',)
			);

			$result = mcPUIX_DOM::prepareArrayForDOMElement($ConfigArray, $ValueArray, $SetDefault);
			$HaveToResult = array(
				array(
					'key1' => array('edit'=>true,	'show'=>true,	'type'=>'checkbox', 'value'=>'value11'),
					'key2' => array('edit'=>true,	'show'=>false,	'type'=>'checkbox', 'value'=>'value12'),
					'key3' => array('edit'=>true,	'show'=>true,	'type'=>'checkbox', 'value'=>'value13'),
					'key4' => array('edit'=>false,	'show'=>true,	'type'=>'checkbox', 'value'=>'value14'),
				),
				array(
					'key1' => array('edit'=>true,	'show'=>true,	'type'=>'checkbox', 'value'=>'value21'),
					'key2' => array('edit'=>true,	'show'=>false,	'type'=>'checkbox', 'value'=>'value22'),
					'key3' => array('edit'=>true,	'show'=>true,	'type'=>'checkbox', 'value'=>'value23'),
					'key4' => array('edit'=>false,	'show'=>true,	'type'=>'checkbox', 'value'=>'value24'),
				),
			);

			$this->checkArrayDiff($result, $HaveToResult);


	}
	
	public function testPrepareArrayForDBOperation() {
		$ConfigArray = array(
			'TABLE' => array(
				'PRIMARY'	=> array('Primary1ID', 'Primary2ID'),
				mcPUIX::FORM_REQUIRED	=> array('Required1', 'Required2'),
			),
			mcPUIX::FORM_DO_ROW => array(),
			'Key1' => array(),
			'Key2' => array(),
		);
		
		/**
		 * nothing new
		 */
			$NewArray = array();
			$result = mcPUIX_PDO::prepareArrayForDBOperation($ConfigArray, $NewArray);
			$HaveToResult = array(
				'UpdateRows' => array(

				),
				'DeleteRows' => array(

				),
				'ErrorRows' => array(

				),
				'NewRows' => array(

				),
			);
			$this->checkArrayDiff($result, $HaveToResult);
		/**
		 *
		 */
			$NewArray = array(
				// UpdateRows
				0 => array(
					mcPUIX::FORM_DO_ROW => 1,
					'Key1' => 'value',
					'Key2' => '',
					'Primary1ID' => 1,
					'Primary2ID' => 2,
					'Required1' => 'require',
					'Required2' => 'require',
				),
				//	DeleteRows
				1 => array(
					mcPUIX::FORM_DO_ROW => false,			// !!!
					'Key1' => 'value',
					'Key2' => '',
					'Primary1ID' => 1,
					'Primary2ID' => 2,
					'Required1' => 'require',
					'Required2' => 'require',
				),
				//	ErrorRows
				2 => array(
					mcPUIX::FORM_DO_ROW => 1,
					'Key1' => 'value',
					'Key2' => '',
					'Primary1ID' => 1,
					'Primary2ID' => 2,
					'Required1' => 'require',
					'Required2' => false,			// !!!
				),
				//	NewRows
				3 => array(
					mcPUIX::FORM_DO_ROW => 1,
					'Key1' => 'value',
					'Key2' => '',
					'Primary1ID' => 0,				// !!!
					'Primary2ID' => 0,				// !!!
					'Required1' => 'require',
					'Required2' => 'require',
				),
			);
			$result = mcPUIX_PDO::prepareArrayForDBOperation($ConfigArray, $NewArray);
			$HaveToResult = array(
				'UpdateRows' => array(
					$NewArray[0]
				),
				'DeleteRows' => array(
					$NewArray[1]
				),
				'ErrorRows' => array(
					$NewArray[2]
				),
				'NewRows' => array(
					$NewArray[3]
				),
			);
			$this->checkArrayDiff($result, $HaveToResult);
	}
	/**
	 *	check arrays rekursiv until the 3. level
	 *
	 * @param array $param_array1
	 * @param array $param_array2
	 */
	private function checkArrayDiff(array $param_array1, array $param_array2) {

		$diff = array_diff_assoc ($param_array1, $param_array2);
		$this->assertTrue(empty($diff), 'validate DOMElement with xsd');
		foreach($param_array1 as $key_1 => $value_1) {
			$diff_1 = array_diff_assoc ($value_1, $param_array2[$key_1]);
			$this->assertTrue(empty($diff_1), 'validate DOMElement with xsd');
			$this->assertTrue((count($value_1) == count($param_array2[$key_1])), 'count');

			foreach($value_1 as $key_2 => $value_2 ) {
				$diff_2 = array_diff_assoc ($value_2, $param_array2[$key_1][$key_2]);
				$this->assertTrue(empty($diff_2), 'validate DOMElement with xsd');
				$this->assertTrue(count($value_2) == count($param_array2[$key_1][$key_2]), 'count');
			}
		}
	}
}
?>