CommandTester example

protected function setUp(): void
    {
        $this->refreshIndexCommand = $this->getContainer()->get(ElasticsearchResetCommand::class);
        $this->getContainer()->get(AbstractKeyValueStorage::class)->set(ElasticsearchIndexer::ENABLE_MULTILINGUAL_INDEX_KEY, 1);

        $this->connection = $this->getContainer()->get(Connection::class);
    }

    public function testExecuteWithInputNo(): void
    {
        $commandTester = new CommandTester($this->refreshIndexCommand);
        $commandTester->setInputs(['no']);
        $commandTester->execute([]);

        $message = $commandTester->getDisplay();

        static::assertStringContainsString('Are you sure you want to reset the Elasticsearch indexing?', $message);
        static::assertStringContainsString('Canceled clearing indexing process', $message);
    }

    public function testExecuteWithInput(): void
    {
        
$errors = array_filter($errors);
        $errorMessage = 'Found some issues in the following files:' . \PHP_EOL . \PHP_EOL . print_r($errors, true);

        static::assertCount(0, $errors$errorMessage);
    }

    public function testContainerLintCommand(): void
    {
        $command = $this->getContainer()->get('console.command.container_lint');
        $command->setApplication(new Application(KernelLifecycleManager::getKernel()));
        $commandTester = new CommandTester($command);

        set_error_handler(fn (): bool => true, \E_USER_DEPRECATED);
        $commandTester->execute([]);
        restore_error_handler();

        static::assertEquals(
            0,
            $commandTester->getStatusCode(),
            "\"bin/console lint:container\" returned errors:\n" . $commandTester->getDisplay()
        );
    }

    
use Symfony\Component\Console\Tester\CommandTester;

/** * @internal */
class StoreLoginCommandTest extends TestCase
{
    use IntegrationTestBehaviour;

    public function testEmptyPasswordOption(): void
    {
        $commandTester = new CommandTester($this->getContainer()->get(StoreLoginCommand::class));

        static::expectException(\RuntimeException::class);
        static::expectExceptionMessage('The password cannot be empty');

        $commandTester->setInputs(['', '', '']);
        $commandTester->execute([
            '--shopwareId' => 'no-reply@shopware.de',
            '--user' => 'missing_user',
        ]);
    }

    
public function testUpdate(): void
    {
        $updater = $this->createMock(IndexMappingUpdater::class);
        $updater
            ->expects(static::once())
            ->method('update');

        $command = new ElasticsearchUpdateMappingCommand(
            $updater
        );

        $tester = new CommandTester($command);
        $tester->execute([]);
    }
}
'testStrategy' => 'test Description',
                'secondStrategy' => 'second Description',
            ]);

        $urlChangeStrategy->expects(static::once())
            ->method('resolve')
            ->with(
                'testStrategy',
                static::isInstanceOf(Context::class)
            );

        $commandTester = new CommandTester(
            new ResolveAppUrlChangeCommand($urlChangeStrategy)
        );

        $commandTester->setInputs(['testStrategy']);
        $commandTester->execute([]);

        static::assertEquals(0, $commandTester->getStatusCode());

        static::assertStringContainsString('Choose what strategy should be applied, to resolve the app url change?:', $commandTester->getDisplay());
        static::assertStringContainsString('testStrategy', $commandTester->getDisplay());
        static::assertStringContainsString('secondStrategy', $commandTester->getDisplay());
        
use IntegrationTestBehaviour;

    private RefreshIndexCommand $refreshIndexCommand;

    protected function setUp(): void
    {
        $this->refreshIndexCommand = $this->getContainer()->get(RefreshIndexCommand::class);
    }

    public function testExecuteWithSkipIndexerOption(): void
    {
        $commandTester = new CommandTester($this->refreshIndexCommand);
        $commandTester->execute([]);

        $message = $commandTester->getDisplay();

        static::assertStringContainsString('sales_channel.indexer', $message);
        static::assertStringContainsString('category.indexer', $message);

        $commandTester = new CommandTester($this->refreshIndexCommand);
        $commandTester->execute(['--skip' => 'sales_channel.indexer,category.indexer']);

        $message = $commandTester->getDisplay();

        
/** * @internal */
#[Package('core')] class SalesChannelUpdateDomainCommandTest extends TestCase
{
    use IntegrationTestBehaviour;

    public function testUpdateDomainCommand(): void
    {
        $commandTester = new CommandTester($this->getContainer()->get(SalesChannelUpdateDomainCommand::class));
        $commandTester->execute(['domain' => 'test.de']);

        static::assertEquals(
            0,
            $commandTester->getStatusCode(),
            "\"bin/console sales-channel:maintenance:disable\" returned errors:\n" . $commandTester->getDisplay()
        );

        $domainRepo = $this->getContainer()->get('sales_channel_domain.repository');
        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('salesChannel.typeId', Defaults::SALES_CHANNEL_TYPE_STOREFRONT));

        
new Filesystem())->remove($directory);

        return true;
    }

    private function getCommandTester(): CommandTester
    {
        $themeCreateCommand = new ThemeCreateCommand(
            $this->projectDir
        );

        $commandTester = new CommandTester($themeCreateCommand);
        $application = new Application();
        $application->add($themeCreateCommand);

        return $commandTester;
    }
}
private Context $context;

    protected function setUp(): void
    {
        $this->userRepository = $this->getContainer()->get('user.repository');
        $this->context = Context::createDefaultContext();
    }

    public function testUnknownUser(): void
    {
        $commandTester = new CommandTester($this->getContainer()->get(UserChangePasswordCommand::class));
        $commandTester->execute([
            'username' => self::TEST_USERNAME,
            '--password' => self::TEST_PASSWORD,
        ]);

        $expected = 'The user "' . self::TEST_USERNAME . '" does not exist.';
        static::assertStringContainsString($expected$commandTester->getDisplay());
        static::assertEquals(1, $commandTester->getStatusCode());
    }

    public function testKnownUser(): void
    {
$this->arrayStorage = new IncrementArrayStorage([]);

        $command = new MigrateIncrementStorageCommand(
            new IncrementStorageRegistry(new \ArrayObject(
                [
                    'SQL' => $this->sqlStorage,
                    'Array' => $this->arrayStorage,
                ]
            ), 'SQL')
        );

        $this->tester = new CommandTester($command);
    }

    public function testMigrateWithConfirmation(): void
    {
        $this->sqlStorage->set(Uuid::randomHex(), 10);
        static::assertNotEmpty($this->sqlStorage->list());
        $before = $this->arrayStorage->list();
        static::assertEmpty($before);

        $this->tester->setInputs(['yes']);
        $this->tester->execute(['from' => 'SQL', 'to' => 'Array']);

        
protected function tearDown(): void
    {
        $fs = new Filesystem();
        $fs->remove($this->projectDir);
    }

    public function testSync(): void
    {
        $command = new SyncComposerVersionCommand($this->projectDir);

        $tester = new CommandTester($command);
        $tester->execute([]);

        $bundle1Json = json_decode((string) file_get_contents($this->projectDir . '/src/Bundle1/composer.json'), true, 512, \JSON_THROW_ON_ERROR);

        static::assertEquals('5.3.0', $bundle1Json['require']['symfony/symfony']);
        static::assertSame(Command::SUCCESS, $tester->getStatusCode());
    }

    public function testDryRun(): void
    {
        $command = new SyncComposerVersionCommand($this->projectDir);

        
$command = $this->getCommand();

        $this->expectException(\InvalidArgumentException::class);
        $command->run(new ArrayInput(['identifier' => [self::INTEGRATION_IDENTIFIER(), '_test_migrations_valid_run_time'], '--all' => true, '--limit' => 10])new BufferedOutput());
    }

    public function testCommandAddMigrations(): void
    {
        static::assertSame(0, $this->getMigrationCount());

        $tester = new CommandTester($this->getCommand());

        $tester->execute(['identifier' => [self::INTEGRATION_IDENTIFIER()], '--until' => \PHP_INT_MAX]);

        // assert no deprecation notice is shown         static::assertStringNotContainsString('v6.4.0', $tester->getDisplay());
        static::assertSame(2, $this->getMigrationCount());
    }

    public function testCommandMigrateMigrationException(): void
    {
        static::assertSame(0, $this->getMigrationCount(true));

        
$themeService = $this->createMock(ThemeService::class);
        $themeService->expects(static::exactly(\count($salesChannels)))
            ->method('assignTheme');

        $themeChangeCommand = new ThemeChangeCommand(
            $themeService,
            $this->pluginRegistry,
            $this->salesChannelRepository,
            $this->themeRepository
        );

        $commandTester = new CommandTester($themeChangeCommand);
        $application = new Application();
        $application->add($themeChangeCommand);

        $commandTester->execute([
            'theme-name' => $themes[0]['technicalName'],
            '--all' => true,
        ]);
    }

    public function testThemeChangeCommandWithOneSalesChannel(): void
    {
        

class UserCreateCommandTest extends TestCase
{
    use IntegrationTestBehaviour;

    private const TEST_USERNAME = 'shopware';

    public function testEmptyPasswordOption(): void
    {
        $commandTester = new CommandTester($this->getContainer()->get(UserCreateCommand::class));

        static::expectException(\RuntimeException::class);
        static::expectExceptionMessage('The password cannot be empty');

        $commandTester->setInputs(['', '', '']);

        $commandTester->execute([
            'username' => self::TEST_USERNAME,
        ]);
    }

    

        $service = $this->createMock(UnusedMediaPurger::class);
        $connection = $this->createMock(Connection::class);

        $connection->expects(static::once())
            ->method('fetchOne')
            ->with('SELECT JSON_OVERLAPS(JSON_ARRAY(1), JSON_ARRAY(1));')
            ->willThrowException(new \Exception('Not available'));

        $command = new DeleteNotUsedMediaCommand($service$connection);

        $commandTester = new CommandTester($command);
        $commandTester->execute([]);

        $output = new BufferedOutput();

        $io = new SymfonyStyle(
            new ArrayInput([]),
            $output,
        );

        $io->error('Your database does not support the JSON_OVERLAPS function. Please update your database to MySQL 8.0 or MariaDB 10.9 or higher.');

        
Home | Imprint | This part of the site doesn't use cookies.