8000 Merge branch '2.7' into 2.8 · symfony/symfony@44cf367 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 44cf367

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: [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 2663293 + 20485d1 commit 44cf367

File tree

6 files changed

+85
-3
lines changed

6 files changed

+85
-3
lines changed

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

Lines changed: 11 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;
@@ -122,6 +123,16 @@ public function all($namespace = null)
122123
return parent::all($namespace);
123124
}
124125

126+
/**
127+
* {@inheritdoc}
128+
*/
129+
public function add(Command $command)
130+
{
131+
$this->registerCommands();
132+
133+
return parent::add($command);
134+
}
135+
125136
protected function registerCommands()
126137
{
127138
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
@@ -465,6 +465,15 @@ public function testAskThrowsExceptionOnMissingInputWithValidator()
465465
$dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
466466
}
467467

468+
/**
469+
* @expectedException \LogicException
470+
* @expectedExceptionMessage Choice question must have at least 1 choice available.
471+
*/
472+
public function testEmptyChoices()
473+
{
474+
new ChoiceQuestion('Question', array(), 'irrelevant');
475+
}
476+
468477
protected function getInputStream($input)
469478
{
470479
$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)
0