|
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\CreateOrganizationCommand; |
|
17
|
|
|
use SWP\Component\MultiTenancy\Factory\OrganizationFactoryInterface; |
|
18
|
|
|
use SWP\Component\MultiTenancy\Model\Organization; |
|
19
|
|
|
use SWP\Component\MultiTenancy\Model\OrganizationInterface; |
|
20
|
|
|
use SWP\Component\MultiTenancy\Repository\OrganizationRepositoryInterface; |
|
21
|
|
|
use Symfony\Component\Console\Application; |
|
22
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
23
|
|
|
|
|
24
|
|
|
class CreateOrganizationCommandTest extends \PHPUnit_Framework_TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
private $commandTester; |
|
27
|
|
|
private $command; |
|
28
|
|
|
private $dialog; |
|
29
|
|
|
|
|
30
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
$application = new Application(); |
|
33
|
|
|
$application->add(new CreateOrganizationCommand()); |
|
34
|
|
|
$this->command = $application->get('swp:organization:create'); |
|
35
|
|
|
$this->dialog = $this->command->getHelper('dialog'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @covers SWP\Bundle\MultiTenancyBundle\Command\CreateOrganizationCommand |
|
40
|
|
|
*/ |
|
41
|
|
View Code Duplication |
public function testExecuteWhenCreatingNewOrganization() |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
$this->dialog->setInputStream($this->getInputStream("Test\n")); |
|
44
|
|
|
$this->command->setContainer($this->getMockContainer(null, new Organization(), 'Test')); |
|
|
|
|
|
|
45
|
|
|
$this->commandTester = new CommandTester($this->command); |
|
46
|
|
|
$this->commandTester->execute(['command' => $this->command->getName()]); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertRegExp( |
|
49
|
|
|
'/Please enter name:Organization Test has been created and enabled!/', |
|
50
|
|
|
$this->commandTester->getDisplay() |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @covers SWP\Bundle\MultiTenancyBundle\Command\CreateOrganizationCommand |
|
56
|
|
|
*/ |
|
57
|
|
View Code Duplication |
public function testExecuteWhenCreatingDefaultOrganization() |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
$this->command->setContainer($this->getMockContainer(null, new Organization())); |
|
|
|
|
|
|
60
|
|
|
$this->commandTester = new CommandTester($this->command); |
|
61
|
|
|
|
|
62
|
|
|
$this->commandTester->execute([ |
|
63
|
|
|
'command' => $this->command->getName(), |
|
64
|
|
|
'--default' => true, |
|
65
|
|
|
]); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertRegExp( |
|
68
|
|
|
'/Organization default has been created and enabled!/', |
|
69
|
|
|
$this->commandTester->getDisplay() |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @expectedException \InvalidArgumentException |
|
75
|
|
|
* @covers SWP\Bundle\MultiTenancyBundle\Command\CreateOrganizationCommand |
|
76
|
|
|
*/ |
|
77
|
|
View Code Duplication |
public function testExecuteWhenDefaultTenantExists() |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
$this->command->setContainer($this->getMockContainer(new Organization())); |
|
|
|
|
|
|
80
|
|
|
$this->commandTester = new CommandTester($this->command); |
|
81
|
|
|
|
|
82
|
|
|
$this->commandTester->execute([ |
|
83
|
|
|
'command' => $this->command->getName(), |
|
84
|
|
|
'--default' => true, |
|
85
|
|
|
]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @covers SWP\Bundle\MultiTenancyBundle\Command\CreateOrganizationCommand |
|
90
|
|
|
*/ |
|
91
|
|
View Code Duplication |
public function testExecuteDisabledOrganization() |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$this->dialog->setInputStream($this->getInputStream("Example\n")); |
|
94
|
|
|
$this->command->setContainer($this->getMockContainer(null, new Organization(), 'Example')); |
|
|
|
|
|
|
95
|
|
|
$this->commandTester = new CommandTester($this->command); |
|
96
|
|
|
$this->commandTester->execute([ |
|
97
|
|
|
'command' => $this->command->getName(), |
|
98
|
|
|
'--disabled' => true, |
|
99
|
|
|
]); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertRegExp( |
|
102
|
|
|
'/Please enter name:Organization Example has been created and disabled!/', |
|
103
|
|
|
$this->commandTester->getDisplay() |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
private function getMockContainer($mockOrganization = null, $mockedOrganizationInFactory = null, $name = OrganizationInterface::DEFAULT_NAME) |
|
108
|
|
|
{ |
|
109
|
|
|
$mockRepo = $this->getMockBuilder(OrganizationRepositoryInterface::class) |
|
110
|
|
|
->getMock(); |
|
111
|
|
|
|
|
112
|
|
|
$mockRepo->expects($this->any()) |
|
113
|
|
|
->method('findOneByName') |
|
114
|
|
|
->with($name) |
|
115
|
|
|
->willReturn($mockOrganization); |
|
116
|
|
|
|
|
117
|
|
|
$mockDoctrine = $this |
|
118
|
|
|
->getMockBuilder('Doctrine\ORM\EntityManager') |
|
119
|
|
|
->disableOriginalConstructor() |
|
120
|
|
|
->getMock(); |
|
121
|
|
|
|
|
122
|
|
|
$mockDoctrine->expects($this->any()) |
|
123
|
|
|
->method('persist') |
|
124
|
|
|
->will($this->returnValue(null)); |
|
125
|
|
|
$mockDoctrine->expects($this->any()) |
|
126
|
|
|
->method('flush') |
|
127
|
|
|
->will($this->returnValue(null)); |
|
128
|
|
|
|
|
129
|
|
|
$mockFactory = $this->getMockBuilder(OrganizationFactoryInterface::class) |
|
130
|
|
|
->getMock(); |
|
131
|
|
|
|
|
132
|
|
|
$mockFactory->expects($this->any()) |
|
133
|
|
|
->method('createWithCode') |
|
134
|
|
|
->willReturn($mockedOrganizationInFactory); |
|
135
|
|
|
|
|
136
|
|
|
$mockContainer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container') |
|
137
|
|
|
->getMock(); |
|
138
|
|
|
|
|
139
|
|
|
$mockContainer->expects($this->any()) |
|
140
|
|
|
->method('get') |
|
141
|
|
|
->will($this->returnValueMap([ |
|
142
|
|
|
['swp.object_manager.organization', 1, $mockDoctrine], |
|
143
|
|
|
['swp.repository.organization', 1, $mockRepo], |
|
144
|
|
|
['swp.factory.organization', 1, $mockFactory], |
|
145
|
|
|
])); |
|
146
|
|
|
|
|
147
|
|
|
return $mockContainer; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param $input |
|
152
|
|
|
* |
|
153
|
|
|
* @return resource |
|
154
|
|
|
*/ |
|
155
|
|
View Code Duplication |
protected function getInputStream($input) |
|
|
|
|
|
|
156
|
|
|
{ |
|
157
|
|
|
$stream = fopen('php://memory', 'r+', false); |
|
158
|
|
|
fwrite($stream, $input); |
|
159
|
|
|
rewind($stream); |
|
160
|
|
|
|
|
161
|
|
|
return $stream; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
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.