Development

Changeset 6340

You must first sign up to be able to contribute.

Changeset 6340

Show
Ignore:
Timestamp:
12/06/07 17:00:57 (7 months ago)
Author:
fabien
Message:

refactored the date time widget + fixed unit tests + added unit tests for empty_values and can_be_empty

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/widget/sfWidgetFormDateTime.class.php

    r6327 r6340  
    6565  { 
    6666    // date 
    67     $date = new sfWidgetFormDate($this->getOptionsFor('date'), $this->getAttributesFor('date')); 
     67    $date = $this->getDateWidget()->render($name, $value); 
    6868 
    6969    if (!$this->getOption('with_time')) 
    7070    { 
    71       return $date->render($name, $value)
     71      return $date
    7272    } 
    7373 
    74     $dateTime = array('%date%' => $date->render($name, $value)); 
    75  
    76     // time 
    77     $time = new sfWidgetFormTime($this->getOptionsFor('time'), $this->getAttributesFor('time')); 
    78  
    79     $dateTime['%time%'] = $time->render($name, $value); 
    80  
    81     return strtr($this->getOption('format'), $dateTime); 
     74    return strtr($this->getOption('format'), array( 
     75      '%date%' => $date, 
     76      '%time%' => $this->getTimeWidget()->render($name, $value), 
     77    )); 
    8278  } 
    8379 
     80  /** 
     81   * Returns the date widget 
     82   * 
     83   * @return sfWidgetForm A Widget representing the date 
     84   */ 
     85  protected function getDateWidget() 
     86  { 
     87    return new sfWidgetFormDate($this->getOptionsFor('date'), $this->getAttributesFor('date')); 
     88  } 
     89 
     90  /** 
     91   * Returns the time widget 
     92   * 
     93   * @return sfWidgetForm A Widget representing the time 
     94   */ 
     95  protected function getTimeWidget() 
     96  { 
     97    return new sfWidgetFormTime($this->getOptionsFor('time'), $this->getAttributesFor('time')); 
     98  } 
     99 
     100  /** 
     101   * Returns an array of options for the given type 
     102   * 
     103   * @param  string The type (date or time) 
     104   * 
     105   * @return array  An array of options 
     106   */ 
    84107  protected function getOptionsFor($type) 
    85108  { 
     
    93116  } 
    94117 
     118  /** 
     119   * Returns an array of HTML attributes for the given type 
     120   * 
     121   * @param  string The type (date or time) 
     122   * 
     123   * @return array  An array of HTML attributes 
     124   */ 
    95125  protected function getAttributesFor($type) 
    96126  { 
  • trunk/test/unit/widget/sfWidgetFormDateTest.php

    r6327 r6340  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(22, new lime_output_color()); 
     13$t = new lime_test(29, new lime_output_color()); 
    1414 
    1515$w = new sfWidgetFormDate(); 
     
    4949 
    5050// number of options in each select 
     51$t->is(count($css->matchAll('#foo_year option')->getNodes()), 12, '->render() renders a select tag for the 10 years around the current one'); 
     52$t->is(count($css->matchAll('#foo_month option')->getNodes()), 13, '->render() renders a select tag for the 12 months in a year'); 
     53$t->is(count($css->matchAll('#foo_day option')->getNodes()), 32, '->render() renders a select tag for the 31 days in a month'); 
     54 
     55// can_be_empty option 
     56$t->diag('can_be_empty option'); 
     57$w->setOption('can_be_empty', false); 
     58$dom->loadHTML($w->render('foo', '2005-10-15')); 
     59$css = new sfDomCssSelector($dom); 
    5160$t->is(count($css->matchAll('#foo_year option')->getNodes()), 11, '->render() renders a select tag for the 10 years around the current one'); 
    5261$t->is(count($css->matchAll('#foo_month option')->getNodes()), 12, '->render() renders a select tag for the 12 months in a year'); 
    5362$t->is(count($css->matchAll('#foo_day option')->getNodes()), 31, '->render() renders a select tag for the 31 days in a month'); 
     63$w->setOption('can_be_empty', true); 
     64 
     65// empty_values 
     66$t->diag('empty_values option'); 
     67$w->setOption('empty_values', array('year' => 'YEAR', 'month' => 'MONTH', 'day' => 'DAY')); 
     68$dom->loadHTML($w->render('foo', '2005-10-15')); 
     69$css = new sfDomCssSelector($dom); 
     70$t->is($css->matchSingle('#foo_year option')->getNode()->nodeValue, 'YEAR', '->configure() can change the empty values'); 
     71$t->is($css->matchSingle('#foo_month option')->getNode()->nodeValue, 'MONTH', '->configure() can change the empty values'); 
     72$t->is($css->matchSingle('#foo_day option')->getNode()->nodeValue, 'DAY', '->configure() can change the empty values'); 
     73$w->setOption('empty_values', array('year' => '', 'month' => '', 'day' => '')); 
    5474 
    5575// format option 
     
    7696$dom->loadHTML($w->render('foo', '2005-10-15')); 
    7797$css = new sfDomCssSelector($dom); 
    78 $t->is(count($css->matchAll('#foo_year option')->getNodes()), 4, '__construct() can change the default array used for years'); 
    79 $t->is(count($css->matchAll('#foo_month option')->getNodes()), 3, '__construct() can change the default array used for months'); 
    80 $t->is(count($css->matchAll('#foo_day option')->getNodes()), 2, '__construct() can change the default array used for days'); 
     98$t->is(count($css->matchAll('#foo_year option')->getNodes()), 5, '__construct() can change the default array used for years'); 
     99$t->is(count($css->matchAll('#foo_month option')->getNodes()), 4, '__construct() can change the default array used for months'); 
     100$t->is(count($css->matchAll('#foo_day option')->getNodes()), 3, '__construct() can change the default array used for days'); 
  • trunk/test/unit/widget/sfWidgetFormDateTimeTest.php

    r6327 r6340  
    5555 
    5656// number of options in each select 
    57 $t->is(count($css->matchAll('#foo_year option')->getNodes()), 11, '->render() renders a select tag for the 10 years around the current one'); 
    58 $t->is(count($css->matchAll('#foo_month option')->getNodes()), 12, '->render() renders a select tag for the 12 months in a year'); 
    59 $t->is(count($css->matchAll('#foo_day option')->getNodes()), 31, '->render() renders a select tag for the 31 days in a month'); 
    60 $t->is(count($css->matchAll('#foo_hour option')->getNodes()), 24, '->render() renders a select tag for the 24 hours in a day'); 
    61 $t->is(count($css->matchAll('#foo_minute option')->getNodes()), 60, '->render() renders a select tag for the 60 minutes in an hour'); 
    62 $t->is(count($css->matchAll('#foo_second option')->getNodes()), 60, '->render() renders a select tag for the 60 seconds in a minute'); 
     57$t->is(count($css->matchAll('#foo_year option')->getNodes()), 12, '->render() renders a select tag for the 10 years around the current one'); 
     58$t->is(count($css->matchAll('#foo_month option')->getNodes()), 13, '->render() renders a select tag for the 12 months in a year'); 
     59$t->is(count($css->matchAll('#foo_day option')->getNodes()), 32, '->render() renders a select tag for the 31 days in a month'); 
     60$t->is(count($css->matchAll('#foo_hour option')->getNodes()), 25, '->render() renders a select tag for the 24 hours in a day'); 
     61$t->is(count($css->matchAll('#foo_minute option')->getNodes()), 61, '->render() renders a select tag for the 60 minutes in an hour'); 
     62$t->is(count($css->matchAll('#foo_second option')->getNodes()), 61, '->render() renders a select tag for the 60 seconds in a minute'); 
    6363 
    6464// date and time format option 
  • trunk/test/unit/widget/sfWidgetFormTimeTest.php

    r6327 r6340  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(23, new lime_output_color()); 
     13$t = new lime_test(29, new lime_output_color()); 
    1414 
    1515$w = new sfWidgetFormTime(array('with_seconds' => true)); 
     
    4848 
    4949// number of options in each select 
    50 $t->is(count($css->matchAll('#foo_hour option')->getNodes()), 24, '->render() renders a select tag for the 24 hours in a day'); 
     50$t->is(count($css->matchAll('#foo_hour option')->getNodes()), 25, '->render() renders a select tag for the 24 hours in a day'); 
     51$t->is(count($css->matchAll('#foo_minute option')->getNodes()), 61, '->render() renders a select tag for the 60 minutes in an hour'); 
     52$t->is(count($css->matchAll('#foo_second option')->getNodes()), 61, '->render() renders a select tag for the 60 seconds in a minute'); 
     53 
     54// can_be_empty option 
     55$t->diag('can_be_empty option'); 
     56$w->setOption('can_be_empty', false); 
     57$dom->loadHTML($w->render('foo', '2005-10-15')); 
     58$css = new sfDomCssSelector($dom); 
     59$t->is(count($css->matchAll('#foo_hour option')->getNodes()), 24, '->render() renders a select tag for the 24 hours around in a day'); 
    5160$t->is(count($css->matchAll('#foo_minute option')->getNodes()), 60, '->render() renders a select tag for the 60 minutes in an hour'); 
    5261$t->is(count($css->matchAll('#foo_second option')->getNodes()), 60, '->render() renders a select tag for the 60 seconds in a minute'); 
     62$w->setOption('can_be_empty', true); 
     63 
     64// empty_values 
     65$t->diag('empty_values option'); 
     66$w->setOption('empty_values', array('hour' => 'HOUR', 'minute' => 'MINUTE', 'second' => 'SECOND')); 
     67$dom->loadHTML($w->render('foo', '2005-10-15')); 
     68$css = new sfDomCssSelector($dom); 
     69$t->is($css->matchSingle('#foo_hour option')->getNode()->nodeValue, 'HOUR', '->configure() can change the empty values'); 
     70$t->is($css->matchSingle('#foo_minute option')->getNode()->nodeValue, 'MINUTE', '->configure() can change the empty values'); 
     71$t->is($css->matchSingle('#foo_second option')->getNode()->nodeValue, 'SECOND', '->configure() can change the empty values'); 
     72$w->setOption('empty_values', array('hour' => '', 'minute' => '', 'second' => '')); 
    5373 
    5474// format option 
     
    7595$dom->loadHTML($w->render('foo', '12:30:35')); 
    7696$css = new sfDomCssSelector($dom); 
    77 $t->is(count($css->matchAll('#foo_hour option')->getNodes()), 4, '__construct() can change the default array used for hours'); 
    78 $t->is(count($css->matchAll('#foo_minute option')->getNodes()), 2, '__construct() can change the default array used for minutes'); 
    79 $t->is(count($css->matchAll('#foo_second option')->getNodes()), 3, '__construct() can change the default array used for seconds'); 
     97$t->is(count($css->matchAll('#foo_hour option')->getNodes()), 5, '__construct() can change the default array used for hours'); 
     98$t->is(count($css->matchAll('#foo_minute option')->getNodes()), 3, '__construct() can change the default array used for minutes'); 
     99$t->is(count($css->matchAll('#foo_second option')->getNodes()), 4, '__construct() can change the default array used for seconds'); 
    80100 
    81101// with_seconds option