Skip to content

Commit

Permalink
Merge branch '3.4' into 4.1
Browse files Browse the repository at this point in the history
* 3.4:
  Ensure final input of CommandTester works with default
  [Intl] handle null date and time types
  Do not ignore the choice groups for caching
  • Loading branch information
Robin Chalas committed Jan 4, 2019
2 parents 4973a5e + b645c07 commit 2d84041
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 58 deletions.
5 changes: 4 additions & 1 deletion src/Symfony/Component/Console/Tester/TesterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ private static function createStream(array $inputs)
{
$stream = fopen('php://memory', 'r+', false);

fwrite($stream, implode(PHP_EOL, $inputs));
foreach ($inputs as $input) {
fwrite($stream, $input.PHP_EOL);
}

rewind($stream);

return $stream;
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/Console/Tests/Tester/CommandTesterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,31 @@ public function testCommandWithInputs()
$this->assertEquals(implode('', $questions), $tester->getDisplay(true));
}

public function testCommandWithDefaultInputs()
{
$questions = array(
'What\'s your name?',
'How are you?',
'Where do you come from?',
);

$command = new Command('foo');
$command->setHelperSet(new HelperSet(array(new QuestionHelper())));
$command->setCode(function ($input, $output) use ($questions, $command) {
$helper = $command->getHelper('question');
$helper->ask($input, $output, new Question($questions[0], 'Bobby'));
$helper->ask($input, $output, new Question($questions[1], 'Fine'));
$helper->ask($input, $output, new Question($questions[2], 'France'));
});

$tester = new CommandTester($command);
$tester->setInputs(array('', '', ''));
$tester->execute(array());

$this->assertEquals(0, $tester->getStatusCode());
$this->assertEquals(implode('', $questions), $tester->getDisplay(true));
}

/**
* @expectedException \RuntimeException
* @expectedMessage Aborted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,6 @@ public static function generateHash($value, $namespace = '')
return hash('sha256', $namespace.':'.serialize($value));
}

/**
* Flattens an array into the given output variable.
*
* @param array $array The array to flatten
* @param array $output The flattened output
*
* @internal
*/
private static function flatten(array $array, &$output)
{
if (null === $output) {
$output = array();
}

foreach ($array as $key => $value) {
if (\is_array($value)) {
self::flatten($value, $output);
continue;
}

$output[$key] = $value;
}
}

public function __construct(ChoiceListFactoryInterface $decoratedFactory)
{
$this->decoratedFactory = $decoratedFactory;
Expand Down Expand Up @@ -113,12 +89,7 @@ public function createListFromChoices($choices, $value = null)
// The value is not validated on purpose. The decorated factory may
// decide which values to accept and which not.

// We ignore the choice groups for caching. If two choice lists are
// requested with the same choices, but a different grouping, the same
// choice list is returned.
self::flatten($choices, $flatChoices);

$hash = self::generateHash(array($flatChoices, $value), 'fromChoices');
$hash = self::generateHash(array($choices, $value), 'fromChoices');

if (!isset($this->lists[$hash])) {
$this->lists[$hash] = $this->decoratedFactory->createListFromChoices($choices, $value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,24 @@ public function testCreateFromChoicesComparesTraversableChoicesAsArray()
$this->assertSame($list, $this->factory->createListFromChoices($choices2));
}

public function testCreateFromChoicesFlattensChoices()
public function testCreateFromChoicesGroupedChoices()
{
$choices1 = array('key' => array('A' => 'a'));
$choices2 = array('A' => 'a');
$list = new \stdClass();
$list1 = new \stdClass();
$list2 = new \stdClass();

$this->decoratedFactory->expects($this->once())
$this->decoratedFactory->expects($this->at(0))
->method('createListFromChoices')
->with($choices1)
->will($this->returnValue($list));
->will($this->returnValue($list1));
$this->decoratedFactory->expects($this->at(1))
->method('createListFromChoices')
->with($choices2)
->will($this->returnValue($list2));

$this->assertSame($list, $this->factory->createListFromChoices($choices1));
$this->assertSame($list, $this->factory->createListFromChoices($choices2));
$this->assertSame($list1, $this->factory->createListFromChoices($choices1));
$this->assertSame($list2, $this->factory->createListFromChoices($choices2));
}

/**
Expand Down
42 changes: 21 additions & 21 deletions src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ class IntlDateFormatter
private $timeZoneId;

/**
* @param string $locale The locale code. The only currently supported locale is "en" (or null using the default locale, i.e. "en")
* @param int $datetype Type of date formatting, one of the format type constants
* @param int $timetype Type of time formatting, one of the format type constants
* @param mixed $timezone Timezone identifier
* @param int $calendar Calendar to use for formatting or parsing. The only currently
* supported value is IntlDateFormatter::GREGORIAN (or null using the default calendar, i.e. "GREGORIAN")
* @param string $pattern Optional pattern to use when formatting
* @param string $locale The locale code. The only currently supported locale is "en" (or null using the default locale, i.e. "en")
* @param int|null $datetype Type of date formatting, one of the format type constants
* @param int|null $timetype Type of time formatting, one of the format type constants
* @param \IntlTimeZone|\DateTimeZone|string|null $timezone Timezone identifier
* @param int $calendar Calendar to use for formatting or parsing. The only currently
* supported value is IntlDateFormatter::GREGORIAN (or null using the default calendar, i.e. "GREGORIAN")
* @param string|null $pattern Optional pattern to use when formatting
*
* @see http://www.php.net/manual/en/intldateformatter.create.php
* @see http://userguide.icu-project.org/formatparse/datetime
Expand All @@ -142,8 +142,8 @@ public function __construct(?string $locale, int $datetype, int $timetype, $time
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'calendar', $calendar, 'Only the GREGORIAN calendar is supported');
}

$this->datetype = $datetype;
$this->timetype = $timetype;
$this->datetype = null !== $datetype ? $datetype : self::FULL;
$this->timetype = null !== $timetype ? $timetype : self::FULL;

$this->setPattern($pattern);
$this->setTimeZone($timezone);
Expand All @@ -152,13 +152,13 @@ public function __construct(?string $locale, int $datetype, int $timetype, $time
/**
* Static constructor.
*
* @param string $locale The locale code. The only currently supported locale is "en" (or null using the default locale, i.e. "en")
* @param int $datetype Type of date formatting, one of the format type constants
* @param int $timetype Type of time formatting, one of the format type constants
* @param string $timezone Timezone identifier
* @param int $calendar Calendar to use for formatting or parsing; default is Gregorian
* One of the calendar constants
* @param string $pattern Optional pattern to use when formatting
* @param string $locale The locale code. The only currently supported locale is "en" (or null using the default locale, i.e. "en")
* @param int|null $datetype Type of date formatting, one of the format type constants
* @param int|null $timetype Type of time formatting, one of the format type constants
* @param \IntlTimeZone|\DateTimeZone|string|null $timezone Timezone identifier
* @param int $calendar Calendar to use for formatting or parsing; default is Gregorian
* One of the calendar constants
* @param string|null $pattern Optional pattern to use when formatting
*
* @return self
*
Expand Down Expand Up @@ -485,7 +485,7 @@ public function setLenient($lenient)
/**
* Set the formatter's pattern.
*
* @param string $pattern A pattern string in conformance with the ICU IntlDateFormatter documentation
* @param string|null $pattern A pattern string in conformance with the ICU IntlDateFormatter documentation
*
* @return bool true on success or false on failure
*
Expand All @@ -506,9 +506,9 @@ public function setPattern($pattern)
/**
* Set the formatter's timezone identifier.
*
* @param string $timeZoneId The time zone ID string of the time zone to use.
* If NULL or the empty string, the default time zone for the
* runtime is used.
* @param string|null $timeZoneId The time zone ID string of the time zone to use.
* If NULL or the empty string, the default time zone for the
* runtime is used.
*
* @return bool true on success or false on failure
*
Expand Down Expand Up @@ -552,7 +552,7 @@ public function setTimeZoneId($timeZoneId)
/**
* This method was added in PHP 5.5 as replacement for `setTimeZoneId()`.
*
* @param mixed $timeZone
* @param \IntlTimeZone|\DateTimeZone|string|null $timeZone
*
* @return bool true on success or false on failure
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ public function testConstructorDefaultTimeZone()
);
}

public function testConstructorWithoutDateType()
{
$formatter = new IntlDateFormatter('en', null, IntlDateFormatter::SHORT, 'UTC', IntlDateFormatter::GREGORIAN);

$this->assertSame('EEEE, LLLL d, y, h:mm a', $formatter->getPattern());
}

public function testConstructorWithoutTimeType()
{
$formatter = new IntlDateFormatter('en', IntlDateFormatter::SHORT, null, 'UTC', IntlDateFormatter::GREGORIAN);

$this->assertSame('M/d/yy, h:mm:ss a zzzz', $formatter->getPattern());
}

/**
* @dataProvider formatProvider
*/
Expand Down

0 comments on commit 2d84041

Please sign in to comment.