Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6f72b34

Browse files
committed
Merge branch '3.4'
* 3.4: [Console] ChoiceQuestion must have choices [Filesystem] improve error handling in lock() [FrameworkBundle][Console] Fix the override of a command registered by the kernel
2 parents f25ef35 + 0cfb574 commit 6f72b34

File tree

6 files changed

+82
-3
lines changed

6 files changed

+82
-3
lines changed

src/Symfony/Bundle/FrameworkBundle/Console/Application.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1515
use Symfony\Component\Console\Application as BaseApplication;
16+
use Symfony\Component\Console\Command\Command;
1617
use Symfony\Component\Console\Input\InputInterface;
1718
use Symfony\Component\Console\Input\InputOption;
1819
use Symfony\Component\Console\Output\OutputInterface;
@@ -118,6 +119,13 @@ public function getLongVersion()
118119
return parent::getLongVersion().sprintf(' (kernel: <comment>%s</>, env: <comment>%s</>, debug: <comment>%s</>)', $this->kernel->getName(), $this->kernel->getEnvironment(), $this->kernel->isDebug() ? 'true' : 'false');
119120
}
120121

122+
public function add(Command $command)
123+
{
124+
$this->registerCommands();
125+
126+
return parent::add($command);
127+
}
128+
121129
protected function registerCommands()
122130
{
123131
if ($this->commandsRegistered) {

src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ public function testBundleCommandsHaveRightContainer()
115115
$tester->run(array('command' => 'foo'));
116116
}
117117

118+
public function testBundleCommandCanOverriddeAPreExistingCommandWithTheSameName()
119+
{
120+
$command = new Command('example');
121+
122+
$bundle = $this->createBundleMock(array($command));
123+
124+
$kernel = $this->getKernel(array($bundle));
125+
126+
$application = new Application($kernel);
127+
$newCommand = new Command('example');
128+
$application->add($newCommand);
129+
130+
$this->assertSame($newCommand, $application->get('example'));
131+
}
132+
118133
private function getKernel(array $bundles, $useDispatcher = false)
119134
{
120135
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();

src/Symfony/Component/Console/Question/ChoiceQuestion.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class ChoiceQuestion extends Question
3434
*/
3535
public function __construct($question, array $choices, $default = null)
3636
{
37+
if (!$choices) {
38+
throw new \LogicException('Choice question must have at least 1 choice available.');
39+
}
40+
3741
parent::__construct($question, $default);
3842

3943
$this->choices = $choices;

src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,15 @@ public function testAskThrowsExceptionOnMissingInputWithValidator()
452452
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), $question);
453453
}
454454

455+
/**
456+
* @expectedException \LogicException
457+
* @expectedExceptionMessage Choice question must have at least 1 choice available.
458+
*/
459+
public function testEmptyChoices()
460+
{
461+
new ChoiceQuestion('Question', array(), 'irrelevant');
462+
}
463+
455464
protected function getInputStream($input)
456465
{
457466
$stream = fopen('php://memory', 'r+', false);

src/Symfony/Component/Filesystem/LockHandler.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ public function lock($blocking = false)
6868
return true;
6969
}
7070

71+
$error = null;
72+
7173
// Silence error reporting
72-
set_error_handler(function () {});
74+
set_error_handler(function ($errno, $msg) use (&$error) {
75+
$error = $msg;
76+
});
7377

7478
if (!$this->handle = fopen($this->file, 'r')) {
7579
if ($this->handle = fopen($this->file, 'x')) {
@@ -82,8 +86,7 @@ public function lock($blocking = false)
8286
restore_error_handler();
8387

8488
if (!$this->handle) {
85-
$error = error_get_last();
86-
throw new IOException($error['message'], 0, null, $this->file);
89+
throw new IOException($error, 0, null, $this->file);
8790
}
8891

8992
// On Windows, even if PHP doc says the contrary, LOCK_NB works, see

src/Symfony/Component/Filesystem/Tests/LockHandlerTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Filesystem\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Filesystem\Exception\IOException;
16+
use Symfony\Component\Filesystem\Filesystem;
1517
use Symfony\Component\Filesystem\LockHandler;
1618

1719
class LockHandlerTest extends TestCase
@@ -40,6 +42,44 @@ public function testConstructWhenRepositoryIsNotWriteable()
4042
new LockHandler('lock', '/');
4143
}
4244

45+
public function testErrorHandlingInLockIfLockPathBecomesUnwritable()
46+
{
47+
// skip test on Windows; PHP can't easily set file as unreadable on Windows
48+
if ('\\' === DIRECTORY_SEPARATOR) {
49+
$this->markTestSkipped('This test cannot run on Windows.');
50+
}
51+
52+
$lockPath = sys_get_temp_dir().'/'.uniqid();
53+
$e = null;
54+
$wrongMessage = null;
55+
56+
try {
57+
mkdir($lockPath);
58+
59+
$lockHandler = new LockHandler('lock', $lockPath);
60+
61+
chmod($lockPath, 0444);
62+
63+
$lockHandler->lock();
64+
} catch (IOException $e) {
65+
if (false === strpos($e->getMessage(), 'Permission denied')) {
66+
$wrongMessage = $e->getMessage();
67+
} else {
68+
$this->addToAssertionCount(1);
69+
}
70+
} catch (\Exception $e) {
71+
} catch (\Throwable $e) {
72+
}
73+
74+
if (is_dir($lockPath)) {
75+
$fs = new Filesystem();
76+
$fs->remove($lockPath);
77+
}
78+
79+
$this->assertInstanceOf('Symfony\Component\Filesystem\Exception\IOException', $e, sprintf('Expected IOException to be thrown, got %s instead.', get_class($e)));
80+
$this->assertNull($wrongMessage, sprintf('Expected exception message to contain "Permission denied", got "%s" instead.', $wrongMessage));
81+
}
82+
4383
public function testConstructSanitizeName()
4484
{
4585
$lock = new LockHandler('<?php echo "% hello word ! %" ?>');

0 commit comments

Comments
 (0)