Passed
Push — 4 ( 3eacec...5b7a84 )
by Maxime
06:31
created

prepopulateCacheNumChildrenDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 50
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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

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