1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Superdesk Web Publisher MultiTenancy Component. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. 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 2015 Sourcefabric z.ú. |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
namespace spec\SWP\Component\MultiTenancy\PathBuilder; |
15
|
|
|
|
16
|
|
|
use PhpSpec\ObjectBehavior; |
17
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
18
|
|
|
use SWP\Component\MultiTenancy\Model\Tenant; |
19
|
|
|
use SWP\Component\MultiTenancy\Resolver\TenantResolverInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
22
|
|
|
|
23
|
|
|
class TenantAwarePathBuilderSpec extends ObjectBehavior |
24
|
|
|
{ |
25
|
|
|
public function let(TenantContextInterface $tenantContext, TenantResolverInterface $tenantResolver) |
26
|
|
|
{ |
27
|
|
|
$currentTenant = new Tenant(); |
28
|
|
|
$currentTenant->setName('Default'); |
29
|
|
|
$currentTenant->setSubdomain('default'); |
30
|
|
|
$tenantContext->getTenant()->willReturn($currentTenant); |
31
|
|
|
|
32
|
|
|
$this->beConstructedWith($tenantResolver, $tenantContext, '/swp'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function it_is_initializable() |
36
|
|
|
{ |
37
|
|
|
$this->shouldHaveType('SWP\Component\MultiTenancy\PathBuilder\TenantAwarePathBuilder'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function it_implements_path_builder_interface() |
41
|
|
|
{ |
42
|
|
|
$this->shouldImplement('SWP\Component\MultiTenancy\PathBuilder\TenantAwarePathBuilderInterface'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function it_should_build_tenant_aware_path() |
46
|
|
|
{ |
47
|
|
|
$this->build('routes/articles')->shouldReturn('/swp/default/routes/articles'); |
48
|
|
|
$this->build('/routes/articles')->shouldReturn('/swp/default'); |
49
|
|
|
$this->build('routes')->shouldReturn('/swp/default/routes'); |
50
|
|
|
$this->build('/')->shouldReturn('/swp/default'); |
51
|
|
|
$this->build('routes', 'context')->shouldReturn('/swp/context/routes'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function it_should_resolve_tenant_from_request( |
|
|
|
|
55
|
|
|
$tenantContext, $tenantResolver, |
56
|
|
|
RequestStack $requestStack, |
57
|
|
|
Request $request |
58
|
|
|
) { |
59
|
|
|
$tenantContext->getTenant()->willReturn(new Tenant()); |
60
|
|
|
$requestStack->getCurrentRequest()->willReturn($request); |
61
|
|
|
$request->getHost()->shouldBeCalled()->willReturn('example.com'); |
62
|
|
|
|
63
|
|
|
$this->setRequestStack($requestStack); |
64
|
|
|
|
65
|
|
|
$currentTenant = new Tenant(); |
66
|
|
|
$currentTenant->setName('Default'); |
67
|
|
|
$currentTenant->setSubdomain('default'); |
68
|
|
|
$tenantResolver->resolve('example.com')->willReturn($currentTenant); |
69
|
|
|
$tenantContext->setTenant($currentTenant)->shouldBeCalled(); |
70
|
|
|
|
71
|
|
|
$this->build('/', 'articles')->shouldReturn('/swp/articles'); |
72
|
|
|
$this->build('/test', 'articles')->shouldReturn('/swp/articles'); |
73
|
|
|
$this->build('test', 'articles')->shouldReturn('/swp/articles/test'); |
74
|
|
|
$this->build('/')->shouldReturn('/swp/default'); |
75
|
|
|
$this->build('routes')->shouldReturn('/swp/default/routes'); |
76
|
|
|
$this->build('/routes')->shouldReturn('/swp/default'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
public function it_should_resolve_tenant_subdomain_from_request( |
|
|
|
|
80
|
|
|
$tenantContext, $tenantResolver, |
81
|
|
|
RequestStack $requestStack, |
82
|
|
|
Request $request |
83
|
|
|
) { |
84
|
|
|
$tenantContext->getTenant()->willReturn(new Tenant()); |
85
|
|
|
$requestStack->getCurrentRequest()->willReturn($request); |
86
|
|
|
$request->getHost()->shouldBeCalled()->willReturn('subdomain1.example.com'); |
87
|
|
|
|
88
|
|
|
$this->setRequestStack($requestStack); |
89
|
|
|
|
90
|
|
|
$currentTenant = new Tenant(); |
91
|
|
|
$currentTenant->setName('Default'); |
92
|
|
|
$currentTenant->setSubdomain('subdomain1'); |
93
|
|
|
$tenantResolver->resolve('subdomain1.example.com')->willReturn($currentTenant); |
94
|
|
|
$tenantContext->setTenant($currentTenant)->shouldBeCalled(); |
95
|
|
|
|
96
|
|
|
$this->build('/', 'articles')->shouldReturn('/swp/articles'); |
97
|
|
|
$this->build('/test', 'articles')->shouldReturn('/swp/articles'); |
98
|
|
|
$this->build('test', 'articles')->shouldReturn('/swp/articles/test'); |
99
|
|
|
$this->build('/')->shouldReturn('/swp/subdomain1'); |
100
|
|
|
$this->build('routes')->shouldReturn('/swp/subdomain1/routes'); |
101
|
|
|
$this->build('/routes')->shouldReturn('/swp/subdomain1'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function it_should_use_default_tenant($tenantContext, $tenantResolver, RequestStack $requestStack, Request $request) |
105
|
|
|
{ |
106
|
|
|
$tenantContext->getTenant()->willReturn(new Tenant()); |
107
|
|
|
$requestStack->getCurrentRequest()->willReturn($request); |
108
|
|
|
$request->getHost()->shouldBeCalled()->willReturn(null); |
109
|
|
|
|
110
|
|
|
$this->setRequestStack($requestStack); |
111
|
|
|
|
112
|
|
|
$currentTenant = new Tenant(); |
113
|
|
|
$currentTenant->setName('Default'); |
114
|
|
|
$currentTenant->setSubdomain('default'); |
115
|
|
|
$tenantResolver->resolve(null)->willReturn($currentTenant); |
116
|
|
|
$tenantContext->setTenant($currentTenant)->shouldBeCalled(); |
117
|
|
|
|
118
|
|
|
$this->build('/', 'articles')->shouldReturn('/swp/articles'); |
119
|
|
|
$this->build('/test', 'articles')->shouldReturn('/swp/articles'); |
120
|
|
|
$this->build('test', 'articles')->shouldReturn('/swp/articles/test'); |
121
|
|
|
$this->build('/')->shouldReturn('/swp/default'); |
122
|
|
|
$this->build('routes')->shouldReturn('/swp/default/routes'); |
123
|
|
|
$this->build('/routes')->shouldReturn('/swp/default'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
public function it_should_throw_an_exception_when_no_tenant_and_empty_path($tenantContext) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$tenantContext->getTenant()->willReturn(new Tenant()); |
129
|
|
|
|
130
|
|
|
$this->shouldThrow('PHPCR\RepositoryException') |
131
|
|
|
->duringBuild('', 'test'); |
132
|
|
|
|
133
|
|
|
$this->shouldThrow('PHPCR\RepositoryException') |
134
|
|
|
->duringBuild(''); |
135
|
|
|
|
136
|
|
|
$this->shouldThrow('PHPCR\RepositoryException') |
137
|
|
|
->duringBuild(null); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
View Code Duplication |
public function it_should_throw_exception_when_tenant_present_and_empty_path($tenantContext) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$tenantContext->getTenant()->willReturn(new Tenant()); |
143
|
|
|
|
144
|
|
|
$this->shouldThrow('PHPCR\RepositoryException') |
145
|
|
|
->duringBuild('', 'test'); |
146
|
|
|
|
147
|
|
|
$this->shouldThrow('PHPCR\RepositoryException') |
148
|
|
|
->duringBuild(''); |
149
|
|
|
|
150
|
|
|
$this->shouldThrow('PHPCR\RepositoryException') |
151
|
|
|
->duringBuild(null); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function it_should_not_throw_an_exception_when_tenant_present() |
155
|
|
|
{ |
156
|
|
|
$this->shouldNotThrow('PHPCR\RepositoryException') |
157
|
|
|
->duringBuild([]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function it_should_throw_an_exception_when_no_tenant_and_no_path_given($tenantContext) |
161
|
|
|
{ |
162
|
|
|
$tenantContext->getTenant()->willReturn(new Tenant()); |
163
|
|
|
|
164
|
|
|
$this->shouldThrow('PHPCR\RepositoryException') |
165
|
|
|
->duringBuild([]); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function it_should_build_multiple_tenant_aware_paths() |
169
|
|
|
{ |
170
|
|
|
$this->build(['routes', 'routes1'])->shouldReturn(['/swp/default/routes', '/swp/default/routes1']); |
171
|
|
|
$this->build(['routes'])->shouldReturn(['/swp/default/routes']); |
172
|
|
|
$this->build(['routes', 'routes1'], 'context') |
173
|
|
|
->shouldReturn(['/swp/context/routes', '/swp/context/routes1']); |
174
|
|
|
|
175
|
|
|
$this->build([], 'context') |
176
|
|
|
->shouldReturn([]); |
177
|
|
|
|
178
|
|
|
$this->build([]) |
179
|
|
|
->shouldReturn([]); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function it_should_test_path_context() |
183
|
|
|
{ |
184
|
|
|
$this->shouldThrow('PHPCR\RepositoryException') |
185
|
|
|
->duringBuild('/articles', ''); |
186
|
|
|
|
187
|
|
|
$this->shouldThrow('PHPCR\RepositoryException') |
188
|
|
|
->duringBuild('articles', ''); |
189
|
|
|
|
190
|
|
|
$this->build('articles', '@')->shouldReturn('/swp/@/articles'); |
191
|
|
|
$this->build('articles', null)->shouldReturn('/swp/default/articles'); |
192
|
|
|
$this->build('/articles', null)->shouldReturn('/swp/default'); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
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.