Completed
Push — clear_configuration ( 24171b...ff4737 )
by Maximilian
12:38
created

testPrependDefaultRepositoryName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrinePHPCRAdminBundle\Tests\DependencyInjection;
13
14
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
15
use Sonata\DoctrinePHPCRAdminBundle\DependencyInjection\SonataDoctrinePHPCRAdminExtension;
16
use Symfony\Cmf\Bundle\ResourceBundle\DependencyInjection\CmfResourceExtension;
17
18
class SonataDoctrinePHPCRAdminExtensionTest extends AbstractExtensionTestCase
19
{
20
    public function getContainerExtensions()
21
    {
22
        return array(
23
           new SonataDoctrinePHPCRAdminExtension(),
24
        );
25
    }
26
27
    public function testDocumentTreeDefaultValues()
28
    {
29
        $this->container->setParameter(
30
            'kernel.bundles',
31
            array()
32
        );
33
        $this->load(array('document_tree' => array()));
34
35
        $this->assertContainerBuilderHasParameter(
36
            'sonata_admin_doctrine_phpcr.tree_block.configuration',
37
            array(
38
                'routing_defaults' => array(),
39
                'repository_name' => null,
40
                'sortable_by' => 'position',
41
                'move' => true,
42
                'reorder' => true,
43
            )
44
        );
45
    }
46
47
    public function testPrependDefaultRepositoryName()
48
    {
49
        $this->container->setParameter('kernel.bundles', ['CmfResourceBundle']);
50
        $this->container->getExtensionConfig('sonata_doctrine_phpcr_admin');
51
        $this->container->prependExtensionConfig('sonata_doctrine_phpcr_admin', array('document_tree' => array()));
52
        $this->container->prependExtensionConfig('cmf_resource', array('default_repository' =>'default'));
53
54
        $this->container->getExtension('sonata_doctrine_phpcr_admin')->prepend($this->container);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Depend...sion\ExtensionInterface as the method prepend() does only exist in the following implementations of said interface: Sonata\AdminBundle\Depen...on\SonataAdminExtension, Sonata\CoreBundle\Depend...ion\SonataCoreExtension, Sonata\DoctrinePHPCRAdmi...rinePHPCRAdminExtension, Symfony\Bundle\Framework...Injection\TestExtension, Symfony\Cmf\Bundle\TreeB...CmfTreeBrowserExtension.

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...
55
56
        $config = $this->container->getExtensionConfig('sonata_doctrine_phpcr_admin');
57
        $expectedConfig = array(
58
            'routing_defaults' => array(),
59
            'repository_name' => 'default',
60
            'sortable_by' => 'position',
61
            'enabled' => true,
62
        );
63
64
        $this->assertEquals($expectedConfig, $config[0]['document_tree']);
65
    }
66
67
    /**
68
     * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
69
     */
70
    public function testPrependDefaultRepositoryNameThrowsOnNonExistingResourceBundle()
71
    {
72
        $this->container->setParameter('kernel.bundles', []);
73
        $this->container->getExtensionConfig('sonata_doctrine_phpcr_admin');
74
        $this->container->prependExtensionConfig('sonata_doctrine_phpcr_admin', array('document_tree' => array()));
75
        $this->container->prependExtensionConfig('cmf_resource', array('default_repository' =>'default'));
76
77
        $this->container->getExtension('sonata_doctrine_phpcr_admin')->prepend($this->container);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Depend...sion\ExtensionInterface as the method prepend() does only exist in the following implementations of said interface: Sonata\AdminBundle\Depen...on\SonataAdminExtension, Sonata\CoreBundle\Depend...ion\SonataCoreExtension, Sonata\DoctrinePHPCRAdmi...rinePHPCRAdminExtension, Symfony\Bundle\Framework...Injection\TestExtension, Symfony\Cmf\Bundle\TreeB...CmfTreeBrowserExtension.

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...
78
    }
79
80
    public function testPrependDefaultRepositoryNameKeepsCustomNames()
81
    {
82
        $this->container->setParameter('kernel.bundles', ['CmfResourceBundle']);
83
        $this->container->getExtensionConfig('sonata_doctrine_phpcr_admin');
84
        $this->container->prependExtensionConfig(
85
            'sonata_doctrine_phpcr_admin',
86
            array('document_tree' => array('repository_name' => 'custom'))
87
        );
88
        $this->container->prependExtensionConfig('cmf_resource', array('default_repository' =>'default'));
89
90
        $this->container->getExtension('sonata_doctrine_phpcr_admin')->prepend($this->container);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Depend...sion\ExtensionInterface as the method prepend() does only exist in the following implementations of said interface: Sonata\AdminBundle\Depen...on\SonataAdminExtension, Sonata\CoreBundle\Depend...ion\SonataCoreExtension, Sonata\DoctrinePHPCRAdmi...rinePHPCRAdminExtension, Symfony\Bundle\Framework...Injection\TestExtension, Symfony\Cmf\Bundle\TreeB...CmfTreeBrowserExtension.

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...
91
92
        $config = $this->container->getExtensionConfig('sonata_doctrine_phpcr_admin');
93
        $expectedConfig = array('repository_name' => 'custom');
94
95
        $this->assertEquals($expectedConfig, $config[0]['document_tree']);
96
    }
97
}
98