|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace eZ\Bundle\EzPublishCoreBundle\Tests\Fragment; |
|
8
|
|
|
|
|
9
|
|
|
use eZ\Bundle\EzPublishCoreBundle\Fragment\InlineFragmentRenderer; |
|
10
|
|
|
use eZ\Publish\Core\MVC\Symfony\SiteAccess; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerReference; |
|
13
|
|
|
|
|
14
|
|
|
class InlineFragmentRendererTest extends DecoratedFragmentRendererTest |
|
15
|
|
|
{ |
|
16
|
|
|
public function testRendererControllerReference() |
|
17
|
|
|
{ |
|
18
|
|
|
$reference = new ControllerReference('FooBundle:bar:baz'); |
|
19
|
|
|
$matcher = new SiteAccess\Matcher\HostElement(1); |
|
20
|
|
|
$siteAccess = new SiteAccess( |
|
21
|
|
|
'test', |
|
22
|
|
|
'test', |
|
23
|
|
|
$matcher |
|
24
|
|
|
); |
|
25
|
|
|
$request = new Request(); |
|
26
|
|
|
$request->attributes->set('siteaccess', $siteAccess); |
|
27
|
|
|
$request->attributes->set('semanticPathinfo', '/foo/bar'); |
|
28
|
|
|
$request->attributes->set('viewParametersString', '/(foo)/bar'); |
|
29
|
|
|
$options = ['foo' => 'bar']; |
|
30
|
|
|
$expectedReturn = '/_fragment?foo=bar'; |
|
31
|
|
|
$this->innerRenderer |
|
32
|
|
|
->expects($this->once()) |
|
33
|
|
|
->method('render') |
|
34
|
|
|
->with($reference, $request, $options) |
|
35
|
|
|
->will($this->returnValue($expectedReturn)); |
|
36
|
|
|
|
|
37
|
|
|
$renderer = new InlineFragmentRenderer($this->innerRenderer); |
|
38
|
|
|
$this->assertSame($expectedReturn, $renderer->render($reference, $request, $options)); |
|
39
|
|
|
$this->assertTrue(isset($reference->attributes['serialized_siteaccess'])); |
|
40
|
|
|
$serializedSiteAccess = json_encode($siteAccess); |
|
41
|
|
|
$this->assertSame($serializedSiteAccess, $reference->attributes['serialized_siteaccess']); |
|
42
|
|
|
$this->assertTrue(isset($reference->attributes['serialized_siteaccess_matcher'])); |
|
43
|
|
|
$this->assertSame( |
|
44
|
|
|
$this->getSerializer()->serialize( |
|
45
|
|
|
$siteAccess->matcher, |
|
46
|
|
|
'json' |
|
47
|
|
|
), |
|
48
|
|
|
$reference->attributes['serialized_siteaccess_matcher'] |
|
49
|
|
|
); |
|
50
|
|
|
$this->assertTrue(isset($reference->attributes['semanticPathinfo'])); |
|
51
|
|
|
$this->assertSame('/foo/bar', $reference->attributes['semanticPathinfo']); |
|
52
|
|
|
$this->assertTrue(isset($reference->attributes['viewParametersString'])); |
|
53
|
|
|
$this->assertSame('/(foo)/bar', $reference->attributes['viewParametersString']); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testRendererControllerReferenceWithCompoundMatcher() |
|
57
|
|
|
{ |
|
58
|
|
|
$reference = parent::testRendererControllerReferenceWithCompoundMatcher(); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertArrayHasKey('semanticPathinfo', $reference->attributes); |
|
61
|
|
|
$this->assertSame('/foo/bar', $reference->attributes['semanticPathinfo']); |
|
62
|
|
|
$this->assertArrayHasKey('viewParametersString', $reference->attributes); |
|
63
|
|
|
$this->assertSame('/(foo)/bar', $reference->attributes['viewParametersString']); |
|
64
|
|
|
|
|
65
|
|
|
return $reference; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getRequest(SiteAccess $siteAccess) |
|
69
|
|
|
{ |
|
70
|
|
|
$request = new Request(); |
|
71
|
|
|
$request->attributes->set('siteaccess', $siteAccess); |
|
72
|
|
|
$request->attributes->set('semanticPathinfo', '/foo/bar'); |
|
73
|
|
|
$request->attributes->set('viewParametersString', '/(foo)/bar'); |
|
74
|
|
|
|
|
75
|
|
|
return $request; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getRenderer() |
|
79
|
|
|
{ |
|
80
|
|
|
return new InlineFragmentRenderer($this->innerRenderer); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.