Completed
Pull Request — master (#1)
by Rafał
02:55
created

CreateTenantCommandTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 142
Duplicated Lines 38.73 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 6
dl 55
loc 142
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testExecuteWhenCreatingNewTenant() 12 12 1
A testExecuteWhenCreatingDefaultTenant() 15 15 1
A testExecuteWhenDefaultTenantExists() 13 13 1
A testExecuteDisabledTenant() 15 15 1
B getMockContainer() 0 35 1
A getInputStream() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher MultiTenancy Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.u. 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\MultiTenancyBundle\Tests\Command;
15
16
use SWP\MultiTenancyBundle\Command\CreateTenantCommand;
17
use Symfony\Component\Console\Application;
18
use Symfony\Component\Console\Tester\CommandTester;
19
20
class CreateTenantCommandTest extends \PHPUnit_Framework_TestCase
21
{
22
    private $commandTester;
23
    private $command;
24
    private $dialog;
25
26
    public function setUp()
27
    {
28
        $application = new Application();
29
        $application->add(new CreateTenantCommand());
30
        $this->command = $application->get('swp:tenant:create');
31
        $this->dialog = $this->command->getHelper('dialog');
32
    }
33
34
    /**
35
     * @covers SWP\MultiTenancyBundle\Command\CreateTenantCommand
36
     */
37 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...
38
    {
39
        $this->dialog->setInputStream($this->getInputStream("subdomain\nTest\n"));
40
        $this->command->setContainer($this->getMockContainer(null, '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, SWP\MultiTenancyBundle\Command\CreateTenantCommand, 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...
41
        $this->commandTester = new CommandTester($this->command);
42
        $this->commandTester->execute(array('command' => $this->command->getName()));
43
44
        $this->assertRegExp(
45
            '/Please enter subdomain:Please enter name:Tenant Test has been created and enabled!/',
46
            $this->commandTester->getDisplay()
47
        );
48
    }
49
50
    /**
51
     * @covers SWP\MultiTenancyBundle\Command\CreateTenantCommand
52
     */
53 View Code Duplication
    public function testExecuteWhenCreatingDefaultTenant()
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...
54
    {
55
        $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, SWP\MultiTenancyBundle\Command\CreateTenantCommand, 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...
56
        $this->commandTester = new CommandTester($this->command);
57
58
        $this->commandTester->execute(array(
59
            'command' => $this->command->getName(),
60
            '--default' => true,
61
        ));
62
63
        $this->assertRegExp(
64
            '/Tenant Default tenant has been created and enabled!/',
65
            $this->commandTester->getDisplay()
66
        );
67
    }
68
69
    /**
70
     * @expectedException \InvalidArgumentException
71
     * @covers SWP\MultiTenancyBundle\Command\CreateTenantCommand
72
     */
73 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...
74
    {
75
        $mockTenant = $this->getMockBuilder('SWP\Component\MultiTenancy\Model\TenantInterface')
76
            ->getMock();
77
78
        $this->command->setContainer($this->getMockContainer($mockTenant));
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, SWP\MultiTenancyBundle\Command\CreateTenantCommand, 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...
79
        $this->commandTester = new CommandTester($this->command);
80
81
        $this->commandTester->execute(array(
82
            'command' => $this->command->getName(),
83
            '--default' => true,
84
        ));
85
    }
86
87
    /**
88
     * @covers SWP\MultiTenancyBundle\Command\CreateTenantCommand
89
     */
90 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...
91
    {
92
        $this->dialog->setInputStream($this->getInputStream("example\nExample\n"));
93
        $this->command->setContainer($this->getMockContainer(null, '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, SWP\MultiTenancyBundle\Command\CreateTenantCommand, 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...
94
        $this->commandTester = new CommandTester($this->command);
95
        $this->commandTester->execute(array(
96
            'command' => $this->command->getName(),
97
            '--disabled' => true,
98
        ));
99
100
        $this->assertRegExp(
101
            '/Please enter subdomain:Please enter name:Tenant Example has been created and disabled!/',
102
            $this->commandTester->getDisplay()
103
        );
104
    }
105
106
    /**
107
     * @param null   $mockTenant
108
     * @param string $subdomain
109
     *
110
     * @return \PHPUnit_Framework_MockObject_MockObject
111
     */
112
    private function getMockContainer($mockTenant = null, $subdomain = 'default')
113
    {
114
        $mockRepo = $this->getMockBuilder('SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface')
115
            ->getMock();
116
117
        $mockRepo->expects($this->any())
118
            ->method('findBySubdomain')
119
            ->with($subdomain)
120
            ->willReturn($mockTenant)
121
        ;
122
123
        $mockDoctrine = $this
124
            ->getMockBuilder('Doctrine\ORM\EntityManager')
125
            ->disableOriginalConstructor()
126
            ->getMock();
127
128
        $mockDoctrine->expects($this->any())
129
            ->method('persist')
130
            ->will($this->returnValue(null));
131
        $mockDoctrine->expects($this->any())
132
            ->method('flush')
133
            ->will($this->returnValue(null));
134
135
        $mockContainer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container')
136
            ->getMock();
137
138
        $mockContainer->expects($this->any())
139
            ->method('get')
140
            ->will($this->returnValueMap([
141
                ['doctrine.orm.entity_manager', 1, $mockDoctrine],
142
                ['swp_multi_tenancy.tenant_repository', 1, $mockRepo],
143
            ]));
144
145
        return $mockContainer;
146
    }
147
148
    /**
149
     * @param $input
150
     *
151
     * @return resource
152
     */
153
    protected function getInputStream($input)
154
    {
155
        $stream = fopen('php://memory', 'r+', false);
156
        fputs($stream, $input);
157
        rewind($stream);
158
159
        return $stream;
160
    }
161
}
162