diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index ab10a64..2b17ec1 100755 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -41,6 +41,9 @@ public function getConfigTreeBuilder() ->end() ->end() ->end() + ->scalarNode('category_root_id') + ->defaultValue('root_id') + ->end() ->arrayNode('snippet') ->cannotBeOverwritten() ->addDefaultsIfNotSet() diff --git a/DependencyInjection/ONGRContentExtension.php b/DependencyInjection/ONGRContentExtension.php index 6e1f299..964296a 100644 --- a/DependencyInjection/ONGRContentExtension.php +++ b/DependencyInjection/ONGRContentExtension.php @@ -46,6 +46,7 @@ public function load(array $configs, ContainerBuilder $container) $categoryService = $container->getDefinition('ongr_content.category_service'); $categoryService->addArgument(new Reference($repositories['category'])); + $categoryService->addArgument($config['category_root_id']); $container->setParameter('ongr_content.snippet.render_strategy', $config['snippet']['render_strategy']); } diff --git a/Service/CategoryService.php b/Service/CategoryService.php index 3eca189..c957828 100755 --- a/Service/CategoryService.php +++ b/Service/CategoryService.php @@ -24,13 +24,16 @@ */ class CategoryService { - const ROOT_CATEGORY_ID = 'category_root_id'; - /** * @var Repository */ private $repository; + /** + * @var string + */ + private $rootId; + /** * @var array */ @@ -58,10 +61,12 @@ class CategoryService /** * @param Repository $repository + * @param string $rootId */ - public function __construct(Repository $repository) + public function __construct(Repository $repository, $rootId) { $this->repository = $repository; + $this->rootId = $rootId; } /** @@ -148,7 +153,7 @@ private function buildNode($node, $references, $tree, $maxLevel) $references[$node->getId()] = $node; - if ($node->getParentId() == self::ROOT_CATEGORY_ID) { + if ($node->getParentId() == $this->rootId) { $this->buildRootNode($node, $tree, 1); } else { $this->buildChildNode($node, $references, $maxLevel); diff --git a/Tests/Unit/Service/CategoryServiceTest.php b/Tests/Unit/Service/CategoryServiceTest.php index cd2046c..3154852 100644 --- a/Tests/Unit/Service/CategoryServiceTest.php +++ b/Tests/Unit/Service/CategoryServiceTest.php @@ -20,6 +20,11 @@ */ class CategoryServiceTest extends ElasticsearchTestCase { + /** + * @var string + */ + private $rootId = 'root_id'; + /** * {@inheritdoc} */ @@ -33,7 +38,7 @@ protected function getDataArray() 'is_active' => true, 'sort' => 1, 'left' => 2, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, 'is_hidden' => false, ], @@ -42,7 +47,7 @@ protected function getDataArray() 'is_active' => true, 'sort' => 2, 'left' => 8, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, 'is_hidden' => false, ], @@ -51,7 +56,7 @@ protected function getDataArray() 'is_active' => true, 'sort' => 1, 'left' => 1, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, 'is_current' => true, 'is_expanded' => true, @@ -62,7 +67,7 @@ protected function getDataArray() 'is_active' => true, 'sort' => 1, 'left' => 3, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, 'is_hidden' => false, ], @@ -107,7 +112,7 @@ protected function getDataArray() 'is_active' => true, 'sort' => 1, 'left' => 3, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, 'is_hidden' => false, ], @@ -116,7 +121,7 @@ protected function getDataArray() 'is_active' => true, 'sort' => 1, 'left' => 9, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, 'is_hidden' => false, ], @@ -153,7 +158,7 @@ public function testSetGetCurrentCategoryId() { $repository = $this->getManager()->getRepository('AcmeTestBundle:Category'); - $service = new CategoryService($repository); + $service = new CategoryService($repository, $this->rootId); $service->setRepository($repository); $id = 'testid'; @@ -198,7 +203,7 @@ public function treeDataProvider() 'is_active' => true, 'sort' => 1, 'left' => 2, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, 'is_hidden' => false, ], @@ -207,7 +212,7 @@ public function treeDataProvider() 'is_active' => true, 'sort' => 2, 'left' => 8, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, 'is_hidden' => false, ], @@ -216,7 +221,7 @@ public function treeDataProvider() 'is_active' => true, 'sort' => 1, 'left' => 1, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, 'is_current' => true, 'is_expanded' => true, @@ -227,7 +232,7 @@ public function treeDataProvider() 'is_active' => true, 'sort' => 1, 'left' => 3, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, 'is_hidden' => false, ], @@ -272,7 +277,7 @@ public function treeDataProvider() 'is_active' => true, 'sort' => 1, 'left' => 3, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, 'is_hidden' => false, ], @@ -281,7 +286,7 @@ public function treeDataProvider() 'is_active' => true, 'sort' => 1, 'left' => 9, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, 'is_hidden' => false, ], @@ -293,7 +298,7 @@ public function treeDataProvider() $data[$category['id']] = $this->buildCategory($category); } foreach ($catData as $category) { - if ($category['parent_id'] == CategoryService::ROOT_CATEGORY_ID) { + if ($category['parent_id'] == $this->rootId) { continue; } $data[$category['id']]->setParent($data[$category['parent_id']]); @@ -314,7 +319,7 @@ public function testGetTree(array $data) { $repository = $this->getManager()->getRepository('AcmeTestBundle:Category'); - $service = new CategoryService($repository); + $service = new CategoryService($repository, $this->rootId); $service->setCurrentCategoryId('cat3'); $service->getCurrentCategoryDocument(); @@ -345,7 +350,7 @@ public function testGetTree(array $data) */ public function testGetCurrentCategoryDocument(array $data) { - $service = new CategoryService($this->getManager()->getRepository('AcmeTestBundle:Category')); + $service = new CategoryService($this->getManager()->getRepository('AcmeTestBundle:Category'), $this->rootId); $service->setCurrentCategoryId('cat42'); $leaf = $service->getCurrentCategoryDocument(); @@ -378,7 +383,7 @@ public function getPartialTreeDataProvider() 'is_active' => true, 'sort' => 1, 'left' => 2, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, ] ); @@ -405,7 +410,7 @@ public function getPartialTreeDataProvider() 'is_active' => true, 'sort' => 2, 'left' => 8, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, ] ); @@ -416,7 +421,7 @@ public function getPartialTreeDataProvider() 'is_active' => true, 'sort' => 1, 'left' => 1, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, 'is_current' => true, 'is_expanded' => true, @@ -429,7 +434,7 @@ public function getPartialTreeDataProvider() 'is_active' => true, 'sort' => 1, 'left' => 3, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, ] ); @@ -546,7 +551,7 @@ public function getCategoryDataProvider() 'is_active' => true, 'sort' => 1, 'left' => 2, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, 'is_hidden' => false, ], @@ -555,7 +560,7 @@ public function getCategoryDataProvider() 'is_active' => true, 'sort' => 2, 'left' => 8, - 'parent_id' => CategoryService::ROOT_CATEGORY_ID, + 'parent_id' => $this->rootId, 'level' => 1, 'is_hidden' => false, ], @@ -582,7 +587,7 @@ public function getCategoryDataProvider() */ public function testGetCategory($categoryId, $category) { - $service = new CategoryService($this->getManager()->getRepository('AcmeTestBundle:Category')); + $service = new CategoryService($this->getManager()->getRepository('AcmeTestBundle:Category'), $this->rootId); $result = $service->getCategory($categoryId); $this->assertEquals($category, $result); } diff --git a/Tests/app/config/config_test.yml b/Tests/app/config/config_test.yml index 0af87c6..a512d27 100644 --- a/Tests/app/config/config_test.yml +++ b/Tests/app/config/config_test.yml @@ -38,6 +38,7 @@ ongr_content: product: es.manager.default.product content: es.manager.default.content category: es.manager.default.category + category_root_id: 'root_id' ongr_router: es_manager: default