Completed
Push — master ( 28c8cd...7e44c6 )
by André
18:53
created

CompoundAndTest::testReverseMatchNotVersatile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 36
nc 1
nop 0
dl 0
loc 49
rs 9.2258
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the CompoundAndTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\MVC\Symfony\SiteAccess\Tests\Compound;
10
11
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
12
use eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilderInterface;
13
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\Compound\LogicalAnd;
14
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\Compound;
15
use eZ\Publish\Core\MVC\Symfony\SiteAccess\VersatileMatcher;
16
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher;
17
use eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest;
18
use eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilder;
19
use PHPUnit\Framework\TestCase;
20
21
class CompoundAndTest extends TestCase
22
{
23
    /**
24
     * @var \PHPUnit_Framework_MockObject_MockObject
25
     */
26
    private $matcherBuilder;
27
28
    protected function setUp()
29
    {
30
        parent::setUp();
31
        $this->matcherBuilder = $this->createMock(MatcherBuilderInterface::class);
32
    }
33
34
    /**
35
     * @return \eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\Compound\LogicalAnd
36
     */
37
    public function testConstruct()
38
    {
39
        return $this->buildMatcher();
40
    }
41
42
    /**
43
     * @return \eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\Compound\LogicalAnd
44
     */
45
    private function buildMatcher()
46
    {
47
        return new LogicalAnd(
48
            array(
49
                array(
50
                    'matchers' => array(
51
                        'Map\\URI' => array('eng' => true),
52
                        'Map\\Host' => array('fr.ezpublish.dev' => true),
53
                    ),
54
                    'match' => 'fr_eng',
55
                ),
56
                array(
57
                    'matchers' => array(
58
                        'Map\\URI' => array('fre' => true),
59
                        'Map\\Host' => array('us.ezpublish.dev' => true),
60
                    ),
61
                    'match' => 'fr_us',
62
                ),
63
                array(
64
                    'matchers' => array(
65
                        'Map\\URI' => array('de' => true),
66
                        'Map\\Host' => array('jp.ezpublish.dev' => true),
67
                    ),
68
                    'match' => 'de_jp',
69
                ),
70
            )
71
        );
72
    }
73
74
    /**
75
     * @depends testConstruct
76
     */
77 View Code Duplication
    public function testSetMatcherBuilder(Compound $compoundMatcher)
78
    {
79
        $this
80
            ->matcherBuilder
81
            ->expects($this->any())
82
            ->method('buildMatcher')
83
            ->will($this->returnValue($this->createMock(Matcher::class)));
84
85
        $compoundMatcher->setRequest($this->createMock(SimplifiedRequest::class));
86
        $compoundMatcher->setMatcherBuilder($this->matcherBuilder);
87
        $matchers = $compoundMatcher->getSubMatchers();
88
        $this->assertInternalType('array', $matchers);
89
        foreach ($matchers as $matcher) {
90
            $this->assertInstanceOf(Matcher::class, $matcher);
91
        }
92
    }
93
94
    /**
95
     * @dataProvider matchProvider
96
     *
97
     * @param \eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest $request
98
     * @param $expectedMatch
99
     */
100 View Code Duplication
    public function testMatch(SimplifiedRequest $request, $expectedMatch)
101
    {
102
        $compoundMatcher = $this->buildMatcher();
103
        $compoundMatcher->setRequest($request);
104
        $compoundMatcher->setMatcherBuilder(new MatcherBuilder());
105
        $this->assertSame($expectedMatch, $compoundMatcher->match());
106
    }
107
108
    public function testSetRequest()
109
    {
110
        $compoundMatcher = new LogicalAnd(
111
            array(
112
                array(
113
                    'matchers' => array(
114
                        'Map\\URI' => array('eng' => true),
115
                        'Map\\Host' => array('fr.ezpublish.dev' => true),
116
                    ),
117
                    'match' => 'fr_eng',
118
                ),
119
            )
120
        );
121
122
        $matcher1 = $this->createMock(Matcher::class);
123
        $matcher2 = $this->createMock(Matcher::class);
124
        $this->matcherBuilder
125
            ->expects($this->exactly(2))
126
            ->method('buildMatcher')
127
            ->will($this->onConsecutiveCalls($matcher1, $matcher2));
128
129
        $request = $this->createMock(SimplifiedRequest::class);
130
        $matcher1
131
            ->expects($this->once())
132
            ->method('setRequest')
133
            ->with($request);
134
        $matcher2
135
            ->expects($this->once())
136
            ->method('setRequest')
137
            ->with($request);
138
139
        $compoundMatcher->setRequest($this->createMock(SimplifiedRequest::class));
140
        $compoundMatcher->setMatcherBuilder($this->matcherBuilder);
141
        $compoundMatcher->setRequest($request);
142
    }
143
144
    public function matchProvider()
145
    {
146
        return array(
147
            array(SimplifiedRequest::fromUrl('http://fr.ezpublish.dev/eng'), 'fr_eng'),
148
            array(SimplifiedRequest::fromUrl('http://ezpublish.dev/eng'), false),
149
            array(SimplifiedRequest::fromUrl('http://fr.ezpublish.dev/fre'), false),
150
            array(SimplifiedRequest::fromUrl('http://fr.ezpublish.dev/'), false),
151
            array(SimplifiedRequest::fromUrl('http://us.ezpublish.dev/eng'), false),
152
            array(SimplifiedRequest::fromUrl('http://us.ezpublish.dev/fre'), 'fr_us'),
153
            array(SimplifiedRequest::fromUrl('http://ezpublish.dev/fr'), false),
154
            array(SimplifiedRequest::fromUrl('http://jp.ezpublish.dev/de'), 'de_jp'),
155
        );
156
    }
157
158 View Code Duplication
    public function testReverseMatchSiteAccessNotConfigured()
159
    {
160
        $compoundMatcher = $this->buildMatcher();
161
        $this->matcherBuilder
162
            ->expects($this->any())
163
            ->method('buildMatcher')
164
            ->will($this->returnValue($this->createMock(VersatileMatcher::class)));
165
166
        $compoundMatcher->setRequest($this->createMock(SimplifiedRequest::class));
167
        $compoundMatcher->setMatcherBuilder($this->matcherBuilder);
168
        $this->assertNull($compoundMatcher->reverseMatch('not_configured_sa'));
169
    }
170
171
    public function testReverseMatchNotVersatile()
172
    {
173
        $request = $this->createMock(SimplifiedRequest::class);
174
        $siteAccessName = 'fr_eng';
175
        $mapUriConfig = array('eng' => true);
176
        $mapHostConfig = array('fr.ezpublish.dev' => true);
177
        $compoundMatcher = new LogicalAnd(
178
            array(
179
                array(
180
                    'matchers' => array(
181
                        'Map\URI' => $mapUriConfig,
182
                        'Map\Host' => $mapHostConfig,
183
                    ),
184
                    'match' => $siteAccessName,
185
                ),
186
            )
187
        );
188
        $compoundMatcher->setRequest($request);
189
190
        $matcher1 = $this->createMock(VersatileMatcher::class);
191
        $matcher2 = $this->getMockBuilder(Matcher::class)
192
            ->disableOriginalConstructor()
193
            ->setMethods(['reverseMatch'])
194
            ->getMockForAbstractClass();
195
196
        $this->matcherBuilder
197
            ->expects($this->exactly(2))
198
            ->method('buildMatcher')
199
            ->will(
200
                $this->returnValueMap(
201
                    array(
202
                        array('Map\URI', $mapUriConfig, $request, $matcher1),
203
                        array('Map\Host', $mapHostConfig, $request, $matcher2),
204
                    )
205
                )
206
            );
207
208
        $matcher1
209
            ->expects($this->once())
210
            ->method('reverseMatch')
211
            ->with($siteAccessName)
212
            ->will($this->returnValue($this->createMock(VersatileMatcher::class)));
213
        $matcher2
214
            ->expects($this->never())
215
            ->method('reverseMatch');
216
217
        $compoundMatcher->setMatcherBuilder($this->matcherBuilder);
218
        $this->assertNull($compoundMatcher->reverseMatch($siteAccessName));
219
    }
220
221 View Code Duplication
    public function testReverseMatchFail()
222
    {
223
        $request = $this->createMock(SimplifiedRequest::class);
224
        $siteAccessName = 'fr_eng';
225
        $mapUriConfig = array('eng' => true);
226
        $mapHostConfig = array('fr.ezpublish.dev' => true);
227
        $compoundMatcher = new LogicalAnd(
228
            array(
229
                array(
230
                    'matchers' => array(
231
                        'Map\URI' => $mapUriConfig,
232
                        'Map\Host' => $mapHostConfig,
233
                    ),
234
                    'match' => $siteAccessName,
235
                ),
236
            )
237
        );
238
        $compoundMatcher->setRequest($request);
239
240
        $matcher1 = $this->createMock(VersatileMatcher::class);
241
        $matcher2 = $this->createMock(VersatileMatcher::class);
242
        $this->matcherBuilder
243
            ->expects($this->exactly(2))
244
            ->method('buildMatcher')
245
            ->will(
246
                $this->returnValueMap(
247
                    array(
248
                        array('Map\URI', $mapUriConfig, $request, $matcher1),
249
                        array('Map\Host', $mapHostConfig, $request, $matcher2),
250
                    )
251
                )
252
            );
253
254
        $matcher1
255
            ->expects($this->once())
256
            ->method('reverseMatch')
257
            ->with($siteAccessName)
258
            ->will($this->returnValue($this->createMock(VersatileMatcher::class)));
259
        $matcher2
260
            ->expects($this->once())
261
            ->method('reverseMatch')
262
            ->with($siteAccessName)
263
            ->will($this->returnValue(null));
264
265
        $compoundMatcher->setMatcherBuilder($this->matcherBuilder);
266
        $this->assertNull($compoundMatcher->reverseMatch($siteAccessName));
267
    }
268
269 View Code Duplication
    public function testReverseMatch()
270
    {
271
        $request = $this->createMock(SimplifiedRequest::class);
272
        $siteAccessName = 'fr_eng';
273
        $mapUriConfig = array('eng' => true);
274
        $mapHostConfig = array('fr.ezpublish.dev' => true);
275
        $compoundMatcher = new LogicalAnd(
276
            array(
277
                array(
278
                    'matchers' => array(
279
                        'Map\URI' => $mapUriConfig,
280
                        'Map\Host' => $mapHostConfig,
281
                    ),
282
                    'match' => $siteAccessName,
283
                ),
284
            )
285
        );
286
        $compoundMatcher->setRequest($request);
287
288
        $matcher1 = $this->createMock(VersatileMatcher::class);
289
        $matcher2 = $this->createMock(VersatileMatcher::class);
290
        $this->matcherBuilder
291
            ->expects($this->exactly(2))
292
            ->method('buildMatcher')
293
            ->will(
294
                $this->returnValueMap(
295
                    array(
296
                        array('Map\URI', $mapUriConfig, $request, $matcher1),
297
                        array('Map\Host', $mapHostConfig, $request, $matcher2),
298
                    )
299
                )
300
            );
301
302
        $reverseMatchedMatcher1 = $this->createMock(VersatileMatcher::class);
303
        $matcher1
304
            ->expects($this->once())
305
            ->method('reverseMatch')
306
            ->with($siteAccessName)
307
            ->will($this->returnValue($reverseMatchedMatcher1));
308
        $reverseMatchedMatcher2 = $this->createMock(VersatileMatcher::class);
309
        $matcher2
310
            ->expects($this->once())
311
            ->method('reverseMatch')
312
            ->with($siteAccessName)
313
            ->will($this->returnValue($reverseMatchedMatcher2));
314
315
        $compoundMatcher->setMatcherBuilder($this->matcherBuilder);
316
        $result = $compoundMatcher->reverseMatch($siteAccessName);
317
        $this->assertInstanceOf(LogicalAnd::class, $result);
318
        foreach ($result->getSubMatchers() as $subMatcher) {
319
            $this->assertInstanceOf(VersatileMatcher::class, $subMatcher);
320
        }
321
    }
322
323 View Code Duplication
    public function testSerialize()
324
    {
325
        $matcher = new LogicalAnd(array());
326
        $matcher->setRequest(new SimplifiedRequest(array('pathinfo' => '/foo/bar')));
327
        $sa = new SiteAccess('test', 'test', $matcher);
328
        $serializedSA1 = serialize($sa);
329
330
        $matcher->setRequest(new SimplifiedRequest(array('pathinfo' => '/foo/bar/baz')));
331
        $serializedSA2 = serialize($sa);
332
333
        $this->assertSame($serializedSA1, $serializedSA2);
334
    }
335
}
336