ListTetantsCommandTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 178
Duplicated Lines 12.92 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 8
dl 23
loc 178
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setupCommand() 0 6 1
A testExecuteWhenNoTenants() 15 15 1
A testListWithTenants() 0 30 1
B testListWithTenantsAndOrganizationOption() 0 62 1
A getMockContainer() 0 29 1
A getInputStream() 8 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.ú. 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\ListTenantsCommand;
19
use SWP\Component\MultiTenancy\Model\Organization;
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 ListTetantsCommandTest extends TestCase
28
{
29
    const ORGANIZATION_CODE = '123456';
30
31
    private $commandTester;
32
33
    private $command;
34
35
    private function setupCommand($container)
36
    {
37
        $application = new Application();
38
        $application->add(new ListTenantsCommand($container->get('swp.repository.organization'), $container->get('swp.repository.tenant')));
39
        $this->command = $application->get('swp:tenant:list');
40
    }
41
42
    /**
43
     * @covers \SWP\Bundle\MultiTenancyBundle\Command\ListTenantsCommand
44
     */
45 View Code Duplication
    public function testExecuteWhenNoTenants()
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...
46
    {
47
        $tenant = new Tenant();
48
        $tenant->setCode('123abc');
49
        $container = $this->getMockContainer(new Organization());
50
        $this->setupCommand($container);
51
52
        $this->commandTester = new CommandTester($this->command);
53
        $this->commandTester->execute(['command' => $this->command->getName()]);
54
55
        $this->assertContains(
56
            'There are no tenants defined.',
57
            trim($this->commandTester->getDisplay())
58
        );
59
    }
60
61
    /**
62
     * @covers \SWP\Bundle\MultiTenancyBundle\Command\ListTenantsCommand
63
     */
64
    public function testListWithTenants()
65
    {
66
        $tenant = new Tenant();
67
        $tenant->setCode('123abc');
68
        $tenant->setId('1');
69
        $tenant->setName('Test Tenant');
70
        $tenant->setCreatedAt(new \DateTime('2017-02-20 15:19:55'));
71
        $organization = new Organization();
72
        $organization->setCode('123456');
73
        $organization->setName('Test Organization');
74
        $organization->addTenant($tenant);
75
        $tenant->setOrganization($organization);
76
77
        $container = $this->getMockContainer($organization);
78
        $this->setupCommand($container);
79
80
        $this->commandTester = new CommandTester($this->command);
81
        $this->commandTester->execute(['command' => $this->command->getName()]);
82
83
        $result = <<<'EOF'
84
List of all available tenants:
85
+----+--------+-------------+--------+-----------+------------+------------+---------------------+----------------------------------+
86
| Id | Code   | Name        | Domain | Subdomain | Is active? | Theme Name | Created at          | Organization                     |
87
+----+--------+-------------+--------+-----------+------------+------------+---------------------+----------------------------------+
88
| 1  | 123abc | Test Tenant |        |           | yes        |            | 2017-02-20 15:19:55 | Test Organization (code: 123456) |
89
+----+--------+-------------+--------+-----------+------------+------------+---------------------+----------------------------------+
90
EOF;
91
92
        $this->assertEquals($result, trim($this->commandTester->getDisplay()));
93
    }
94
95
    /**
96
     * @covers \SWP\Bundle\MultiTenancyBundle\Command\CreateTenantCommand
97
     */
98
    public function testListWithTenantsAndOrganizationOption()
99
    {
100
        $tenant = new Tenant();
101
        $tenant->setCode('123abc');
102
        $tenant->setId('1');
103
        $tenant->setName('Test Tenant');
104
        $tenant->setCreatedAt(new \DateTime('2017-02-20 15:19:55'));
105
        $organization = new Organization();
106
        $organization->setCode('123456');
107
        $organization->setName('Test Organization');
108
        $organization->addTenant($tenant);
109
        $tenant->setOrganization($organization);
110
111
        $container = $this->getMockContainer($organization);
112
        $this->setupCommand($container);
113
114
        $this->commandTester = new CommandTester($this->command);
115
        $this->commandTester->execute(['command' => $this->command->getName()]);
116
117
        $result = <<<'EOF'
118
List of all available tenants:
119
+----+--------+-------------+--------+-----------+------------+------------+---------------------+----------------------------------+
120
| Id | Code   | Name        | Domain | Subdomain | Is active? | Theme Name | Created at          | Organization                     |
121
+----+--------+-------------+--------+-----------+------------+------------+---------------------+----------------------------------+
122
| 1  | 123abc | Test Tenant |        |           | yes        |            | 2017-02-20 15:19:55 | Test Organization (code: 123456) |
123
+----+--------+-------------+--------+-----------+------------+------------+---------------------+----------------------------------+
124
EOF;
125
126
        $this->assertEquals($result, trim($this->commandTester->getDisplay()));
127
128
        $tenant2 = new Tenant();
129
        $tenant2->setCode('345def');
130
        $tenant2->setId('2');
131
        $tenant2->setName('Test Tenant 2');
132
        $tenant2->setCreatedAt(new \DateTime('2017-02-20 15:19:55'));
133
        $organization2 = new Organization();
134
        $organization2->setCode('789012');
135
        $organization2->setName('Test Organization2');
136
        $organization2->addTenant($tenant2);
137
        $tenant2->setOrganization($organization2);
138
139
        $container = $this->getMockContainer($organization2);
140
        $this->setupCommand($container);
141
142
        $this->commandTester = new CommandTester($this->command);
143
        $this->commandTester->execute(['command' => $this->command->getName(), '-o' => '123456']);
144
        $result = <<<'EOF'
145
There are no tenants defined.
146
EOF;
147
        $this->assertEquals($result, trim($this->commandTester->getDisplay()));
148
149
        $this->commandTester->execute(['command' => $this->command->getName()]);
150
        $result = <<<'EOF'
151
List of all available tenants:
152
+----+--------+---------------+--------+-----------+------------+------------+---------------------+-----------------------------------+
153
| Id | Code   | Name          | Domain | Subdomain | Is active? | Theme Name | Created at          | Organization                      |
154
+----+--------+---------------+--------+-----------+------------+------------+---------------------+-----------------------------------+
155
| 2  | 345def | Test Tenant 2 |        |           | yes        |            | 2017-02-20 15:19:55 | Test Organization2 (code: 789012) |
156
+----+--------+---------------+--------+-----------+------------+------------+---------------------+-----------------------------------+
157
EOF;
158
        $this->assertEquals($result, trim($this->commandTester->getDisplay()));
159
    }
160
161
    private function getMockContainer($mockOrganization = null)
162
    {
163
        $mockRepoOrganization = $this->getMockBuilder(OrganizationRepositoryInterface::class)
164
            ->getMock();
165
166
        $mockRepoOrganization->expects($this->any())
167
            ->method('findOneByCode')
168
            ->with(self::ORGANIZATION_CODE)
169
            ->willReturn($mockOrganization);
170
171
        $mockRepo = $this->getMockBuilder(TenantRepositoryInterface::class)
172
            ->getMock();
173
174
        $mockRepo->expects($this->any())
175
            ->method('findAll')
176
            ->willReturn($mockOrganization->getTenants());
177
178
        $mockContainer = $this->getMockBuilder(ContainerInterface::class)
179
            ->getMock();
180
181
        $mockContainer->expects($this->any())
182
            ->method('get')
183
            ->will($this->returnValueMap([
184
                ['swp.repository.tenant', 1, $mockRepo],
185
                ['swp.repository.organization', 1, $mockRepoOrganization],
186
            ]));
187
188
        return $mockContainer;
189
    }
190
191
    /**
192
     * @param $input
193
     *
194
     * @return resource
195
     */
196 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...
197
    {
198
        $stream = fopen('php://memory', 'r+', false);
199
        fwrite($stream, $input);
200
        rewind($stream);
201
202
        return $stream;
203
    }
204
}
205