Completed
Pull Request — master (#453)
by Maximilian
18:42 queued 14:16
created

testPrependDefaultRepositoryName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 19
rs 9.4285
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
17
class SonataDoctrinePHPCRAdminExtensionTest extends AbstractExtensionTestCase
18
{
19
    public function getContainerExtensions()
20
    {
21
        return array(
22
           new SonataDoctrinePHPCRAdminExtension(),
23
        );
24
    }
25
26
    public function testDocumentTreeDefaultValues()
27
    {
28
        $this->container->setParameter(
29
            'kernel.bundles',
30
            array()
31
        );
32
        $this->load(array('document_tree' => array()));
33
34
        $this->assertContainerBuilderHasParameter(
35
            'sonata_admin_doctrine_phpcr.tree_block.configuration',
36
            array(
37
                'routing_defaults' => array(),
38
                'repository_name' => null,
39
                'sortable_by' => 'position',
40
                'move' => true,
41
                'reorder' => true,
42
            )
43
        );
44
    }
45
46
    public function testPrependDefaultRepositoryName()
47
    {
48
        $this->container->setParameter('kernel.bundles', array('CmfResourceBundle'));
49
        $this->container->getExtensionConfig('sonata_doctrine_phpcr_admin');
50
        $this->container->prependExtensionConfig('sonata_doctrine_phpcr_admin', array('document_tree' => array()));
51
        $this->container->prependExtensionConfig('cmf_resource', array('default_repository' => 'default'));
52
53
        $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...
54
55
        $config = $this->container->getExtensionConfig('sonata_doctrine_phpcr_admin');
56
        $expectedConfig = array(
57
            'routing_defaults' => array(),
58
            'repository_name' => 'default',
59
            'sortable_by' => 'position',
60
            'enabled' => true,
61
        );
62
63
        $this->assertEquals($expectedConfig, $config[0]['document_tree']);
64
    }
65
66
    /**
67
     * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
68
     */
69
    public function testPrependDefaultRepositoryNameThrowsOnNonExistingResourceBundle()
70
    {
71
        $this->container->setParameter('kernel.bundles', array());
72
        $this->container->getExtensionConfig('sonata_doctrine_phpcr_admin');
73
        $this->container->prependExtensionConfig('sonata_doctrine_phpcr_admin', array('document_tree' => array()));
74
        $this->container->prependExtensionConfig('cmf_resource', array('default_repository' => 'default'));
75
76
        $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...
77
    }
78
79
    public function testPrependDefaultRepositoryNameKeepsCustomNames()
80
    {
81
        $this->container->setParameter('kernel.bundles', array('CmfResourceBundle'));
82
        $this->container->getExtensionConfig('sonata_doctrine_phpcr_admin');
83
        $this->container->prependExtensionConfig(
84
            'sonata_doctrine_phpcr_admin',
85
            array('document_tree' => array('repository_name' => 'custom'))
86
        );
87
        $this->container->prependExtensionConfig('cmf_resource', array('default_repository' => 'default'));
88
89
        $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...
90
91
        $config = $this->container->getExtensionConfig('sonata_doctrine_phpcr_admin');
92
        $expectedConfig = array('repository_name' => 'custom');
93
94
        $this->assertEquals($expectedConfig, $config[0]['document_tree']);
95
    }
96
}
97