samedi 17 août 2019

There are no commands defined in the "app" namespace

I starting to use symfony for the first time and i follow the examples on the official site related to create a command with standalone console project. here But i cant find the origin of this issue when i run the test.

Test result

PHPUnit 8.3.4 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 92 ms, Memory: 4.00 MB

There was 1 error:

1) App\Tests\Command\CreateUserCommandTest::testExecute
Symfony\Component\Console\Exception\NamespaceNotFoundException: There are no commands defined in the "app" namespace.
/projects/testConsole/vendor/symfony/console/Application.php:589
/projects/testConsole/vendor/symfony/console/Application.php:638
/projects/testConsole/tests/Command/CreateUserCommandTest.php:17

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

composer.json

{
  "name": "nnn",
  "license": "proprietary",
  "description": "dddd",
  "config": {
    "bin-dir": "bin"
  },
  "autoload": {
    "psr-4": {
      "App\\": "./src"
    }
  },
  "require": {
    "php": "^7.2",
    "symfony/console": "^4.1"
  },
  "require-dev": {
    "phpunit/phpunit": "^8.3"
  }
}


src/console.php

#!/usr/bin/env php
<?php

require __DIR__ . '/../vendor/autoload.php';

use Symfony\Component\Console\Application;

$application = new Application();

$application->add(new \App\Command\CreateUserCommand());

$application->run();

src/Command/CreateUserCommand.php

<?php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CreateUserCommand extends Command
{
    protected static $defaultName = 'app:create-user';

    protected function configure()
    {
        // ...
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('hello');
    }
}

$ ./src/console.php app:create-user
hello

Even running only ./src/console.php The command app:create-user appears on the list.

tests/Command/CreateUserCommandTest.php

<?php

namespace App\Tests\Command;

use PHPUnit\Framework\TestCase;
use App\Command\CreateUserCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

class CreateUserCommandTest extends TestCase
{

    public function testExecute()
    {
        $application = new Application();

        $command = $application->find('app:create-user');
        $commandTester = new CommandTester($command);
        $commandTester->execute([
            'command'  => $command->getName(),

            // pass arguments to the helper
            // 'username' => 'Wouter',

            // prefix the key with two dashes when passing options,
            // e.g: '--some-option' => 'option_value',
        ]);

        $output = $commandTester->getDisplay();
        $this->assertContains('hello', $output);
    }
}

I know there are some information related to similar issues but i can't find the way to test a command :/ Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire