|
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')); |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
$organization = new Organization(); |
|
70
|
|
|
$organization->setCode('123456'); |
|
71
|
|
|
$this->command->setContainer($this->getMockContainer(null, $organization)); |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
$organization = new Organization(); |
|
92
|
|
|
$organization->setCode('123456'); |
|
93
|
|
|
$this->command->setContainer($this->getMockContainer($organization)); |
|
|
|
|
|
|
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')); |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
176
|
|
|
{ |
|
177
|
|
|
$stream = fopen('php://memory', 'r+', false); |
|
178
|
|
|
fwrite($stream, $input); |
|
179
|
|
|
rewind($stream); |
|
180
|
|
|
|
|
181
|
|
|
return $stream; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: