Completed
Push — master ( bf3f93...22d625 )
by Maksim
03:11
created

FlysystemLoaderFactoryTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 48 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 9
c 4
b 1
f 1
lcom 1
cbo 6
dl 48
loc 100
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 2
A testImplementsLoaderFactoryInterface() 0 6 1
A testCouldBeConstructedWithoutAnyArguments() 0 4 1
A testReturnExpectedName() 0 6 1
A testCreateLoaderDefinitionOnCreate() 19 19 1
A testThrowIfFileSystemServiceNotSetOnAddConfiguration() 10 10 1
A testProcessCorrectlyOptionsOnAddConfiguration() 19 19 1
A processConfigTree() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Liip\ImagineBundle\Tests\DependencyInjection\Factory\Loader;
4
5
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\FlysystemLoaderFactory;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\Processor;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
10
/**
11
 * @requires PHP 5.4
12
 * @covers Liip\ImagineBundle\DependencyInjection\Factory\Loader\FlysystemLoaderFactory<extended>
13
 */
14
class FlysystemLoaderFactoryTest extends \Phpunit_Framework_TestCase
15
{
16
    public function setUp()
17
    {
18
        parent::setUp();
19
20
        if (!class_exists('\League\Flysystem\Filesystem')) {
21
            $this->markTestSkipped(
22
              'The league/flysystem PHP library is not available.'
23
            );
24
        }
25
    }
26
27
    public function testImplementsLoaderFactoryInterface()
28
    {
29
        $rc = new \ReflectionClass('Liip\ImagineBundle\DependencyInjection\Factory\Loader\FlysystemLoaderFactory');
30
31
        $this->assertTrue($rc->implementsInterface('Liip\ImagineBundle\DependencyInjection\Factory\Loader\LoaderFactoryInterface'));
32
    }
33
34
    public function testCouldBeConstructedWithoutAnyArguments()
35
    {
36
        new FlysystemLoaderFactory();
37
    }
38
39
    public function testReturnExpectedName()
40
    {
41
        $loader = new FlysystemLoaderFactory();
42
43
        $this->assertEquals('flysystem', $loader->getName());
44
    }
45
46 View Code Duplication
    public function testCreateLoaderDefinitionOnCreate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        $container = new ContainerBuilder();
49
50
        $loader = new FlysystemLoaderFactory();
51
52
        $loader->create($container, 'theLoaderName', array(
53
            'filesystem_service' => 'flyfilesystemservice',
54
        ));
55
56
        $this->assertTrue($container->hasDefinition('liip_imagine.binary.loader.theloadername'));
57
58
        $loaderDefinition = $container->getDefinition('liip_imagine.binary.loader.theloadername');
59
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $loaderDefinition);
60
        $this->assertEquals('liip_imagine.binary.loader.prototype.flysystem', $loaderDefinition->getParent());
61
62
        $reference = $loaderDefinition->getArgument(1);
63
        $this->assertEquals('flyfilesystemservice', "$reference");
64
    }
65
66
    /**
67
     * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
68
     * @expectedExceptionMessage The child node "filesystem_service" at path "flysystem" must be configured.
69
     */
70 View Code Duplication
    public function testThrowIfFileSystemServiceNotSetOnAddConfiguration()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $treeBuilder = new TreeBuilder();
73
        $rootNode = $treeBuilder->root('flysystem', 'array');
74
75
        $resolver = new FlysystemLoaderFactory();
76
        $resolver->addConfiguration($rootNode);
77
78
        $this->processConfigTree($treeBuilder, array());
79
    }
80
81 View Code Duplication
    public function testProcessCorrectlyOptionsOnAddConfiguration()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        $expectedService = 'theService';
84
85
        $treeBuilder = new TreeBuilder();
86
        $rootNode = $treeBuilder->root('flysystem', 'array');
87
88
        $loader = new FlysystemLoaderFactory();
89
        $loader->addConfiguration($rootNode);
90
91
        $config = $this->processConfigTree($treeBuilder, array(
92
            'flysystem' => array(
93
                'filesystem_service' => $expectedService,
94
            ),
95
        ));
96
97
        $this->assertArrayHasKey('filesystem_service', $config);
98
        $this->assertEquals($expectedService, $config['filesystem_service']);
99
    }
100
101
    /**
102
     * @param TreeBuilder $treeBuilder
103
     * @param array       $configs
104
     *
105
     * @return array
106
     */
107
    protected function processConfigTree(TreeBuilder $treeBuilder, array $configs)
108
    {
109
        $processor = new Processor();
110
111
        return $processor->process($treeBuilder->buildTree(), $configs);
112
    }
113
}
114