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