| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  |  * @copyright NetMonsters <team@netmonsters.ru> | 
					
						
							|  |  |  |  * @link http://netmonsters.ru | 
					
						
							|  |  |  |  * @package Majestic | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  |  * @subpackage UnitTests | 
					
						
							|  |  |  |  * @since 2011-10-07 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unit tests for Form class | 
					
						
							| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-20 13:02:46 +04:00
										 |  |  | require_once dirname(__FILE__) . '/../../view/iView.php'; | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../view/PHPView.php'; | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../view/helpers/ViewHelper.php'; | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../form/FormViewHelper.php'; | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../session/Session.php'; | 
					
						
							| 
									
										
										
										
											2011-11-25 13:54:31 +04:00
										 |  |  | require_once dirname(__FILE__) . '/../../exception/InitializationException.php'; | 
					
						
							| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class FormViewHelperTest extends PHPUnit_Framework_TestCase | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function setUp() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  |         $this->view = new PHPView(array('path' => '/path/to/templates')); | 
					
						
							|  |  |  |         $this->formname = 'formname'; | 
					
						
							|  |  |  |         $this->test_array = array('values' => array('key1' => 'val"ue1'), 'messages' => array('field1' => 'Can\'t serialize "value"')); | 
					
						
							|  |  |  |         Session::set($this->formname, $this->test_array); | 
					
						
							| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function testFormWithNotViewInstance() | 
					
						
							| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  |         // @TODO check, that iView used in construct
 | 
					
						
							|  |  |  |         $form = new FormViewHelper('something'); | 
					
						
							|  |  |  |         $this->assertInstanceOf('FormViewHelper', $form); | 
					
						
							| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function testFormUnsetFormName() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $helper = new FormViewHelper($this->view); | 
					
						
							| 
									
										
										
										
											2011-11-25 13:54:31 +04:00
										 |  |  |         $this->setExpectedException('InitializationException', 'Form name required for helper init'); | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  |         // @TODO Refactor for form name is required param?
 | 
					
						
							|  |  |  |         $helper->form(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testFormEmptyFormName() | 
					
						
							| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  |         $helper = new FormViewHelper($this->view); | 
					
						
							| 
									
										
										
										
											2011-11-25 13:54:31 +04:00
										 |  |  |         $this->setExpectedException('InitializationException', 'Form name required for helper init'); | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  |         $helper->form(''); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testFillData() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $helper = new FormViewHelper($this->view); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertAttributeSame(null, 'data', $helper); | 
					
						
							|  |  |  |         $return_obj = $helper->form($this->formname); | 
					
						
							|  |  |  |         $this->assertAttributeSame($this->test_array, 'data', $helper); | 
					
						
							|  |  |  |         $this->assertNull(Session::get($this->formname)); | 
					
						
							|  |  |  |         $this->assertSame($helper, $return_obj); | 
					
						
							| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function testValueSet() | 
					
						
							| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  |         $helper = new FormViewHelper($this->view); | 
					
						
							|  |  |  |         $helper->form($this->formname); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $value = $helper->value('key1'); | 
					
						
							|  |  |  |         $this->assertSame($this->view->escape('val"ue1'), $value); | 
					
						
							| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function testValueDefault() | 
					
						
							| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  |         $helper = new FormViewHelper($this->view); | 
					
						
							|  |  |  |         $helper->form($this->formname); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $value = $helper->value('key_not_exist', 'default"'); | 
					
						
							|  |  |  |         $this->assertSame($this->view->escape('default"'), $value); | 
					
						
							| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function testMessageSet() | 
					
						
							| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  |         $helper = new FormViewHelper($this->view); | 
					
						
							|  |  |  |         $helper->form($this->formname); | 
					
						
							|  |  |  |         $value = $helper->message('field1'); | 
					
						
							|  |  |  |         $this->assertSame('<span class="error">' . $this->view->escape('Can\'t serialize "value"') . '</span>', $value); | 
					
						
							| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  |     public function testMessageNotSet() | 
					
						
							| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-10-24 16:58:20 +04:00
										 |  |  |         $helper = new FormViewHelper($this->view); | 
					
						
							|  |  |  |         $helper->form($this->formname); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $value = $helper->message('key_not_exist', 'default"'); | 
					
						
							|  |  |  |         $this->assertSame('', $value); | 
					
						
							| 
									
										
										
										
											2011-10-20 12:40:13 +04:00
										 |  |  |     } | 
					
						
							|  |  |  | } |