testExecuteWhenCreatingDefaultTenant()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
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
15
namespace SWP\Bundle\MultiTenancyBundle\Tests\Command;
16
17
use PHPUnit\Framework\TestCase;
18
use SWP\Bundle\MultiTenancyBundle\Command\CreateTenantCommand;
19
use SWP\Component\MultiTenancy\Factory\TenantFactoryInterface;
20
use SWP\Component\MultiTenancy\Model\Organization;
21
use SWP\Component\MultiTenancy\Model\OrganizationInterface;
22
use SWP\Component\MultiTenancy\Model\Tenant;
23
use SWP\Component\MultiTenancy\Repository\OrganizationRepositoryInterface;
24
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
25
use Symfony\Component\Console\Application;
26
use Symfony\Component\Console\Tester\CommandTester;
27
use Symfony\Component\DependencyInjection\ContainerInterface;
28
29
class CreateTenantCommandTest extends TestCase
30
{
31
    const ORGANIZATION_CODE = '123456';
32
33
    private $commandTester;
34
35
    private $command;
36
37
    private $question;
38
39
    private function setupCommand($container)
40
    {
41
        $application = new Application();
42
        $application->add(new CreateTenantCommand(
43
            $container->getParameter('swp_tenant'),
44
            $container->get('swp.factory.tenant'),
45
            $container->get('swp.object_manager.tenant'),
46
            $container->get('swp.repository.tenant'),
47
            $container->get('swp.repository.organization')
48
        ));
49
        $this->command = $application->get('swp:tenant:create');
50
        $this->question = $this->command->getHelper('question');
51
    }
52
53
    /**
54
     * @covers \SWP\Bundle\MultiTenancyBundle\Command\CreateTenantCommand
55
     */
56 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...
57
    {
58
        $tenant = new Tenant();
59
        $tenant->setCode('123abc');
60
61
        $container = $this->getMockContainer(null, new Organization(), $tenant, 'subdomain', 'domain.dev');
62
        $this->setupCommand($container);
63
64
        $this->commandTester = new CommandTester($this->command);
65
        $this->commandTester->setInputs(['domain.dev', null, 'Test', '123456']);
66
        $this->commandTester->execute(['command' => $this->command->getName()]);
67
68
        $this->assertContains(
69
            'Please enter domain:Please enter subdomain:Please enter name:Please enter organization code:Tenant Test (code: 123abc) has been created and enabled!',
70
            $this->commandTester->getDisplay()
71
        );
72
    }
73
74
    /**
75
     * @covers \SWP\Bundle\MultiTenancyBundle\Command\CreateTenantCommand
76
     */
77 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...
78
    {
79
        $tenant = new Tenant();
80
        $tenant->setCode('123abc');
81
        $container = $this->getMockContainer(null, new Organization(), $tenant);
82
        $this->setupCommand($container);
83
        $this->commandTester = new CommandTester($this->command);
84
85
        $this->commandTester->execute([
86
            'command' => $this->command->getName(),
87
            '--default' => true,
88
        ]);
89
90
        $this->assertContains(
91
            'Tenant Default tenant (code: 123abc) has been created and enabled!',
92
            $this->commandTester->getDisplay()
93
        );
94
    }
95
96
    /**
97
     * @covers \SWP\Bundle\MultiTenancyBundle\Command\CreateTenantCommand
98
     * @expectedException \InvalidArgumentException
99
     */
100 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...
101
    {
102
        $container = $this->getMockContainer();
103
        $this->setupCommand($container);
104
        $this->commandTester = new CommandTester($this->command);
105
106
        $this->commandTester->execute([
107
            'command' => $this->command->getName(),
108
            '--default' => true,
109
        ]);
110
111
        $this->assertRegExp(
112
            '/Default organization doesn\'t exist!/',
113
            $this->commandTester->getDisplay()
114
        );
115
    }
116
117
    /**
118
     * @expectedException \InvalidArgumentException
119
     * @covers \SWP\Bundle\MultiTenancyBundle\Command\CreateTenantCommand
120
     */
121
    public function testExecuteWhenDefaultTenantExists()
122
    {
123
        $container = $this->getMockContainer(new Tenant());
124
        $this->setupCommand($container);
125
126
        $this->commandTester = new CommandTester($this->command);
127
128
        $this->commandTester->execute([
129
            'command' => $this->command->getName(),
130
            '--default' => true,
131
        ]);
132
    }
133
134
    /**
135
     * @covers \SWP\Bundle\MultiTenancyBundle\Command\CreateTenantCommand
136
     */
137 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...
138
    {
139
        $tenant = new Tenant();
140
        $tenant->setCode('123abc');
141
        $container = $this->getMockContainer(null, new Organization(), $tenant, 'example', 'example.com');
142
        $this->setupCommand($container);
143
144
        $this->commandTester = new CommandTester($this->command);
145
        $this->commandTester->setInputs(['example.com', null, 'Example', '123456']);
146
        $this->commandTester->execute([
147
            'command' => $this->command->getName(),
148
            '--disabled' => true,
149
        ]);
150
151
        $this->assertContains(
152
            'Please enter domain:Please enter subdomain:Please enter name:Please enter organization code:Tenant Example (code: 123abc) has been created and disabled!',
153
            $this->commandTester->getDisplay()
154
        );
155
    }
156
157
    private function getMockContainer($mockTenant = null, $mockOrganization = null, $mockedTenantInFactory = null, $subdomain = 'default', $domain = 'localhost')
158
    {
159
        $mockRepoOrganization = $this->getMockBuilder(OrganizationRepositoryInterface::class)
160
            ->getMock();
161
162
        $mockRepoOrganization->expects($this->any())
163
            ->method('findOneByCode')
164
            ->with(self::ORGANIZATION_CODE)
165
            ->willReturn($mockOrganization);
166
167
        $mockRepoOrganization->expects($this->any())
168
            ->method('findOneByName')
169
            ->with(OrganizationInterface::DEFAULT_NAME)
170
            ->willReturn($mockOrganization);
171
172
        $mockRepo = $this->getMockBuilder(TenantRepositoryInterface::class)
173
            ->getMock();
174
175
        $mockRepo->expects($this->any())
176
            ->method('findOneBySubdomainAndDomain')
177
            ->with($subdomain, $domain)
178
            ->willReturn($mockTenant);
179
180
        $mockRepo->expects($this->any())
181
            ->method('findOneByDomain')
182
            ->with($domain)
183
            ->willReturn($mockTenant);
184
185
        $mockDoctrine = $this
186
            ->getMockBuilder('Doctrine\ORM\EntityManager')
187
            ->disableOriginalConstructor()
188
            ->getMock();
189
190
        $mockDoctrine->expects($this->any())
191
            ->method('persist')
192
            ->will($this->returnValue(null));
193
        $mockDoctrine->expects($this->any())
194
            ->method('flush')
195
            ->will($this->returnValue(null));
196
197
        $mockContainer = $this->getMockBuilder(ContainerInterface::class)
198
            ->getMock();
199
200
        $mockFactory = $this->getMockBuilder(TenantFactoryInterface::class)
201
            ->getMock();
202
203
        $mockFactory->expects($this->any())
204
            ->method('create')
205
            ->willReturn($mockedTenantInFactory);
206
207
        $mockContainer->expects($this->any())
208
            ->method('get')
209
            ->will(self::returnValueMap([
210
                ['swp.object_manager.tenant', 1, $mockDoctrine],
211
                ['swp.repository.tenant', 1, $mockRepo],
212
                ['swp.repository.organization', 1, $mockRepoOrganization],
213
                ['swp.factory.tenant', 1, $mockFactory],
214
            ]));
215
216
        $mockContainer->expects($this->any())
217
            ->method('getParameter')
218
            ->willReturn('localhost');
219
220
        return $mockContainer;
221
    }
222
223
    /**
224
     * @param $input
225
     *
226
     * @return resource
227
     */
228 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...
229
    {
230
        $stream = fopen('php://memory', 'r+', false);
231
        fwrite($stream, $input);
232
        rewind($stream);
233
234
        return $stream;
235
    }
236
}
237