Completed
Push — master ( 68f942...d69058 )
by Rafał
18:18
created

testExecuteWhenCreatingDefaultTenantAndDefaultOrganizationDoesntExist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 15
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher MultiTenancy Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú.
12
 * @license http://www.superdesk.org/license
13
 */
14
namespace SWP\Bundle\MultiTenancyBundle\Tests\Command;
15
16
use SWP\Bundle\MultiTenancyBundle\Command\CreateTenantCommand;
17
use SWP\Component\MultiTenancy\Factory\TenantFactoryInterface;
18
use SWP\Component\MultiTenancy\Model\Organization;
19
use SWP\Component\MultiTenancy\Model\OrganizationInterface;
20
use SWP\Component\MultiTenancy\Model\Tenant;
21
use SWP\Component\MultiTenancy\Repository\OrganizationRepositoryInterface;
22
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
23
use Symfony\Component\Console\Application;
24
use Symfony\Component\Console\Tester\CommandTester;
25
use Symfony\Component\DependencyInjection\ContainerInterface;
26
27
class CreateTenantCommandTest extends \PHPUnit_Framework_TestCase
28
{
29
    const ORGANIZATION_CODE = '123456';
30
31
    private $commandTester;
32
    private $command;
33
    private $dialog;
34
35 View Code Duplication
    public function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37
        $application = new Application();
38
        $application->add(new CreateTenantCommand());
39
        $this->command = $application->get('swp:tenant:create');
40
        $this->dialog = $this->command->getHelper('dialog');
41
    }
42
43
    /**
44
     * @covers SWP\Bundle\MultiTenancyBundle\Command\CreateTenantCommand
45
     */
46 View Code Duplication
    public function testExecuteWhenCreatingNewTenant()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        $this->dialog->setInputStream($this->getInputStream("subdomain\nTest\n123456\n"));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method setInputStream() does only exist in the following implementations of said interface: Symfony\Component\Console\Helper\QuestionHelper, Symfony\Component\Consol...r\SymfonyQuestionHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
49
        $this->command->setContainer($this->getMockContainer(null, new Organization(), new Tenant(), 'subdomain'));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Command\Command as the method setContainer() does only exist in the following sub-classes of Symfony\Component\Console\Command\Command: Doctrine\Bundle\Doctrine...DatabaseDoctrineCommand, Doctrine\Bundle\Doctrine...Command\DoctrineCommand, Doctrine\Bundle\Doctrine...DatabaseDoctrineCommand, Doctrine\Bundle\Doctrine...EntitiesDoctrineCommand, Doctrine\Bundle\Doctrine...tMappingDoctrineCommand, Doctrine\Bundle\Doctrine...le\Command\CacheCommand, Doctrine\Bundle\Doctrine...Command\ContainsCommand, Doctrine\Bundle\Doctrine...e\Command\DeleteCommand, Doctrine\Bundle\Doctrine...le\Command\FlushCommand, Doctrine\Bundle\Doctrine...le\Command\StatsCommand, Doctrine\Bundle\PHPCRBun...mand\LoadFixtureCommand, Doctrine\Bundle\PHPCRBun...\MigratorMigrateCommand, Doctrine\Bundle\PHPCRBun...Command\NodeDumpCommand, Doctrine\Bundle\PHPCRBun...mmand\PhpcrShellCommand, Doctrine\Bundle\PHPCRBun...d\RepositoryInitCommand, SWP\Bundle\MultiTenancyB...eateOrganizationCommand, SWP\Bundle\MultiTenancyB...and\CreateTenantCommand, SWP\Bundle\MultiTenancyB...istOrganizationsCommand, Symfony\Bundle\Framework...d\AbstractConfigCommand, Symfony\Bundle\Framework...nd\AssetsInstallCommand, Symfony\Bundle\Framework...mmand\CacheClearCommand, Symfony\Bundle\Framework...mand\CacheWarmupCommand, Symfony\Bundle\Framework...mand\ConfigDebugCommand, Symfony\Bundle\Framework...figDumpReferenceCommand, Symfony\Bundle\Framework...d\ContainerAwareCommand, Symfony\Bundle\Framework...d\ContainerDebugCommand, Symfony\Bundle\Framework...tDispatcherDebugCommand, Symfony\Bundle\Framework...uterApacheDumperCommand, Symfony\Bundle\Framework...mand\RouterDebugCommand, Symfony\Bundle\Framework...mand\RouterMatchCommand, Symfony\Bundle\Framework...e\Command\ServerCommand, Symfony\Bundle\Framework...ommand\ServerRunCommand, Symfony\Bundle\Framework...mand\ServerStartCommand, Symfony\Bundle\Framework...and\ServerStatusCommand, Symfony\Bundle\Framework...mmand\ServerStopCommand, Symfony\Bundle\Framework...TranslationDebugCommand, Symfony\Bundle\Framework...ranslationUpdateCommand. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
50
        $this->commandTester = new CommandTester($this->command);
51
        $this->commandTester->execute(['command' => $this->command->getName()]);
52
53
        $this->assertRegExp(
54
            '/Please enter subdomain:Please enter name:Please enter organization:Tenant Test has been created and enabled!/',
55
            $this->commandTester->getDisplay()
56
        );
57
    }
58
59
    /**
60
     * @covers SWP\Bundle\MultiTenancyBundle\Command\CreateTenantCommand
61
     */
62
    public function testExecuteWhenCreatingDefaultTenant()
63
    {
64
        $this->command->setContainer($this->getMockContainer(null, new Organization(), new Tenant()));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Command\Command as the method setContainer() does only exist in the following sub-classes of Symfony\Component\Console\Command\Command: Doctrine\Bundle\Doctrine...DatabaseDoctrineCommand, Doctrine\Bundle\Doctrine...Command\DoctrineCommand, Doctrine\Bundle\Doctrine...DatabaseDoctrineCommand, Doctrine\Bundle\Doctrine...EntitiesDoctrineCommand, Doctrine\Bundle\Doctrine...tMappingDoctrineCommand, Doctrine\Bundle\Doctrine...le\Command\CacheCommand, Doctrine\Bundle\Doctrine...Command\ContainsCommand, Doctrine\Bundle\Doctrine...e\Command\DeleteCommand, Doctrine\Bundle\Doctrine...le\Command\FlushCommand, Doctrine\Bundle\Doctrine...le\Command\StatsCommand, Doctrine\Bundle\PHPCRBun...mand\LoadFixtureCommand, Doctrine\Bundle\PHPCRBun...\MigratorMigrateCommand, Doctrine\Bundle\PHPCRBun...Command\NodeDumpCommand, Doctrine\Bundle\PHPCRBun...mmand\PhpcrShellCommand, Doctrine\Bundle\PHPCRBun...d\RepositoryInitCommand, SWP\Bundle\MultiTenancyB...eateOrganizationCommand, SWP\Bundle\MultiTenancyB...and\CreateTenantCommand, SWP\Bundle\MultiTenancyB...istOrganizationsCommand, Symfony\Bundle\Framework...d\AbstractConfigCommand, Symfony\Bundle\Framework...nd\AssetsInstallCommand, Symfony\Bundle\Framework...mmand\CacheClearCommand, Symfony\Bundle\Framework...mand\CacheWarmupCommand, Symfony\Bundle\Framework...mand\ConfigDebugCommand, Symfony\Bundle\Framework...figDumpReferenceCommand, Symfony\Bundle\Framework...d\ContainerAwareCommand, Symfony\Bundle\Framework...d\ContainerDebugCommand, Symfony\Bundle\Framework...tDispatcherDebugCommand, Symfony\Bundle\Framework...uterApacheDumperCommand, Symfony\Bundle\Framework...mand\RouterDebugCommand, Symfony\Bundle\Framework...mand\RouterMatchCommand, Symfony\Bundle\Framework...e\Command\ServerCommand, Symfony\Bundle\Framework...ommand\ServerRunCommand, Symfony\Bundle\Framework...mand\ServerStartCommand, Symfony\Bundle\Framework...and\ServerStatusCommand, Symfony\Bundle\Framework...mmand\ServerStopCommand, Symfony\Bundle\Framework...TranslationDebugCommand, Symfony\Bundle\Framework...ranslationUpdateCommand. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
65
        $this->commandTester = new CommandTester($this->command);
66
67
        $this->commandTester->execute([
68
            'command' => $this->command->getName(),
69
            '--default' => true,
70
        ]);
71
72
        $this->assertRegExp(
73
            '/Tenant Default tenant has been created and enabled!/',
74
            $this->commandTester->getDisplay()
75
        );
76
    }
77
78
    /**
79
     * @covers SWP\Bundle\MultiTenancyBundle\Command\CreateTenantCommand
80
     * @expectedException \InvalidArgumentException
81
     */
82 View Code Duplication
    public function testExecuteWhenCreatingDefaultTenantAndDefaultOrganizationDoesntExist()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $this->command->setContainer($this->getMockContainer());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Command\Command as the method setContainer() does only exist in the following sub-classes of Symfony\Component\Console\Command\Command: Doctrine\Bundle\Doctrine...DatabaseDoctrineCommand, Doctrine\Bundle\Doctrine...Command\DoctrineCommand, Doctrine\Bundle\Doctrine...DatabaseDoctrineCommand, Doctrine\Bundle\Doctrine...EntitiesDoctrineCommand, Doctrine\Bundle\Doctrine...tMappingDoctrineCommand, Doctrine\Bundle\Doctrine...le\Command\CacheCommand, Doctrine\Bundle\Doctrine...Command\ContainsCommand, Doctrine\Bundle\Doctrine...e\Command\DeleteCommand, Doctrine\Bundle\Doctrine...le\Command\FlushCommand, Doctrine\Bundle\Doctrine...le\Command\StatsCommand, Doctrine\Bundle\PHPCRBun...mand\LoadFixtureCommand, Doctrine\Bundle\PHPCRBun...\MigratorMigrateCommand, Doctrine\Bundle\PHPCRBun...Command\NodeDumpCommand, Doctrine\Bundle\PHPCRBun...mmand\PhpcrShellCommand, Doctrine\Bundle\PHPCRBun...d\RepositoryInitCommand, SWP\Bundle\MultiTenancyB...eateOrganizationCommand, SWP\Bundle\MultiTenancyB...and\CreateTenantCommand, SWP\Bundle\MultiTenancyB...istOrganizationsCommand, Symfony\Bundle\Framework...d\AbstractConfigCommand, Symfony\Bundle\Framework...nd\AssetsInstallCommand, Symfony\Bundle\Framework...mmand\CacheClearCommand, Symfony\Bundle\Framework...mand\CacheWarmupCommand, Symfony\Bundle\Framework...mand\ConfigDebugCommand, Symfony\Bundle\Framework...figDumpReferenceCommand, Symfony\Bundle\Framework...d\ContainerAwareCommand, Symfony\Bundle\Framework...d\ContainerDebugCommand, Symfony\Bundle\Framework...tDispatcherDebugCommand, Symfony\Bundle\Framework...uterApacheDumperCommand, Symfony\Bundle\Framework...mand\RouterDebugCommand, Symfony\Bundle\Framework...mand\RouterMatchCommand, Symfony\Bundle\Framework...e\Command\ServerCommand, Symfony\Bundle\Framework...ommand\ServerRunCommand, Symfony\Bundle\Framework...mand\ServerStartCommand, Symfony\Bundle\Framework...and\ServerStatusCommand, Symfony\Bundle\Framework...mmand\ServerStopCommand, Symfony\Bundle\Framework...TranslationDebugCommand, Symfony\Bundle\Framework...ranslationUpdateCommand. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
85
        $this->commandTester = new CommandTester($this->command);
86
87
        $this->commandTester->execute([
88
            'command' => $this->command->getName(),
89
            '--default' => true,
90
        ]);
91
92
        $this->assertRegExp(
93
            '/Default organization doesn\'t exist!/',
94
            $this->commandTester->getDisplay()
95
        );
96
    }
97
98
    /**
99
     * @expectedException \InvalidArgumentException
100
     * @covers SWP\Bundle\MultiTenancyBundle\Command\CreateTenantCommand
101
     */
102 View Code Duplication
    public function testExecuteWhenDefaultTenantExists()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        $this->command->setContainer($this->getMockContainer(new Tenant()));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Command\Command as the method setContainer() does only exist in the following sub-classes of Symfony\Component\Console\Command\Command: Doctrine\Bundle\Doctrine...DatabaseDoctrineCommand, Doctrine\Bundle\Doctrine...Command\DoctrineCommand, Doctrine\Bundle\Doctrine...DatabaseDoctrineCommand, Doctrine\Bundle\Doctrine...EntitiesDoctrineCommand, Doctrine\Bundle\Doctrine...tMappingDoctrineCommand, Doctrine\Bundle\Doctrine...le\Command\CacheCommand, Doctrine\Bundle\Doctrine...Command\ContainsCommand, Doctrine\Bundle\Doctrine...e\Command\DeleteCommand, Doctrine\Bundle\Doctrine...le\Command\FlushCommand, Doctrine\Bundle\Doctrine...le\Command\StatsCommand, Doctrine\Bundle\PHPCRBun...mand\LoadFixtureCommand, Doctrine\Bundle\PHPCRBun...\MigratorMigrateCommand, Doctrine\Bundle\PHPCRBun...Command\NodeDumpCommand, Doctrine\Bundle\PHPCRBun...mmand\PhpcrShellCommand, Doctrine\Bundle\PHPCRBun...d\RepositoryInitCommand, SWP\Bundle\MultiTenancyB...eateOrganizationCommand, SWP\Bundle\MultiTenancyB...and\CreateTenantCommand, SWP\Bundle\MultiTenancyB...istOrganizationsCommand, Symfony\Bundle\Framework...d\AbstractConfigCommand, Symfony\Bundle\Framework...nd\AssetsInstallCommand, Symfony\Bundle\Framework...mmand\CacheClearCommand, Symfony\Bundle\Framework...mand\CacheWarmupCommand, Symfony\Bundle\Framework...mand\ConfigDebugCommand, Symfony\Bundle\Framework...figDumpReferenceCommand, Symfony\Bundle\Framework...d\ContainerAwareCommand, Symfony\Bundle\Framework...d\ContainerDebugCommand, Symfony\Bundle\Framework...tDispatcherDebugCommand, Symfony\Bundle\Framework...uterApacheDumperCommand, Symfony\Bundle\Framework...mand\RouterDebugCommand, Symfony\Bundle\Framework...mand\RouterMatchCommand, Symfony\Bundle\Framework...e\Command\ServerCommand, Symfony\Bundle\Framework...ommand\ServerRunCommand, Symfony\Bundle\Framework...mand\ServerStartCommand, Symfony\Bundle\Framework...and\ServerStatusCommand, Symfony\Bundle\Framework...mmand\ServerStopCommand, Symfony\Bundle\Framework...TranslationDebugCommand, Symfony\Bundle\Framework...ranslationUpdateCommand. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
105
        $this->commandTester = new CommandTester($this->command);
106
107
        $this->commandTester->execute([
108
            'command' => $this->command->getName(),
109
            '--default' => true,
110
        ]);
111
    }
112
113
    /**
114
     * @covers SWP\Bundle\MultiTenancyBundle\Command\CreateTenantCommand
115
     */
116 View Code Duplication
    public function testExecuteDisabledTenant()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118
        $this->dialog->setInputStream($this->getInputStream("example\nExample\n123456\n"));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method setInputStream() does only exist in the following implementations of said interface: Symfony\Component\Console\Helper\QuestionHelper, Symfony\Component\Consol...r\SymfonyQuestionHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
119
        $this->command->setContainer($this->getMockContainer(null, new Organization(), new Tenant(), 'example'));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Command\Command as the method setContainer() does only exist in the following sub-classes of Symfony\Component\Console\Command\Command: Doctrine\Bundle\Doctrine...DatabaseDoctrineCommand, Doctrine\Bundle\Doctrine...Command\DoctrineCommand, Doctrine\Bundle\Doctrine...DatabaseDoctrineCommand, Doctrine\Bundle\Doctrine...EntitiesDoctrineCommand, Doctrine\Bundle\Doctrine...tMappingDoctrineCommand, Doctrine\Bundle\Doctrine...le\Command\CacheCommand, Doctrine\Bundle\Doctrine...Command\ContainsCommand, Doctrine\Bundle\Doctrine...e\Command\DeleteCommand, Doctrine\Bundle\Doctrine...le\Command\FlushCommand, Doctrine\Bundle\Doctrine...le\Command\StatsCommand, Doctrine\Bundle\PHPCRBun...mand\LoadFixtureCommand, Doctrine\Bundle\PHPCRBun...\MigratorMigrateCommand, Doctrine\Bundle\PHPCRBun...Command\NodeDumpCommand, Doctrine\Bundle\PHPCRBun...mmand\PhpcrShellCommand, Doctrine\Bundle\PHPCRBun...d\RepositoryInitCommand, SWP\Bundle\MultiTenancyB...eateOrganizationCommand, SWP\Bundle\MultiTenancyB...and\CreateTenantCommand, SWP\Bundle\MultiTenancyB...istOrganizationsCommand, Symfony\Bundle\Framework...d\AbstractConfigCommand, Symfony\Bundle\Framework...nd\AssetsInstallCommand, Symfony\Bundle\Framework...mmand\CacheClearCommand, Symfony\Bundle\Framework...mand\CacheWarmupCommand, Symfony\Bundle\Framework...mand\ConfigDebugCommand, Symfony\Bundle\Framework...figDumpReferenceCommand, Symfony\Bundle\Framework...d\ContainerAwareCommand, Symfony\Bundle\Framework...d\ContainerDebugCommand, Symfony\Bundle\Framework...tDispatcherDebugCommand, Symfony\Bundle\Framework...uterApacheDumperCommand, Symfony\Bundle\Framework...mand\RouterDebugCommand, Symfony\Bundle\Framework...mand\RouterMatchCommand, Symfony\Bundle\Framework...e\Command\ServerCommand, Symfony\Bundle\Framework...ommand\ServerRunCommand, Symfony\Bundle\Framework...mand\ServerStartCommand, Symfony\Bundle\Framework...and\ServerStatusCommand, Symfony\Bundle\Framework...mmand\ServerStopCommand, Symfony\Bundle\Framework...TranslationDebugCommand, Symfony\Bundle\Framework...ranslationUpdateCommand. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
120
        $this->commandTester = new CommandTester($this->command);
121
        $this->commandTester->execute([
122
            'command' => $this->command->getName(),
123
            '--disabled' => true,
124
        ]);
125
126
        $this->assertRegExp(
127
            '/Please enter subdomain:Please enter name:Please enter organization:Tenant Example has been created and disabled!/',
128
            $this->commandTester->getDisplay()
129
        );
130
    }
131
132
    private function getMockContainer($mockTenant = null, $mockOrganization = null, $mockedTenantInFactory = null, $subdomain = 'default')
133
    {
134
        $mockRepoOrganization = $this->getMockBuilder(OrganizationRepositoryInterface::class)
135
            ->getMock();
136
137
        $mockRepoOrganization->expects($this->any())
138
            ->method('findOneByCode')
139
            ->with(self::ORGANIZATION_CODE)
140
            ->willReturn($mockOrganization);
141
142
        $mockRepoOrganization->expects($this->any())
143
            ->method('findOneByName')
144
            ->with(OrganizationInterface::DEFAULT_NAME)
145
            ->willReturn($mockOrganization);
146
147
        $mockRepo = $this->getMockBuilder(TenantRepositoryInterface::class)
148
            ->getMock();
149
150
        $mockRepo->expects($this->any())
151
            ->method('findOneBySubdomain')
152
            ->with($subdomain)
153
            ->willReturn($mockTenant);
154
155
        $mockDoctrine = $this
156
            ->getMockBuilder('Doctrine\ORM\EntityManager')
157
            ->disableOriginalConstructor()
158
            ->getMock();
159
160
        $mockDoctrine->expects($this->any())
161
            ->method('persist')
162
            ->will($this->returnValue(null));
163
        $mockDoctrine->expects($this->any())
164
            ->method('flush')
165
            ->will($this->returnValue(null));
166
167
        $mockContainer = $this->getMockBuilder(ContainerInterface::class)
168
            ->getMock();
169
170
        $mockFactory = $this->getMockBuilder(TenantFactoryInterface::class)
171
            ->getMock();
172
173
        $mockFactory->expects($this->any())
174
            ->method('create')
175
            ->willReturn($mockedTenantInFactory);
176
177
        $mockContainer->expects($this->any())
178
            ->method('get')
179
            ->will($this->returnValueMap([
180
                ['swp.object_manager.tenant', 1, $mockDoctrine],
181
                ['swp.repository.tenant', 1, $mockRepo],
182
                ['swp.repository.organization', 1, $mockRepoOrganization],
183
                ['swp.factory.tenant', 1, $mockFactory],
184
            ]));
185
186
        return $mockContainer;
187
    }
188
189
    /**
190
     * @param $input
191
     *
192
     * @return resource
193
     */
194 View Code Duplication
    protected function getInputStream($input)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
195
    {
196
        $stream = fopen('php://memory', 'r+', false);
197
        fwrite($stream, $input);
198
        rewind($stream);
199
200
        return $stream;
201
    }
202
}
203