/lib/vendor/symfony/test/unit/widget/sfWidgetFormTimeTest.php

https://github.com/frhumanes/PLM · PHP · 143 lines · 101 code · 23 blank · 19 comment · 0 complexity · d357864f82e8ecb9baa07cd42a33d26f MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
  10. $t = new lime_test(40);
  11. $w = new sfWidgetFormTime(array('with_seconds' => true));
  12. $dom = new DomDocument('1.0', 'utf-8');
  13. $dom->validateOnParse = true;
  14. // ->render()
  15. $t->diag('->render()');
  16. foreach (array(
  17. '12:30:35',
  18. mktime(12, 30, 35, 15, 10, 2005),
  19. ) as $date)
  20. {
  21. $dom->loadHTML($w->render('foo', $date));
  22. $css = new sfDomCssSelector($dom);
  23. // selected date
  24. $t->is($css->matchSingle('#foo_hour option[value="12"][selected="selected"]')->getValue(), 12, '->render() renders a select tag for the hour');
  25. $t->is($css->matchSingle('#foo_minute option[value="30"][selected="selected"]')->getValue(), 30, '->render() renders a select tag for the minute');
  26. $t->is($css->matchSingle('#foo_second option[value="35"][selected="selected"]')->getValue(), 35, '->render() renders a select tag for the second');
  27. }
  28. // time as an array
  29. $t->diag('time as an array');
  30. $dom->loadHTML($w->render('foo', array('hour' => 12, 'minute' => '30', 'second' => 35)));
  31. $css = new sfDomCssSelector($dom);
  32. $t->is($css->matchSingle('#foo_hour option[value="12"][selected="selected"]')->getValue(), 12, '->render() renders a select tag for the hour');
  33. $t->is($css->matchSingle('#foo_minute option[value="30"][selected="selected"]')->getValue(), 30, '->render() renders a select tag for the minute');
  34. $t->is($css->matchSingle('#foo_second option[value="35"][selected="selected"]')->getValue(), 35, '->render() renders a select tag for the second');
  35. // time as an array - single digits
  36. $t->diag('time as an array - single digits');
  37. $dom->loadHTML($w->render('foo', '01:03:05'));
  38. $css = new sfDomCssSelector($dom);
  39. $t->is($css->matchSingle('#foo_hour option[value="1"][selected="selected"]')->getValue(), 1, '->render() renders a select tag for the hour');
  40. $t->is($css->matchSingle('#foo_minute option[value="3"][selected="selected"]')->getValue(), 3, '->render() renders a select tag for the minute');
  41. $t->is($css->matchSingle('#foo_second option[value="5"][selected="selected"]')->getValue(), 5, '->render() renders a select tag for the second');
  42. // invalid time
  43. $t->diag('time as an array');
  44. $dom->loadHTML($w->render('foo', array('hour' => null, 'minute' => 30)));
  45. $css = new sfDomCssSelector($dom);
  46. $t->is($css->matchSingle('#foo_hour option[selected="selected"]')->getValue(), '', '->render() renders a select tag for the hour');
  47. $t->is($css->matchSingle('#foo_minute option[selected="selected"]')->getValue(), 30, '->render() renders a select tag for the minute');
  48. $t->is($css->matchSingle('#foo_second option[selected="selected"]')->getValue(), '', '->render() renders a select tag for the second');
  49. $dom->loadHTML($w->render('foo', 'invalidtime'));
  50. $css = new sfDomCssSelector($dom);
  51. $t->is($css->matchSingle('#foo_hour option[selected="selected"]')->getValue(), '', '->render() renders a select tag for the hour');
  52. $t->is($css->matchSingle('#foo_minute option[selected="selected"]')->getValue(), '', '->render() renders a select tag for the minute');
  53. $t->is($css->matchSingle('#foo_second option[selected="selected"]')->getValue(), '', '->render() renders a select tag for the second');
  54. // number of options in each select
  55. $t->diag('number of options in each select');
  56. $dom->loadHTML($w->render('foo', '12:30:35'));
  57. $css = new sfDomCssSelector($dom);
  58. $t->is(count($css->matchAll('#foo_hour option')->getNodes()), 25, '->render() renders a select tag for the 24 hours in a day');
  59. $t->is(count($css->matchAll('#foo_minute option')->getNodes()), 61, '->render() renders a select tag for the 60 minutes in an hour');
  60. $t->is(count($css->matchAll('#foo_second option')->getNodes()), 61, '->render() renders a select tag for the 60 seconds in a minute');
  61. // can_be_empty option
  62. $t->diag('can_be_empty option');
  63. $w->setOption('can_be_empty', false);
  64. $dom->loadHTML($w->render('foo', '2005-10-15'));
  65. $css = new sfDomCssSelector($dom);
  66. $t->is(count($css->matchAll('#foo_hour option')->getNodes()), 24, '->render() renders a select tag for the 24 hours around in a day');
  67. $t->is(count($css->matchAll('#foo_minute option')->getNodes()), 60, '->render() renders a select tag for the 60 minutes in an hour');
  68. $t->is(count($css->matchAll('#foo_second option')->getNodes()), 60, '->render() renders a select tag for the 60 seconds in a minute');
  69. $w->setOption('can_be_empty', true);
  70. // empty_values
  71. $t->diag('empty_values option');
  72. $w->setOption('empty_values', array('hour' => 'HOUR', 'minute' => 'MINUTE', 'second' => 'SECOND'));
  73. $dom->loadHTML($w->render('foo', '2005-10-15'));
  74. $css = new sfDomCssSelector($dom);
  75. $t->is($css->matchSingle('#foo_hour option')->getNode()->nodeValue, 'HOUR', '->configure() can change the empty values');
  76. $t->is($css->matchSingle('#foo_minute option')->getNode()->nodeValue, 'MINUTE', '->configure() can change the empty values');
  77. $t->is($css->matchSingle('#foo_second option')->getNode()->nodeValue, 'SECOND', '->configure() can change the empty values');
  78. $w->setOption('empty_values', array('hour' => '', 'minute' => '', 'second' => ''));
  79. // format option
  80. $t->diag('format option');
  81. $t->like($css->matchSingle('#foo_hour')->getNode()->nextSibling->nodeValue, '/^:/', '->render() renders 3 selects with a default : as a separator');
  82. $t->is($css->matchSingle('#foo_minute')->getNode()->nextSibling->nodeValue, ':', '->render() renders 3 selects with a default : as a separator');
  83. $w->setOption('format', '%hour%#%minute%#%second%');
  84. $dom->loadHTML($w->render('foo', '12:30:35'));
  85. $css = new sfDomCssSelector($dom);
  86. $t->like($css->matchSingle('#foo_hour')->getNode()->nextSibling->nodeValue, '/^#/', '__construct() can change the default format');
  87. $t->is($css->matchSingle('#foo_minute')->getNode()->nextSibling->nodeValue, '#', '__construct() can change the default format');
  88. $w->setOption('format', '%minute%#%hour%#%second%');
  89. $dom->loadHTML($w->render('foo', '12:30:35'));
  90. $css = new sfDomCssSelector($dom);
  91. $t->is($css->matchSingle('select')->getNode()->getAttribute('name'), 'foo[minute]', '__construct() can change the default time format');
  92. // hours / minutes / seconds options
  93. $t->diag('hours / minutes / seconds options');
  94. $w->setOption('hours', array(1 => 1, 2 => 2, 3 => 3, 4 => 4));
  95. $w->setOption('minutes', array(1 => 1, 2 => 2));
  96. $w->setOption('seconds', array(15 => 15, 30 => 30, 45 => 45));
  97. $dom->loadHTML($w->render('foo', '12:30:35'));
  98. $css = new sfDomCssSelector($dom);
  99. $t->is(count($css->matchAll('#foo_hour option')->getNodes()), 5, '__construct() can change the default array used for hours');
  100. $t->is(count($css->matchAll('#foo_minute option')->getNodes()), 3, '__construct() can change the default array used for minutes');
  101. $t->is(count($css->matchAll('#foo_second option')->getNodes()), 4, '__construct() can change the default array used for seconds');
  102. // with_seconds option
  103. $t->diag('with_seconds option');
  104. $w->setOption('with_seconds', false);
  105. $dom->loadHTML($w->render('foo', '12:30:35'));
  106. $css = new sfDomCssSelector($dom);
  107. $t->is(count($css->matchAll('#foo_second option')->getNodes()), 0, '__construct() can enable or disable the seconds select box with the with_seconds option');
  108. $w->setOption('format_without_seconds', '%hour%#%minute%');
  109. $dom->loadHTML($w->render('foo', '12:30:35'));
  110. $css = new sfDomCssSelector($dom);
  111. $t->like($css->matchSingle('#foo_hour')->getNode()->nextSibling->nodeValue, '/^#/', '__construct() can change the default format');
  112. $t->ok(!count($css->matchSingle('#foo_second')->getNodes()), '__construct() can change the default format');
  113. // attributes
  114. $t->diag('attributes');
  115. $w->setOption('with_seconds', true);
  116. $dom->loadHTML($w->render('foo', '12:30:35', array('disabled' => 'disabled')));
  117. $t->is(count($css->matchAll('select[disabled="disabled"]')->getNodes()), 3, '->render() takes the attributes into account for all the three embedded widgets');
  118. $w->setAttribute('disabled', 'disabled');
  119. $dom->loadHTML($w->render('foo', '12:30:35'));
  120. $t->is(count($css->matchAll('select[disabled="disabled"]')->getNodes()), 3, '->render() takes the attributes into account for all the three embedded widgets');