Skip to content

Commit b00d332

Browse files
Merge branch '7.4' into 8.0
* 7.4: [HttpFoundation] Deprecate using `Request::sendHeaders()` after headers have already been sent Revert "minor #60377 [HttpFoundation] Emit PHP warning when `Response::sendHeaders()` is called while output has already been sent (ivo95v)" [Validator] Add missing HasNamedArguments to some constraints [Serializer] Remove unused variable Add support for Invokable Commands in CommandTester
2 parents ba5e1d6 + bf1b343 commit b00d332

File tree

20 files changed

+121
-30
lines changed

20 files changed

+121
-30
lines changed

UPGRADE-7.4.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ HttpClient
2222
----------
2323

2424
* Deprecate using amphp/http-client < 5
25+
26+
HttpFoundation
27+
--------------
28+
29+
* Deprecate using `Request::sendHeaders()` after headers have already been sent; use a `StreamedResponse` instead

src/Symfony/Component/Console/Application.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -542,22 +542,7 @@ public function addCommand(callable|Command $command): ?Command
542542
$this->init();
543543

544544
if (!$command instanceof Command) {
545-
if (!\is_object($command) || $command instanceof \Closure) {
546-
throw new InvalidArgumentException(\sprintf('The command must be an instance of "%s" or an invokable object.', Command::class));
547-
}
548-
549-
/** @var AsCommand $attribute */
550-
$attribute = ((new \ReflectionObject($command))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance()
551-
?? throw new LogicException(\sprintf('The command must use the "%s" attribute.', AsCommand::class));
552-
553-
$command = (new Command($attribute->name))
554-
->setDescription($attribute->description ?? '')
555-
->setHelp($attribute->help ?? '')
556-
->setCode($command);
557-
558-
foreach ($attribute->usages as $usage) {
559-
$command->addUsage($usage);
560-
}
545+
$command = new Command(null, $command);
561546
}
562547

563548
$command->setApplication($this);

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ CHANGELOG
1616
* Introduce `Symfony\Component\Console\Application::addCommand()` to simplify using invokable commands when the component is used standalone
1717
* Deprecate `Symfony\Component\Console\Application::add()` in favor of `Symfony\Component\Console\Application::addCommand()`
1818
* Add `BackedEnum` support with `#[Argument]` and `#[Option]` inputs in invokable commands
19-
* Allow Usages to be specified via #[AsCommand] attribute.
19+
* Allow Usages to be specified via `#[AsCommand]` attribute.
20+
* Allow passing invokable commands to `Symfony\Component\Console\Tester\CommandTester`
2021

2122
7.3
2223
---

src/Symfony/Component/Console/Command/Command.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,31 @@ class Command implements SignalableCommandInterface
5959
*
6060
* @throws LogicException When the command name is empty
6161
*/
62-
public function __construct(?string $name = null)
62+
public function __construct(?string $name = null, ?callable $code = null)
6363
{
6464
$this->definition = new InputDefinition();
6565

66+
if ($code !== null) {
67+
if (!\is_object($code) || $code instanceof \Closure) {
68+
throw new InvalidArgumentException(\sprintf('The command must be an instance of "%s" or an invokable object.', Command::class));
69+
}
70+
71+
/** @var AsCommand $attribute */
72+
$attribute = ((new \ReflectionObject($code))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance()
73+
?? throw new LogicException(\sprintf('The command must use the "%s" attribute.', AsCommand::class));
74+
75+
$this->setName($name ?? $attribute->name)
76+
->setDescription($attribute->description ?? '')
77+
->setHelp($attribute->help ?? '')
78+
->setCode($code);
79+
80+
foreach ($attribute->usages as $usage) {
81+
$this->addUsage($usage);
82+
}
83+
84+
return;
85+
}
86+
6687
$attribute = ((new \ReflectionClass(static::class))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();
6788

6889
if (null !== $name ??= $attribute?->name) {

src/Symfony/Component/Console/Tester/CommandTester.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ class CommandTester
2424
{
2525
use TesterTrait;
2626

27+
private Command $command;
28+
2729
public function __construct(
28-
private Command $command,
30+
callable|Command $command,
2931
) {
32+
$this->command = $command instanceof Command ? $command : new Command(null, $command);
3033
}
3134

3235
/**

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
use Symfony\Component\Console\SignalRegistry\SignalRegistry;
4949
use Symfony\Component\Console\Terminal;
5050
use Symfony\Component\Console\Tester\ApplicationTester;
51+
use Symfony\Component\Console\Tests\Fixtures\InvokableExtendingCommandTestCommand;
52+
use Symfony\Component\Console\Tests\Fixtures\InvokableTestCommand;
5153
use Symfony\Component\Console\Tests\Fixtures\MockableAppliationWithTerminalWidth;
5254
use Symfony\Component\DependencyInjection\ContainerBuilder;
5355
use Symfony\Component\EventDispatcher\EventDispatcher;
@@ -257,7 +259,7 @@ public function testAddCommandWithInvokableCommand()
257259
$application->addCommand($foo = new InvokableTestCommand());
258260
$commands = $application->all();
259261

260-
$this->assertInstanceOf(Command::class, $command = $commands['invokable']);
262+
$this->assertInstanceOf(Command::class, $command = $commands['invokable:test']);
261263
$this->assertEquals(new InvokableCommand($command, $foo), (new \ReflectionObject($command))->getProperty('code')->getValue($command));
262264
}
263265

@@ -2570,14 +2572,6 @@ public function isEnabled(): bool
25702572
}
25712573
}
25722574

2573-
#[AsCommand(name: 'invokable')]
2574-
class InvokableTestCommand
2575-
{
2576-
public function __invoke(): int
2577-
{
2578-
}
2579-
}
2580-
25812575
#[AsCommand(name: 'invokable-extended')]
25822576
class InvokableExtendedTestCommand extends Command
25832577
{

src/Symfony/Component/Console/Tests/Command/CommandTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\Component\Console\Output\NullOutput;
2828
use Symfony\Component\Console\Output\OutputInterface;
2929
use Symfony\Component\Console\Tester\CommandTester;
30+
use Symfony\Component\Console\Tests\Fixtures\InvokableTestCommand;
3031

3132
class CommandTest extends TestCase
3233
{
@@ -304,6 +305,13 @@ public function testRunInteractive()
304305
$this->assertEquals('interact called'.\PHP_EOL.'execute called'.\PHP_EOL, $tester->getDisplay(), '->run() calls the interact() method if the input is interactive');
305306
}
306307

308+
public function testInvokableCommand()
309+
{
310+
$tester = new CommandTester(new InvokableTestCommand());
311+
312+
$this->assertSame(Command::SUCCESS, $tester->execute([]));
313+
}
314+
307315
public function testRunNonInteractive()
308316
{
309317
$tester = new CommandTester(new \TestCommand());
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\Component\Console\Tests\Fixtures;
4+
5+
use Symfony\Component\Console\Attribute\AsCommand;
6+
use Symfony\Component\Console\Command\Command;
7+
8+
#[AsCommand('invokable:test')]
9+
class InvokableExtendingCommandTestCommand extends Command
10+
{
11+
public function __invoke(): int
12+
{
13+
return Command::SUCCESS;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\Component\Console\Tests\Fixtures;
4+
5+
use Symfony\Component\Console\Attribute\AsCommand;
6+
use Symfony\Component\Console\Command\Command;
7+
8+
#[AsCommand('invokable:test')]
9+
class InvokableTestCommand
10+
{
11+
public function __invoke(): int
12+
{
13+
return Command::SUCCESS;
14+
}
15+
}

src/Symfony/Component/Console/Tests/Tester/CommandTesterTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use Symfony\Component\Console\Question\Question;
2424
use Symfony\Component\Console\Style\SymfonyStyle;
2525
use Symfony\Component\Console\Tester\CommandTester;
26+
use Symfony\Component\Console\Tests\Fixtures\InvokableExtendingCommandTestCommand;
27+
use Symfony\Component\Console\Tests\Fixtures\InvokableTestCommand;
2628

2729
class CommandTesterTest extends TestCase
2830
{
@@ -267,4 +269,24 @@ public function testErrorOutput()
267269

268270
$this->assertSame('foo', $tester->getErrorOutput());
269271
}
272+
273+
public function testAInvokableCommand()
274+
{
275+
$command = new InvokableTestCommand();
276+
277+
$tester = new CommandTester($command);
278+
$tester->execute([]);
279+
280+
$tester->assertCommandIsSuccessful();
281+
}
282+
283+
public function testAInvokableExtendedCommand()
284+
{
285+
$command = new InvokableExtendingCommandTestCommand();
286+
287+
$tester = new CommandTester($command);
288+
$tester->execute([]);
289+
290+
$tester->assertCommandIsSuccessful();
291+
}
270292
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy