Completed
Push — EZP-31460 ( fb49b1...efa331 )
by
unknown
19:23 queued 11s
created

testRendererControllerReferenceWithCompoundMatcher()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 50
rs 9.0909
c 0
b 0
f 0
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\Publish\Core\MVC\Symfony\Component\Serializer\SerializerTrait;
10
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
11
use PHPUnit\Framework\TestCase;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Controller\ControllerReference;
14
use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
15
16
abstract class FragmentRendererBaseTest extends TestCase
17
{
18
    use SerializerTrait;
19
20
    public function testRendererControllerReferenceWithCompoundMatcher(): ControllerReference
21
    {
22
        $reference = new ControllerReference('FooBundle:bar:baz');
23
        $compoundMatcher = new SiteAccess\Matcher\Compound\LogicalAnd([]);
24
        $subMatchers = [
25
            'Map\URI' => new SiteAccess\Matcher\Map\URI([]),
26
            'Map\Host' => new SiteAccess\Matcher\Map\Host([]),
27
        ];
28
        $compoundMatcher->setSubMatchers($subMatchers);
29
        $siteAccess = new SiteAccess(
30
            'test',
31
            'test',
32
            $compoundMatcher
33
        );
34
35
        $request = $this->getRequest($siteAccess);
36
        $options = ['foo' => 'bar'];
37
        $expectedReturn = '/_fragment?foo=bar';
38
        $this->innerRenderer
0 ignored issues
show
Bug introduced by
The property innerRenderer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
            ->expects($this->once())
40
            ->method('render')
41
            ->with($reference, $request, $options)
42
            ->will($this->returnValue($expectedReturn));
43
44
        $renderer = $this->getRenderer();
45
        $this->assertSame($expectedReturn, $renderer->render($reference, $request, $options));
46
        $this->assertArrayHasKey('serialized_siteaccess', $reference->attributes);
47
        $serializedSiteAccess = json_encode($siteAccess);
48
        $this->assertSame($serializedSiteAccess, $reference->attributes['serialized_siteaccess']);
49
        $this->assertArrayHasKey('serialized_siteaccess_matcher', $reference->attributes);
50
        $this->assertSame(
51
            $this->getSerializer()->serialize(
52
                $siteAccess->matcher,
53
                'json'
54
            ),
55
            $reference->attributes['serialized_siteaccess_matcher']
56
        );
57
        $this->assertArrayHasKey('serialized_siteaccess_sub_matchers', $reference->attributes);
58
        foreach ($siteAccess->matcher->getSubMatchers() as $subMatcher) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher as the method getSubMatchers() does only exist in the following implementations of said interface: eZ\Publish\Core\MVC\Symf...Access\Matcher\Compound, eZ\Publish\Core\MVC\Symf...her\Compound\LogicalAnd, eZ\Publish\Core\MVC\Symf...cher\Compound\LogicalOr.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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 implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
59
            $this->assertSame(
60
                $this->getSerializer()->serialize(
61
                    $subMatcher,
62
                    'json'
63
                ),
64
                $reference->attributes['serialized_siteaccess_sub_matchers'][get_class($subMatcher)]
65
            );
66
        }
67
68
        return $reference;
69
    }
70
71
    abstract public function getRequest(SiteAccess $siteAccess): Request;
72
73
    abstract public function getRenderer(): FragmentRendererInterface;
74
}
75