From dd436f510206f2c503914e22c7398f3c1eedf3f3 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Mon, 24 Sep 2018 00:05:09 +1200 Subject: [PATCH] Write unit test for testing Hierarchy caching. --- tests/php/ORM/HierarchyCachingTest.php | 150 +++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 tests/php/ORM/HierarchyCachingTest.php diff --git a/tests/php/ORM/HierarchyCachingTest.php b/tests/php/ORM/HierarchyCachingTest.php new file mode 100644 index 00000000000..656014bf145 --- /dev/null +++ b/tests/php/ORM/HierarchyCachingTest.php @@ -0,0 +1,150 @@ +flushCache(); + } + + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + HideTestObject::config()->update( + 'hide_from_hierarchy', + [ HideTestSubObject::class ] + ); + } + + public function cacheNumChildrenDataProvider() + { + return [ + [TestObject::class, 'obj1', false, 0, 'childless object should have a numChildren of 0'], + [TestObject::class, 'obj1', true, 0, 'childless object should have a numChildren of 0 when cache'], + [TestObject::class, 'obj2', false, 2, 'Root object numChildren should count direct children'], + [TestObject::class, 'obj2', true, 2, 'Root object numChildren should count direct children when cache'], + [TestObject::class, 'obj3a', false, 2, 'Sub object numChildren should count direct children'], + [TestObject::class, 'obj3a', false, 2, 'Sub object numChildren should count direct children when cache'], + [TestObject::class, 'obj3d', false, 0, 'Childess Sub object numChildren should be 0'], + [TestObject::class, 'obj3d', true, 0, 'Childess Sub object numChildren should be 0 when cache'], + [HideTestObject::class, 'obj4', false, 1, 'Hidden object should not be included in count'], + [HideTestObject::class, 'obj4', true, 1, 'Hidden object should not be included in couunt when cache'] + ]; + } + + + /** + * @dataProvider cacheNumChildrenDataProvider + */ + public function testNumChildrenCache($className, $identifier, $cache, $expected, $message) + { + $node = $this->objFromFixture($className, $identifier); + + $actual = $node->numChildren($cache); + + $this->assertEquals($expected, $actual, $message); + + if ($cache) { + // When caching is eanbled, try re-accessing the numChildren value to make sure it doesn't change. + $actual = $node->numChildren($cache); + $this->assertEquals($expected, $actual, $message); + } + } + + public function prepopulateCacheNumChildrenDataProvider() + { + return [ + [ + TestObject::class, 'Stage', [], + TestObject::class, 'obj1', false, 0, 'childless object should have a numChildren of 0' + ], + [ + TestObject::class, 'Stage', [], + TestObject::class, 'obj1', true, 0, 'childless object should have a numChildren of 0 when cache' + ], + [ + TestObject::class, 'Stage', [2], + TestObject::class, 'obj1', false, 0, 'childless object should have a numChildren of 0' + ], + [ + TestObject::class, 'Stage', [2], + TestObject::class, 'obj1', true, 0, 'childless object should have a numChildren of 0 when cache' + ], + [ + TestObject::class, 'Stage', [], + TestObject::class, 'obj2', false, 2, 'Root object numChildren should count direct children' + ], + [ + TestObject::class, 'Stage', [], + TestObject::class, 'obj2', true, 2, 'Root object numChildren should count direct children when cache' + ], + [ + TestObject::class, 'Stage', [2], + TestObject::class, 'obj2', false, 2, 'Root object numChildren should count direct children' + ], + [ + TestObject::class, 'Stage', [2], + TestObject::class, 'obj2', true, 2, 'Root object numChildren should count direct children when cache' + ], + [ + HideTestObject::class, 'Stage', [], + HideTestObject::class, 'obj4', false, 1, 'Hidden object should not be included in count' + ], + [ + HideTestObject::class, 'Stage', [], + HideTestObject::class, 'obj4', true, 1, 'Hidden object should not be included in count when cache' + ], + [ + HideTestObject::class, 'Stage', [2], + HideTestObject::class, 'obj4', false, 1, 'Hidden object should not be included in count' + ], + [ + HideTestObject::class, 'Stage', [2], + HideTestObject::class, 'obj4', true, 1, 'Hidden object should not be included in count when cache' + ] + ]; + } + + /** + * @dataProvider prepopulateCacheNumChildrenDataProvider + */ + public function testPrepopulatedNumChildrenCache( + $baseClass, + $stage, + $idList, + $className, + $identifier, + $cache, + $expected, + $message + ) { + Hierarchy::prepopulate_numchildren_cache($baseClass, $stage, $idList); + $node = $this->objFromFixture($className, $identifier); + + $actual = $node->numChildren($cache); + + $this->assertEquals($expected, $actual, $message); + } +}