Passed
Push — fix-8379 ( cc7769...dd436f )
by Maxime
05:08
created

testPrepopulatedNumChildrenCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 8
dl 0
loc 16
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace SilverStripe\Versioned\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\ORM\Hierarchy\Hierarchy;
7
use SilverStripe\Versioned\Versioned;
8
use SilverStripe\ORM\Tests\HierarchyTest\TestObject;
9
use SilverStripe\ORM\Tests\HierarchyTest\HideTestObject;
10
use SilverStripe\ORM\Tests\HierarchyTest\HideTestSubObject;
11
12
/**
13
 * @internal Only test the right values are returned, not that the cache is actually used.
14
 */
15
class HierachyCacheTest extends SapphireTest
16
{
17
18
    protected static $fixture_file = 'HierarchyTest.yml';
19
20
    protected static $extra_dataobjects = array(
21
        TestObject::class,
22
        HideTestObject::class,
23
        HideTestSubObject::class,
24
    );
25
26
    public function setUp()
27
    {
28
        parent::setUp();
29
        TestObject::singleton()->flushCache();
30
    }
31
32
    public static function setUpBeforeClass()
33
    {
34
        parent::setUpBeforeClass();
35
        HideTestObject::config()->update(
36
            'hide_from_hierarchy',
37
            [ HideTestSubObject::class ]
38
        );
39
    }
40
41
    public function cacheNumChildrenDataProvider()
42
    {
43
        return [
44
            [TestObject::class, 'obj1', false, 0, 'childless object should have a numChildren of 0'],
45
            [TestObject::class, 'obj1', true, 0, 'childless object should have a numChildren of 0 when cache'],
46
            [TestObject::class, 'obj2', false, 2, 'Root object numChildren should count direct children'],
47
            [TestObject::class, 'obj2', true, 2, 'Root object numChildren should count direct children when cache'],
48
            [TestObject::class, 'obj3a', false, 2, 'Sub object numChildren should count direct children'],
49
            [TestObject::class, 'obj3a', false, 2, 'Sub object numChildren should count direct children when cache'],
50
            [TestObject::class, 'obj3d', false, 0, 'Childess Sub object numChildren should be 0'],
51
            [TestObject::class, 'obj3d', true, 0, 'Childess Sub object numChildren should be 0 when cache'],
52
            [HideTestObject::class, 'obj4', false, 1, 'Hidden object should not be included in count'],
53
            [HideTestObject::class, 'obj4', true, 1, 'Hidden object should not be included in couunt when cache']
54
        ];
55
    }
56
57
58
    /**
59
     * @dataProvider cacheNumChildrenDataProvider
60
     */
61
    public function testNumChildrenCache($className, $identifier, $cache, $expected, $message)
62
    {
63
        $node = $this->objFromFixture($className, $identifier);
64
65
        $actual = $node->numChildren($cache);
0 ignored issues
show
Bug introduced by
The method numChildren() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        /** @scrutinizer ignore-call */ 
66
        $actual = $node->numChildren($cache);
Loading history...
66
67
        $this->assertEquals($expected, $actual, $message);
68
69
        if ($cache) {
70
            // When caching is eanbled, try re-accessing the numChildren value to make sure it doesn't change.
71
            $actual = $node->numChildren($cache);
72
            $this->assertEquals($expected, $actual, $message);
73
        }
74
    }
75
76
    public function prepopulateCacheNumChildrenDataProvider()
77
    {
78
        return [
79
            [
80
                TestObject::class, 'Stage', [],
81
                TestObject::class, 'obj1', false, 0, 'childless object should have a numChildren of 0'
82
            ],
83
            [
84
                TestObject::class, 'Stage', [],
85
                TestObject::class, 'obj1', true, 0, 'childless object should have a numChildren of 0 when cache'
86
            ],
87
            [
88
                TestObject::class, 'Stage', [2],
89
                TestObject::class, 'obj1', false, 0, 'childless object should have a numChildren of 0'
90
            ],
91
            [
92
                TestObject::class, 'Stage', [2],
93
                TestObject::class, 'obj1', true, 0, 'childless object should have a numChildren of 0 when cache'
94
            ],
95
            [
96
                TestObject::class, 'Stage', [],
97
                TestObject::class, 'obj2', false, 2, 'Root object numChildren should count direct children'
98
            ],
99
            [
100
                TestObject::class, 'Stage', [],
101
                TestObject::class, 'obj2', true, 2, 'Root object numChildren should count direct children when cache'
102
            ],
103
            [
104
                TestObject::class, 'Stage', [2],
105
                TestObject::class, 'obj2', false, 2, 'Root object numChildren should count direct children'
106
            ],
107
            [
108
                TestObject::class, 'Stage', [2],
109
                TestObject::class, 'obj2', true, 2, 'Root object numChildren should count direct children when cache'
110
            ],
111
            [
112
                HideTestObject::class, 'Stage', [],
113
                HideTestObject::class, 'obj4', false, 1, 'Hidden object should not be included in count'
114
            ],
115
            [
116
                HideTestObject::class, 'Stage', [],
117
                HideTestObject::class, 'obj4', true, 1, 'Hidden object should not be included in count when cache'
118
            ],
119
            [
120
                HideTestObject::class, 'Stage', [2],
121
                HideTestObject::class, 'obj4', false, 1, 'Hidden object should not be included in count'
122
            ],
123
            [
124
                HideTestObject::class, 'Stage', [2],
125
                HideTestObject::class, 'obj4', true, 1, 'Hidden object should not be included in count when cache'
126
            ]
127
        ];
128
    }
129
130
    /**
131
     * @dataProvider prepopulateCacheNumChildrenDataProvider
132
     */
133
    public function testPrepopulatedNumChildrenCache(
134
        $baseClass,
135
        $stage,
136
        $idList,
137
        $className,
138
        $identifier,
139
        $cache,
140
        $expected,
141
        $message
142
    ) {
143
        Hierarchy::prepopulate_numchildren_cache($baseClass, $stage, $idList);
144
        $node = $this->objFromFixture($className, $identifier);
145
146
        $actual = $node->numChildren($cache);
147
148
        $this->assertEquals($expected, $actual, $message);
149
    }
150
}
151