Completed
Push — http_cache_error_ttl ( 7ac505 )
by André
16:16
created

ContentTest::contentSettingsProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 78
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 54
nc 1
nop 0
dl 0
loc 78
rs 8.9019
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * File containing the ContentTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Bundle\EzPublishCoreBundle\Tests\DependencyInjection\Configuration\Parser;
10
11
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\EzPublishCoreExtension;
12
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Parser\Content as ContentConfigParser;
13
use Symfony\Component\Yaml\Yaml;
14
15
class ContentTest extends AbstractParserTestCase
16
{
17
    protected function getContainerExtensions()
18
    {
19
        return array(
20
            new EzPublishCoreExtension(array(new ContentConfigParser())),
21
        );
22
    }
23
24
    protected function getMinimalConfiguration()
25
    {
26
        return Yaml::parse(file_get_contents(__DIR__ . '/../../Fixtures/ezpublish_minimal.yml'));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Symfony\Componen...publish_minimal.yml')); (null|Symfony\Component\Y...e|string|array|stdClass) is incompatible with the return type of the parent method Matthias\SymfonyDependen...getMinimalConfiguration of type array.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
27
    }
28
29
    public function testDefaultContentSettings()
30
    {
31
        $this->load();
32
33
        $this->assertConfigResolverParameterValue('content.view_cache', true, 'ezdemo_site');
34
        $this->assertConfigResolverParameterValue('content.ttl_cache', true, 'ezdemo_site');
35
        $this->assertConfigResolverParameterValue('content.default_ttl', 600, 'ezdemo_site');
36
        $this->assertConfigResolverParameterValue('content.default_error_ttl', 10, 'ezdemo_site');
37
    }
38
39
    /**
40
     * @dataProvider contentSettingsProvider
41
     */
42 View Code Duplication
    public function testContentSettings(array $config, array $expected)
43
    {
44
        $this->load(
45
            array(
46
                'system' => array(
47
                    'ezdemo_site' => $config,
48
                ),
49
            )
50
        );
51
52
        foreach ($expected as $key => $val) {
53
            $this->assertConfigResolverParameterValue($key, $val, 'ezdemo_site');
54
        }
55
    }
56
57
    public function contentSettingsProvider()
58
    {
59
        return array(
60
            array(
61
                array(
62
                    'content' => array(
63
                        'view_cache' => true,
64
                        'ttl_cache' => true,
65
                        'default_ttl' => 100,
66
                    ),
67
                ),
68
                array(
69
                    'content.view_cache' => true,
70
                    'content.ttl_cache' => true,
71
                    'content.default_ttl' => 100,
72
                ),
73
            ),
74
            array(
75
                array(
76
                    'content' => array(
77
                        'view_cache' => false,
78
                        'ttl_cache' => false,
79
                        'default_ttl' => 123,
80
                        'default_error_ttl' => 234,
81
                    ),
82
                ),
83
                array(
84
                    'content.view_cache' => false,
85
                    'content.ttl_cache' => false,
86
                    'content.default_ttl' => 123,
87
                    'content.default_error_ttl' => 234,
88
                ),
89
            ),
90
            array(
91
                array(
92
                    'content' => array(
93
                        'view_cache' => false,
94
                    ),
95
                ),
96
                array(
97
                    'content.view_cache' => false,
98
                    'content.ttl_cache' => true,
99
                    'content.default_ttl' => 600,
100
                ),
101
            ),
102
            array(
103
                array(
104
                    'content' => array(
105
                        'tree_root' => array('location_id' => 123),
106
                    ),
107
                ),
108
                array(
109
                    'content.view_cache' => true,
110
                    'content.ttl_cache' => true,
111
                    'content.default_ttl' => 600,
112
                    'content.tree_root.location_id' => 123,
113
                ),
114
            ),
115
            array(
116
                array(
117
                    'content' => array(
118
                        'tree_root' => array(
119
                            'location_id' => 456,
120
                            'excluded_uri_prefixes' => array('/media/images', '/products'),
121
                        ),
122
                    ),
123
                ),
124
                array(
125
                    'content.view_cache' => true,
126
                    'content.ttl_cache' => true,
127
                    'content.default_ttl' => 600,
128
                    'content.default_error_ttl' => 10,
129
                    'content.tree_root.location_id' => 456,
130
                    'content.tree_root.excluded_uri_prefixes' => array('/media/images', '/products'),
131
                ),
132
            ),
133
        );
134
    }
135
}
136