|
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() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$this->dialog->setInputStream($this->getInputStream("subdomain\nTest\n123456\n")); |
|
|
|
|
|
|
49
|
|
|
$this->command->setContainer($this->getMockContainer(null, new Organization(), new Tenant(), 'subdomain')); |
|
|
|
|
|
|
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())); |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
$this->command->setContainer($this->getMockContainer()); |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
103
|
|
|
{ |
|
104
|
|
|
$this->command->setContainer($this->getMockContainer(new Tenant())); |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
117
|
|
|
{ |
|
118
|
|
|
$this->dialog->setInputStream($this->getInputStream("example\nExample\n123456\n")); |
|
|
|
|
|
|
119
|
|
|
$this->command->setContainer($this->getMockContainer(null, new Organization(), new Tenant(), 'example')); |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
195
|
|
|
{ |
|
196
|
|
|
$stream = fopen('php://memory', 'r+', false); |
|
197
|
|
|
fwrite($stream, $input); |
|
198
|
|
|
rewind($stream); |
|
199
|
|
|
|
|
200
|
|
|
return $stream; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
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.