Permalink
Browse files

Merge branch 'form/select-date' of github.com:bakura10/zf2 into form/…

…select-date
  • Loading branch information...
2 parents 57c7d09 + d5c62e0 commit c3a7d8c99773a566691c194cb8bb90f19badb79a @bakura10 bakura10 committed Aug 27, 2012
View
118 library/Zend/Form/Element/DateSelect.php
@@ -0,0 +1,118 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Form
+ */
+
+namespace Zend\Form\Element;
+
+use DateTime;
+use Zend\Form\Form;
+use Zend\Validator\ValidatorInterface;
+use Zend\Validator\Date as DateValidator;
+
+class DateSelect extends MonthSelect
+{
+ /**
+ * Select form element that contains values for day
+ *
+ * @var Select
+ */
+ protected $dayElement;
+
+ /**
+ * Constructor. Add the day select element
+ *
+ * @param null|int|string $name Optional name for the element
+ * @param array $options Optional options for the element
+ */
+ public function __construct($name = null, $options = array())
+ {
+ parent::__construct($name, $options);
+
+ $this->dayElement = new Select('day');
+ }
+
+ /**
+ * @return Select
+ */
+ public function getDayElement()
+ {
+ return $this->dayElement;
+ }
+
+ /**
+ * @param mixed $value
+ * @return void|\Zend\Form\Element
+ */
+ public function setValue($value)
+ {
+ parent::setValue($value);
+ $this->dayElement->setValue($value['day']);
+ }
+
+ /**
+ * Prepare the form element (mostly used for rendering purposes)
+ *
+ * @param Form $form
+ * @return mixed
+ */
+ public function prepareElement(Form $form)
+ {
+ parent::prepareElement($form);
+
+ $name = $this->getName();
+ $this->dayElement->setName($name . '[day]');
+ }
+
+ /**
+ * Get validator
+ *
+ * @return ValidatorInterface
+ */
+ protected function getValidator()
+ {
+ if (null === $this->validator) {
+ $this->validator = new DateValidator(array('format' => 'Y-m-d'));
+ }
+
+ return $this->validator;
+ }
+
+ /**
+ * Should return an array specification compatible with
+ * {@link Zend\InputFilter\Factory::createInput()}.
+ *
+ * @return array
+ */
+ public function getInputSpecification()
+ {
+ return array(
+ 'name' => $this->getName(),
+ 'required' => false,
+ 'filters' => array(
+ array(
+ 'name' => 'Callback',
+ 'options' => array(
+ 'callback' => function($date) {
+ // Convert the date to a specific format
+ if (is_array($date)) {
+ $date = $date['year'] . '-' . $date['month'] . '-' . $date['day'];
+ }
+
+ return $date;
+ }
+ )
+ )
+ ),
+ 'validators' => array(
+ $this->getValidator(),
+ )
+ );
+ }
+}
+
View
213 library/Zend/Form/Element/MonthSelect.php
@@ -0,0 +1,213 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Form
+ */
+
+namespace Zend\Form\Element;
+
+use Zend\Form\Element;
+use Zend\Form\ElementPrepareAwareInterface;
+use Zend\Form\Form;
+use Zend\InputFilter\InputProviderInterface;
+use Zend\Validator\ValidatorInterface;
+use Zend\Validator\Regex as RegexValidator;
+
+class MonthSelect extends Element implements InputProviderInterface, ElementPrepareAwareInterface
+{
+ /**
+ * Select form element that contains values for month
+ *
+ * @var Select
+ */
+ protected $monthElement;
+
+ /**
+ * Select form element that contains values for year
+ *
+ * @var Select
+ */
+ protected $yearElement;
+
+ /**
+ * Min year to use for the select (default: current year - 100)
+ *
+ * @var int
+ */
+ protected $minYear;
+
+ /**
+ * Max year to use for the select (default: current year)
+ *
+ * @var int
+ */
+ protected $maxYear;
+
+ /**
+ * @var ValidatorInterface
+ */
+ protected $validator;
+
+
+ /**
+ * Constructor. Add two selects elements
+ *
+ * @param null|int|string $name Optional name for the element
+ * @param array $options Optional options for the element
+ */
+ public function __construct($name = null, $options = array())
+ {
+ parent::__construct($name, $options);
+
+ $this->monthElement = new Select('month');
+ $this->yearElement = new Select('year');
+
+ $this->maxYear = date('Y');
+ $this->minYear = $this->maxYear - 100;
+ }
+
+ /**
+ * Accepted options for DateSelect:
+ * - min_year: min year to use in the year select
+ * - max_year: max year to use in the year select
+ *
+ * @param array|\Traversable $options
+ * @return DateSelect
+ */
+ public function setOptions($options)
+ {
+ parent::setOptions($options);
+
+ if (isset($options['min_year'])) {
+ $this->setMinYear($options['min_year']);
+ }
+
+ if (isset($options['max_year'])) {
+ $this->setMaxYear($options['max_year']);
+ }
+
+ return $this;
+ }
+
+ /**
+ * @return Select
+ */
+ public function getMonthElement()
+ {
+ return $this->monthElement;
+ }
+
+ /**
+ * @return Select
+ */
+ public function getYearElement()
+ {
+ return $this->yearElement;
+ }
+
+ /**
+ * @param int $minYear
+ * @return DateSelect
+ */
+ public function setMinYear($minYear)
+ {
+ $this->minYear = $minYear;
+ return $this;
+ }
+
+ /**
+ * @return int
+ */
+ public function getMinYear()
+ {
+ return $this->minYear;
+ }
+
+ /**
+ * @param int $maxYear
+ * @return DateSelect
+ */
+ public function setMaxYear($maxYear)
+ {
+ $this->maxYear = $maxYear;
+ return $this;
+ }
+
+ /**
+ * @return int
+ */
+ public function getMaxYear()
+ {
+ return $this->maxYear;
+ }
+
+ /**
+ * @param mixed $value
+ * @return void|\Zend\Form\Element
+ */
+ public function setValue($value)
+ {
+ $this->monthElement->setValue($value['month']);
+ $this->yearElement->setValue($value['year']);
+ }
+
+ /**
+ * Prepare the form element (mostly used for rendering purposes)
+ *
+ * @param Form $form
+ * @return mixed
+ */
+ public function prepareElement(Form $form)
+ {
+ $name = $this->getName();
+ $this->monthElement->setName($name . '[month]');
+ $this->yearElement->setName($name . '[year]');
+ }
+
+ /**
+ * Get validator
+ *
+ * @return ValidatorInterface
+ */
+ protected function getValidator()
+ {
+ return new RegexValidator('/^[0-9]{4}\-(0?[1-9]|1[012])$/');
+ }
+
+ /**
+ * Should return an array specification compatible with
+ * {@link Zend\InputFilter\Factory::createInput()}.
+ *
+ * @return array
+ */
+ public function getInputSpecification()
+ {
+ return array(
+ 'name' => $this->getName(),
+ 'required' => false,
+ 'filters' => array(
+ array(
+ 'name' => 'Callback',
+ 'options' => array(
+ 'callback' => function($date) {
+ // Convert the date to a specific format
+ if (is_array($date)) {
+ $date = $date['year'] . '-' . $date['month'];
+ }
+
+ return $date;
+ }
+ )
+ )
+ ),
+ 'validators' => array(
+ $this->getValidator(),
+ )
+ );
+ }
+}
+
View
95 library/Zend/Form/View/Helper/FormDateSelect.php
@@ -0,0 +1,95 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Form
+ */
+
+namespace Zend\Form\View\Helper;
+
+use DateTime;
+use IntlDateFormatter;
+use Zend\Form\ElementInterface;
+use Zend\Form\Exception;
+use Zend\Form\View\Helper\FormMonthSelect as FormMonthSelectHelper;
+
+/**
+ * @category Zend
+ * @package Zend_Form
+ * @subpackage View
+ */
+class FormDateSelect extends FormMonthSelectHelper
+{
+ /**
+ * Render a date element that is composed of three selects
+ *
+ * @param ElementInterface $element
+ * @throws Exception\DomainException
+ * @return string
+ */
+ public function render(ElementInterface $element)
+ {
+ $name = $element->getName();
+ if ($name === null || $name === '') {
+ throw new Exception\DomainException(sprintf(
+ '%s requires that the element has an assigned name; none discovered',
+ __METHOD__
+ ));
+ }
+
+ $selectHelper = $this->getSelectElementHelper();
+ $pattern = $this->parsePattern();
+
+ $daysOptions = $this->getDaysOptions($pattern['day']);
+ $monthsOptions = $this->getMonthsOptions($pattern['month']);
+ $yearOptions = $this->getYearsOptions($element->getMinYear(), $element->getMaxYear());
+
+ $dayElement = $element->getDayElement()->setAttribute('options', $daysOptions);
+ $monthElement = $element->getMonthElement()->setAttribute('options', $monthsOptions);
+ $yearElement = $element->getYearElement()->setAttribute('options', $yearOptions);
+
+ $markup = array();
+ $markup[$pattern['day']] = $selectHelper->render($dayElement);
+ $markup[$pattern['month']] = $selectHelper->render($monthElement);
+ $markup[$pattern['year']] = $selectHelper->render($yearElement);
+
+ $markup = sprintf(
+ '%s %s %s %s %s',
+ $markup[array_shift($pattern)],
+ array_shift($pattern), // Delimiter
+ $markup[array_shift($pattern)],
+ array_shift($pattern), // Delimiter
+ $markup[array_shift($pattern)]
+ );
+
+ return $markup;
+ }
+
+ /**
+ * Create a key => value options for days
+ *
+ * @param string $pattern Pattern to use for days
+ * @return array
+ */
+ public function getDaysOptions($pattern)
+ {
+ $keyFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, 'dd');
+ $valueFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, $pattern);
+ $date = new DateTime('1970-01-01');
+
+ $result = array();
+ for ($day = 1; $day <= 31; $day++) {
+ $key = $keyFormatter->format($date);
+ $value = $valueFormatter->format($date);
+ $result[$key] = $value;
+
+ $date->modify('+1 day');
+ }
+
+ return $result;
+ }
+}
+
View
10 library/Zend/Form/View/Helper/FormElement.php
@@ -58,6 +58,16 @@ public function render(ElementInterface $element)
return $helper($element);
}
+ if ($element instanceof Element\MonthSelect) {
+ $helper = $renderer->plugin('form_month_select');
+ return $helper($element);
+ }
+
+ if ($element instanceof Element\DateSelect) {
+ $helper = $renderer->plugin('form_date_select');
+ return $helper($element);
+ }
+
$type = $element->getAttribute('type');
$options = $element->getAttribute('options');
View
269 library/Zend/Form/View/Helper/FormMonthSelect.php
@@ -0,0 +1,269 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Form
+ */
+
+namespace Zend\Form\View\Helper;
+
+use DateTime;
+use IntlDateFormatter;
+use Locale;
+use Zend\Form\ElementInterface;
+use Zend\Form\Exception;
+
+/**
+ * @category Zend
+ * @package Zend_Form
+ * @subpackage View
+ */
+class FormMonthSelect extends AbstractHelper
+{
+ /**
+ * FormSelect helper
+ *
+ * @var FormSelect
+ */
+ protected $selectHelper;
+
+ /**
+ * Date formatter to use
+ *
+ * @var int
+ */
+ protected $dateType = IntlDateFormatter::LONG;
+
+ /**
+ * Pattern to use for Date rendering
+ *
+ * @var string
+ */
+ protected $pattern;
+
+ /**
+ * Locale to use
+ *
+ * @var string
+ */
+ protected $locale;
+
+ /**
+ * Render a month element that is composed of two selects
+ *
+ * @param \Zend\Form\ElementInterface $element
+ * @throws \Zend\Form\Exception\DomainException
+ * @return string
+ */
+ public function render(ElementInterface $element)
+ {
+ $name = $element->getName();
+ if ($name === null || $name === '') {
+ throw new Exception\DomainException(sprintf(
+ '%s requires that the element has an assigned name; none discovered',
+ __METHOD__
+ ));
+ }
+
+ $selectHelper = $this->getSelectElementHelper();
+ $pattern = $this->parsePattern();
+
+ // The pattern always contains "day" part and the first separator, so we have to remove it
+ unset($pattern['day']);
+ unset($pattern[0]);
+
+ $monthsOptions = $this->getMonthsOptions($pattern['month']);
+ $yearOptions = $this->getYearsOptions($element->getMinYear(), $element->getMaxYear());
+
+ $monthElement = $element->getMonthElement()->setAttribute('options', $monthsOptions);
+ $yearElement = $element->getYearElement()->setAttribute('options', $yearOptions);
+
+ $markup = array();
+ $markup[$pattern['month']] = $selectHelper->render($monthElement);
+ $markup[$pattern['year']] = $selectHelper->render($yearElement);
+
+ $markup = sprintf(
+ '%s %s %s',
+ $markup[array_shift($pattern)],
+ array_shift($pattern), // Delimiter
+ $markup[array_shift($pattern)]
+ );
+
+ return $markup;
+ }
+
+ /**
+ * Invoke helper as function
+ *
+ * Proxies to {@link render()}.
+ *
+ * @param \Zend\Form\ElementInterface $element
+ * @param int $dateType
+ * @param null|string $locale
+ * @return FormDateSelect
+ */
+ public function __invoke(ElementInterface $element = null, $dateType = IntlDateFormatter::LONG, $locale = null)
+ {
+ if (!$element) {
+ return $this;
+ }
+
+ $this->setDateType($dateType);
+
+ if ($locale !== null) {
+ $this->setLocale($locale);
+ }
+
+ return $this->render($element);
+ }
+
+ /**
+ * @return string
+ */
+ public function getPattern()
+ {
+ if ($this->pattern === null) {
+ $intl = new IntlDateFormatter($this->getLocale(), $this->dateType, IntlDateFormatter::NONE);
+ $this->pattern = $intl->getPattern();
+ }
+
+ return $this->pattern;
+ }
+
+ /**
+ * Parse the pattern
+ *
+ * @return array
+ */
+ protected function parsePattern()
+ {
+ $pattern = $this->getPattern();
+ $pregResult = preg_split('/([ -,.\/]+)/', $pattern, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
+
+ $result = array();
+ foreach ($pregResult as $value) {
+ if (stripos($value, 'd') !== false) {
+ $result['day'] = $value;
+ } elseif (stripos($value, 'm') !== false) {
+ $result['month'] = $value;
+ } elseif (stripos($value, 'y') !== false) {
+ $result['year'] = $value;
+ } else {
+ $result[] = $value;
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * @param int $dateType
+ * @return FormDateSelect
+ */
+ public function setDateType($dateType)
+ {
+ // The FULL format uses values that are not used
+ if ($dateType === IntlDateFormatter::FULL) {
+ $dateType = IntlDateFormatter::LONG;
+ }
+
+ $this->dateType = $dateType;
+
+ return $this;
+ }
+
+ /**
+ * @return int
+ */
+ public function getDateType()
+ {
+ return $this->dateType;
+ }
+
+ /**
+ * @param string $locale
+ * @return FormDateSelect
+ */
+ public function setLocale($locale)
+ {
+ $this->locale = $locale;
+ return $this;
+ }
+
+ /**
+ * @return string
+ */
+ public function getLocale()
+ {
+ if ($this->locale === null) {
+ $this->locale = Locale::getDefault();
+ }
+
+ return $this->locale;
+ }
+
+ /**
+ * Create a key => value options for months
+ *
+ * @param string $pattern Pattern to use for months
+ * @return array
+ */
+ public function getMonthsOptions($pattern)
+ {
+ $keyFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, 'MM');
+ $valueFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, $pattern);
+ $date = new DateTime('1970-01-01');
+
+ $result = array();
+ for ($month = 1; $month <= 12; $month++) {
+ $key = $keyFormatter->format($date);
+ $value = $valueFormatter->format($date);
+ $result[$key] = $value;
+
+ $date->modify('+1 month');
+ }
+
+ return $result;
+ }
+
+ /**
+ * Create a key => value options for years
+ * NOTE: we don't use a pattern for years, as years written as two digits can lead to hard to
+ * read date for users, so we only use four digits years
+ *
+ * @param int $minYear
+ * @param int $maxYear
+ * @return array
+ */
+ public function getYearsOptions($minYear, $maxYear)
+ {
+ $result = array();
+ for ($i = $maxYear; $i >= $minYear; --$i) {
+ $result[$i] = $i;
+ }
+
+ return $result;
+ }
+
+ /**
+ * Retrieve the FormSelect helper
+ *
+ * @return FormRow
+ */
+ protected function getSelectElementHelper()
+ {
+ if ($this->selectHelper) {
+ return $this->selectHelper;
+ }
+
+ if (method_exists($this->view, 'plugin')) {
+ $this->selectHelper = $this->view->plugin('formselect');
+ }
+
+ return $this->selectHelper;
+ }
+}
+
View
2 library/Zend/Form/View/HelperConfig.php
@@ -43,6 +43,7 @@ class HelperConfig implements ConfigInterface
'formdate' => 'Zend\Form\View\Helper\FormDate',
'formdatetime' => 'Zend\Form\View\Helper\FormDateTime',
'formdatetimelocal' => 'Zend\Form\View\Helper\FormDateTimeLocal',
+ 'formdateselect' => 'Zend\Form\View\Helper\FormDateSelect',
'formelement' => 'Zend\Form\View\Helper\FormElement',
'formelementerrors' => 'Zend\Form\View\Helper\FormElementErrors',
'formemail' => 'Zend\Form\View\Helper\FormEmail',
@@ -52,6 +53,7 @@ class HelperConfig implements ConfigInterface
'forminput' => 'Zend\Form\View\Helper\FormInput',
'formlabel' => 'Zend\Form\View\Helper\FormLabel',
'formmonth' => 'Zend\Form\View\Helper\FormMonth',
+ 'formmonthselect' => 'Zend\Form\View\Helper\FormMonthSelect',
'formmulticheckbox' => 'Zend\Form\View\Helper\FormMultiCheckbox',
'formnumber' => 'Zend\Form\View\Helper\FormNumber',
'formpassword' => 'Zend\Form\View\Helper\FormPassword',
View
42 tests/ZendTest/Form/Element/DateSelectTest.php
@@ -0,0 +1,42 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Form
+ */
+
+namespace ZendTest\Form\Element;
+
+use PHPUnit_Framework_TestCase as TestCase;
+use Zend\Form\Element\DateSelect as DateSelectElement;
+use Zend\Form\Factory;
+
+class DateSelectTest extends TestCase
+{
+ public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttributes()
+ {
+ $element = new DateSelectElement();
+
+ $inputSpec = $element->getInputSpecification();
+ $this->assertArrayHasKey('validators', $inputSpec);
+ $this->assertInternalType('array', $inputSpec['validators']);
+
+ $expectedClasses = array(
+ 'Zend\Validator\Date'
+ );
+ foreach ($inputSpec['validators'] as $validator) {
+ $class = get_class($validator);
+ $this->assertTrue(in_array($class, $expectedClasses), $class);
+ switch ($class) {
+ case 'Zend\Validator\Date':
+ $this->assertEquals('Y-m-d', $validator->getFormat());
+ break;
+ default:
+ break;
+ }
+ }
+ }
+}
View
74 tests/ZendTest/Form/Element/MonthSelectTest.php
@@ -0,0 +1,74 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Form
+ */
+
+namespace ZendTest\Form\Element;
+
+use PHPUnit_Framework_TestCase as TestCase;
+use Zend\Form\Element\MonthSelect as MonthSelectElement;
+use Zend\Form\Factory;
+
+class MonthSelectTest extends TestCase
+{
+ public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttributes()
+ {
+ $element = new MonthSelectElement();
+
+ $inputSpec = $element->getInputSpecification();
+ $this->assertArrayHasKey('validators', $inputSpec);
+ $this->assertInternalType('array', $inputSpec['validators']);
+
+ $expectedClasses = array(
+ 'Zend\Validator\Regex'
+ );
+ foreach ($inputSpec['validators'] as $validator) {
+ $class = get_class($validator);
+ $this->assertTrue(in_array($class, $expectedClasses), $class);
+ switch ($class) {
+ case 'Zend\Validator\Regex':
+ $this->assertEquals('/^[0-9]{4}\-(0?[1-9]|1[012])$/', $validator->getPattern());
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ /**
+ * Note about those tests: 2012-1 is not valid in HTML5 validation, but here we use selects, and in some
+ * locales, the month may be expressed using only 1 digit, so this is valid here
+ *
+ * @return array
+ */
+ public function monthValuesDataProvider()
+ {
+ return array(
+ // value expected
+ array('2012-01', true),
+ array('2012-12', true),
+ array('2012-13', false),
+ array('2012-12-01', false),
+ array('12-2012', false),
+ array('2012-1', true),
+ array('12-01', false),
+ );
+ }
+
+ /**
+ * @dataProvider monthValuesDataProvider
+ */
+ public function testMonthValidation($value, $expected)
+ {
+ $element = new MonthSelectElement('foo');
+ $inputSpec = $element->getInputSpecification();
+ $this->assertArrayHasKey('validators', $inputSpec);
+ $monthValidator = $inputSpec['validators'][0];
+ $this->assertEquals($expected, $monthValidator->isValid($value));
+ }
+}
View
58 tests/ZendTest/Form/View/Helper/FormDateSelectTest.php
@@ -0,0 +1,58 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Form
+ */
+
+namespace ZendTest\Form\View\Helper;
+
+use Zend\Form\Element\DateSelect;
+use Zend\Form\View\Helper\FormDateSelect as FormDateSelectHelper;
+
+/**
+ * @category Zend
+ * @package Zend_Form
+ * @subpackage UnitTest
+ */
+class FormDateSelectTest extends CommonTestCase
+{
+ public function setUp()
+ {
+ $this->helper = new FormDateSelectHelper();
+ parent::setUp();
+ }
+
+ public function testRaisesExceptionWhenNameIsNotPresentInElement()
+ {
+ $element = new DateSelect();
+ $this->setExpectedException('Zend\Form\Exception\DomainException', 'name');
+ $this->helper->render($element);
+ }
+
+ public function testGeneratesThreeSelectsWithElement()
+ {
+ $element = new DateSelect('foo');
+ $markup = $this->helper->render($element);
+ $this->assertContains('<select name="day"', $markup);
+ $this->assertContains('<select name="month"', $markup);
+ $this->assertContains('<select name="year"', $markup);
+ }
+
+ public function testInvokeProxiesToRender()
+ {
+ $element = new DateSelect('foo');
+ $markup = $this->helper->__invoke($element);
+ $this->assertContains('<select name="day"', $markup);
+ $this->assertContains('<select name="month"', $markup);
+ $this->assertContains('<select name="year"', $markup);
+ }
+
+ public function testInvokeWithNoElementChainsHelper()
+ {
+ $this->assertSame($this->helper, $this->helper->__invoke());
+ }
+}
View
58 tests/ZendTest/Form/View/Helper/FormMonthSelectTest.php
@@ -0,0 +1,58 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Form
+ */
+
+namespace ZendTest\Form\View\Helper;
+
+use Zend\Form\Element\MonthSelect;
+use Zend\Form\View\Helper\FormMonthSelect as FormMonthSelectHelper;
+
+/**
+ * @category Zend
+ * @package Zend_Form
+ * @subpackage UnitTest
+ */
+class FormMonthSelectTest extends CommonTestCase
+{
+ public function setUp()
+ {
+ $this->helper = new FormMonthSelectHelper();
+ parent::setUp();
+ }
+
+ public function testRaisesExceptionWhenNameIsNotPresentInElement()
+ {
+ $element = new MonthSelect();
+ $this->setExpectedException('Zend\Form\Exception\DomainException', 'name');
+ $this->helper->render($element);
+ }
+
+ public function testGeneratesTwoSelectsWithElement()
+ {
+ $element = new MonthSelect('foo');
+ $markup = $this->helper->render($element);
+ $this->assertNotContains('<select name="day"', $markup);
+ $this->assertContains('<select name="month"', $markup);
+ $this->assertContains('<select name="year"', $markup);
+ }
+
+ public function testInvokeProxiesToRender()
+ {
+ $element = new MonthSelect('foo');
+ $markup = $this->helper->__invoke($element);
+ $this->assertNotContains('<select name="day"', $markup);
+ $this->assertContains('<select name="month"', $markup);
+ $this->assertContains('<select name="year"', $markup);
+ }
+
+ public function testInvokeWithNoElementChainsHelper()
+ {
+ $this->assertSame($this->helper, $this->helper->__invoke());
+ }
+}

0 comments on commit c3a7d8c

Please sign in to comment.