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.

94 lines
3.1 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. require_once dirname(__FILE__) . '/../../exception/InitializationException.php';
  17. class FormViewHelperTest extends PHPUnit_Framework_TestCase
  18. {
  19. public function setUp()
  20. {
  21. $this->view = new PHPView(array('path' => '/path/to/templates'));
  22. $this->formname = 'formname';
  23. $this->test_array = array('values' => array('key1' => 'val"ue1'), 'messages' => array('field1' => 'Can\'t serialize "value"'));
  24. Session::set($this->formname, $this->test_array);
  25. }
  26. public function testFormWithNotViewInstance()
  27. {
  28. // @TODO check, that iView used in construct
  29. $form = new FormViewHelper('something');
  30. $this->assertInstanceOf('FormViewHelper', $form);
  31. }
  32. public function testFormUnsetFormName()
  33. {
  34. $helper = new FormViewHelper($this->view);
  35. $this->setExpectedException('InitializationException', 'Form name required for helper init');
  36. // @TODO Refactor for form name is required param?
  37. $helper->form();
  38. }
  39. public function testFormEmptyFormName()
  40. {
  41. $helper = new FormViewHelper($this->view);
  42. $this->setExpectedException('InitializationException', 'Form name required for helper init');
  43. $helper->form('');
  44. }
  45. public function testFillData()
  46. {
  47. $helper = new FormViewHelper($this->view);
  48. $this->assertAttributeSame(null, 'data', $helper);
  49. $return_obj = $helper->form($this->formname);
  50. $this->assertAttributeSame($this->test_array, 'data', $helper);
  51. $this->assertNull(Session::get($this->formname));
  52. $this->assertSame($helper, $return_obj);
  53. }
  54. public function testValueSet()
  55. {
  56. $helper = new FormViewHelper($this->view);
  57. $helper->form($this->formname);
  58. $value = $helper->value('key1');
  59. $this->assertSame($this->view->escape('val"ue1'), $value);
  60. }
  61. public function testValueDefault()
  62. {
  63. $helper = new FormViewHelper($this->view);
  64. $helper->form($this->formname);
  65. $value = $helper->value('key_not_exist', 'default"');
  66. $this->assertSame($this->view->escape('default"'), $value);
  67. }
  68. public function testMessageSet()
  69. {
  70. $helper = new FormViewHelper($this->view);
  71. $helper->form($this->formname);
  72. $value = $helper->message('field1');
  73. $this->assertSame('<span class="error">' . $this->view->escape('Can\'t serialize "value"') . '</span>', $value);
  74. }
  75. public function testMessageNotSet()
  76. {
  77. $helper = new FormViewHelper($this->view);
  78. $helper->form($this->formname);
  79. $value = $helper->message('key_not_exist', 'default"');
  80. $this->assertSame('', $value);
  81. }
  82. }