You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.0 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-07
  8. *
  9. * Unit tests for Form class
  10. */
  11. require_once dirname(__FILE__) . '/../../view/iView.php';
  12. require_once dirname(__FILE__) . '/../../view/PHPView.php';
  13. require_once dirname(__FILE__) . '/../../view/helpers/ViewHelper.php';
  14. require_once dirname(__FILE__) . '/../../form/FormViewHelper.php';
  15. require_once dirname(__FILE__) . '/../../session/Session.php';
  16. class FormViewHelperTest extends PHPUnit_Framework_TestCase
  17. {
  18. public function setUp()
  19. {
  20. $this->view = new PHPView(array('path' => '/path/to/templates'));
  21. $this->formname = 'formname';
  22. $this->test_array = array('values' => array('key1' => 'val"ue1'), 'messages' => array('field1' => 'Can\'t serialize "value"'));
  23. Session::set($this->formname, $this->test_array);
  24. }
  25. public function testFormWithNotViewInstance()
  26. {
  27. // @TODO check, that iView used in construct
  28. $form = new FormViewHelper('something');
  29. $this->assertInstanceOf('FormViewHelper', $form);
  30. }
  31. public function testFormUnsetFormName()
  32. {
  33. $helper = new FormViewHelper($this->view);
  34. $this->setExpectedException('Exception', 'Form name required for helper init');
  35. // @TODO Refactor for form name is required param?
  36. $helper->form();
  37. }
  38. public function testFormEmptyFormName()
  39. {
  40. $helper = new FormViewHelper($this->view);
  41. $this->setExpectedException('Exception', 'Form name required for helper init');
  42. $helper->form('');
  43. }
  44. public function testFillData()
  45. {
  46. $helper = new FormViewHelper($this->view);
  47. $this->assertAttributeSame(null, 'data', $helper);
  48. $return_obj = $helper->form($this->formname);
  49. $this->assertAttributeSame($this->test_array, 'data', $helper);
  50. $this->assertNull(Session::get($this->formname));
  51. $this->assertSame($helper, $return_obj);
  52. }
  53. public function testValueSet()
  54. {
  55. $helper = new FormViewHelper($this->view);
  56. $helper->form($this->formname);
  57. $value = $helper->value('key1');
  58. $this->assertSame($this->view->escape('val"ue1'), $value);
  59. }
  60. public function testValueDefault()
  61. {
  62. $helper = new FormViewHelper($this->view);
  63. $helper->form($this->formname);
  64. $value = $helper->value('key_not_exist', 'default"');
  65. $this->assertSame($this->view->escape('default"'), $value);
  66. }
  67. public function testMessageSet()
  68. {
  69. $helper = new FormViewHelper($this->view);
  70. $helper->form($this->formname);
  71. $value = $helper->message('field1');
  72. $this->assertSame('<span class="error">' . $this->view->escape('Can\'t serialize "value"') . '</span>', $value);
  73. }
  74. public function testMessageNotSet()
  75. {
  76. $helper = new FormViewHelper($this->view);
  77. $helper->form($this->formname);
  78. $value = $helper->message('key_not_exist', 'default"');
  79. $this->assertSame('', $value);
  80. }
  81. }