Repository: symfony/symfony
File: src/Symfony/Component/Console/Tests/Command/InvokableCommandTest.php (PHP)

Code before:
        $this->assertSame('__invoke', $command->called);
    }

    public function testInvalidReturnType()
    {
        $command = new Command('foo');
        $command->setCode(new class {
            public function __invoke()
            {
            }
        });

        $this->expectException(\TypeError::class);
        $this->expectExceptionMessage('The command "foo" must return an integer value in the "__invoke" method, but "null" was returned.');

        $command->run(new ArrayInput([]), new NullOutput());
    }

    public function testGetCode()
    {
        // Create a command from an invokable class.
        $command = new Command(null, new InvokableTestCommand());

        // Ensure that the invokable class can be retrieved from the Command.
        $this->assertInstanceOf(InvokableCommand::class, $command->getCode());
        $this->assertInstanceOf(InvokableTestCommand::class, $command->getCode()->getCallable());
    }

    /**
     * @dataProvider provideInputArguments
     */
    public function testInputArguments(array $parameters, array $expected)
    {
        $command = new Command('foo');
        $command->setCode(function (
            #[Argument] string $a,
            #[Argument] ?string $b,
            #[Argument] string $c = '',
            #[Argument] array $d = [],
        ) use ($expected): int {
            $this->assertSame($expected[0], $a);
            $this->assertSame($expected[1], $b);
            $this->assertSame($expected[2], $c);
            $this->assertSame($expected[3], $d);

            return 0;
        });

        $command->run(new ArrayInput($parameters), new NullOutput());
    }


Review comment (suggestion):
```suggestion
        $this->assertInstanceOf(InvokableTestCommand::class, $command->getCode());
```

Code after:
        $this->assertSame('__invoke', $command->called);
    }

    public function testInvalidReturnType()
    {
        $command = new Command('foo');
        $command->setCode(new class {
            public function __invoke()
            {
            }
        });

        $this->expectException(\TypeError::class);
        $this->expectExceptionMessage('The command "foo" must return an integer value in the "__invoke" method, but "null" was returned.');

        $command->run(new ArrayInput([]), new NullOutput());
    }

    public function testGetCode()
    {
        $invokableTestCommand = new InvokableTestCommand();
        $command = new Command(null, $invokableTestCommand);

        $this->assertSame($invokableTestCommand, $command->getCode());
    }

    #[DataProvider('provideInputArguments')]
    public function testInputArguments(array $parameters, array $expected)
    {
        $command = new Command('foo');
        $command->setCode(function (
            #[Argument] string $a,
            #[Argument] ?string $b,
            #[Argument] string $c = '',
            #[Argument] array $d = [],
        ) use ($expected): int {
            $this->assertSame($expected[0], $a);
            $this->assertSame($expected[1], $b);
            $this->assertSame($expected[2], $c);
            $this->assertSame($expected[3], $d);

            return 0;
        });

        $command->run(new ArrayInput($parameters), new NullOutput());
    }

    public static function provideInputArguments(): \Generator
    {
        yield 'required & defaults' => [['a' => 'x'], ['x', null, '', []]];
        yield 'required & with-value' => [['a' => 'x', 'b' => 'y', 'c' => 'z', 'd' => ['d']], ['x', 'y', 'z', ['d']]];