CreateOrganizationCommandTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 157
Duplicated Lines 23.57 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 37
loc 157
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testExecuteWhenCreatingNewOrganization() 0 14 1
A testExecuteWhenCreatingDefaultOrganization() 17 17 1
A getMockContainer() 0 46 1
A getInputStream() 8 8 1
A setUp() 0 7 1
A testExecuteWhenDefaultTenantExists() 12 12 1
A testExecuteDisabledOrganization() 0 17 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.ú. 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
15
namespace SWP\Bundle\MultiTenancyBundle\Tests\Command;
16
17
use PHPUnit\Framework\TestCase;
18
use SWP\Bundle\MultiTenancyBundle\Command\CreateOrganizationCommand;
19
use SWP\Component\MultiTenancy\Factory\OrganizationFactoryInterface;
20
use SWP\Component\MultiTenancy\Model\Organization;
21
use SWP\Component\MultiTenancy\Model\OrganizationInterface;
22
use SWP\Component\MultiTenancy\Repository\OrganizationRepositoryInterface;
23
use Symfony\Component\Console\Application;
24
use Symfony\Component\Console\Helper\QuestionHelper;
25
use Symfony\Component\Console\Tester\CommandTester;
26
27
class CreateOrganizationCommandTest extends TestCase
28
{
29
    private $commandTester;
30
31
    private $command;
32
33
    /**
34
     * @var QuestionHelper
35
     */
36
    private $question;
37
38
    public function setUp()
39
    {
40
        $application = new Application();
41
        $application->add(new CreateOrganizationCommand());
42
        $this->command = $application->get('swp:organization:create');
43
        $this->question = $this->command->getHelper('question');
44
    }
45
46
    /**
47
     * @covers \SWP\Bundle\MultiTenancyBundle\Command\CreateOrganizationCommand
48
     */
49
    public function testExecuteWhenCreatingNewOrganization()
50
    {
51
        $organization = new Organization();
52
        $organization->setCode('123456');
53
        $this->command->setContainer($this->getMockContainer(null, $organization, 'Test'));
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\Bundle\MultiTenancyB...eateOrganizationCommand, SWP\Bundle\MultiTenancyB...istOrganizationsCommand, Symfony\Bundle\Framework...d\ContainerAwareCommand. 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...
54
        $this->commandTester = new CommandTester($this->command);
55
        $this->commandTester->setInputs(['Test']);
56
        $this->commandTester->execute(['command' => $this->command->getName()]);
57
58
        $this->assertEquals(
59
            'Please enter name:Organization Test (code: 123456) has been created and enabled!',
60
            trim($this->commandTester->getDisplay())
61
        );
62
    }
63
64
    /**
65
     * @covers \SWP\Bundle\MultiTenancyBundle\Command\CreateOrganizationCommand
66
     */
67 View Code Duplication
    public function testExecuteWhenCreatingDefaultOrganization()
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...
68
    {
69
        $organization = new Organization();
70
        $organization->setCode('123456');
71
        $this->command->setContainer($this->getMockContainer(null, $organization));
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\Bundle\MultiTenancyB...eateOrganizationCommand, SWP\Bundle\MultiTenancyB...istOrganizationsCommand, Symfony\Bundle\Framework...d\ContainerAwareCommand. 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...
72
        $this->commandTester = new CommandTester($this->command);
73
74
        $this->commandTester->execute([
75
            'command' => $this->command->getName(),
76
            '--default' => true,
77
        ]);
78
79
        $this->assertEquals(
80
            'Organization default (code: 123456) has been created and enabled!',
81
            trim($this->commandTester->getDisplay())
82
        );
83
    }
84
85
    /**
86
     * @expectedException \InvalidArgumentException
87
     * @covers \SWP\Bundle\MultiTenancyBundle\Command\CreateOrganizationCommand
88
     */
89 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...
90
    {
91
        $organization = new Organization();
92
        $organization->setCode('123456');
93
        $this->command->setContainer($this->getMockContainer($organization));
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\Bundle\MultiTenancyB...eateOrganizationCommand, SWP\Bundle\MultiTenancyB...istOrganizationsCommand, Symfony\Bundle\Framework...d\ContainerAwareCommand. 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
96
        $this->commandTester->execute([
97
            'command' => $this->command->getName(),
98
            '--default' => true,
99
        ]);
100
    }
101
102
    /**
103
     * @covers \SWP\Bundle\MultiTenancyBundle\Command\CreateOrganizationCommand
104
     */
105
    public function testExecuteDisabledOrganization()
106
    {
107
        $organization = new Organization();
108
        $organization->setCode('123456');
109
        $this->command->setContainer($this->getMockContainer(null, $organization, '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\Bundle\MultiTenancyB...eateOrganizationCommand, SWP\Bundle\MultiTenancyB...istOrganizationsCommand, Symfony\Bundle\Framework...d\ContainerAwareCommand. 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...
110
        $this->commandTester = new CommandTester($this->command);
111
        $this->commandTester->setInputs(['Example']);
112
        $this->commandTester->execute([
113
            'command' => $this->command->getName(),
114
            '--disabled' => true,
115
        ]);
116
117
        $this->assertEquals(
118
            'Please enter name:Organization Example (code: 123456) has been created and disabled!',
119
            trim($this->commandTester->getDisplay())
120
        );
121
    }
122
123
    private function getMockContainer($mockOrganization = null, $mockedOrganizationInFactory = null, $name = OrganizationInterface::DEFAULT_NAME)
124
    {
125
        $mockRepo = $this->getMockBuilder(OrganizationRepositoryInterface::class)
126
            ->getMock();
127
128
        $mockRepo->expects($this->any())
129
            ->method('findOneByName')
130
            ->with($name)
131
            ->willReturn($mockOrganization);
132
133
        $mockDoctrine = $this
134
            ->getMockBuilder('Doctrine\ORM\EntityManager')
135
            ->disableOriginalConstructor()
136
            ->getMock();
137
138
        $mockDoctrine->expects($this->any())
139
            ->method('persist')
140
            ->will($this->returnValue(null));
141
        $mockDoctrine->expects($this->any())
142
            ->method('flush')
143
            ->will($this->returnValue(null));
144
145
        $mockFactory = $this->getMockBuilder(OrganizationFactoryInterface::class)
146
            ->getMock();
147
148
        $mockFactory->expects($this->any())
149
            ->method('createWithCode')
150
            ->willReturn($mockedOrganizationInFactory);
151
152
        $mockFactory->expects($this->any())
153
            ->method('create')
154
            ->willReturn(new Organization());
155
156
        $mockContainer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container')
157
            ->getMock();
158
159
        $mockContainer->expects($this->any())
160
            ->method('get')
161
            ->will($this->returnValueMap([
162
                ['swp.object_manager.organization', 1, $mockDoctrine],
163
                ['swp.repository.organization', 1, $mockRepo],
164
                ['swp.factory.organization', 1, $mockFactory],
165
            ]));
166
167
        return $mockContainer;
168
    }
169
170
    /**
171
     * @param $input
172
     *
173
     * @return resource
174
     */
175 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...
176
    {
177
        $stream = fopen('php://memory', 'r+', false);
178
        fwrite($stream, $input);
179
        rewind($stream);
180
181
        return $stream;
182
    }
183
}
184