Skip to content

Commit

Permalink
Merge pull request ongr-archive#33 from chyzas/patch_updating_tree_bu…
Browse files Browse the repository at this point in the history
…ilder

configuration tree builder update
  • Loading branch information
saimaz committed Jan 16, 2015
2 parents 50da353 + a71673c commit 0f851de
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 27 deletions.
3 changes: 3 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->scalarNode('category_root_id')
->defaultValue('root_id')
->end()
->arrayNode('snippet')
->cannotBeOverwritten()
->addDefaultsIfNotSet()
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/ONGRContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand Down
13 changes: 9 additions & 4 deletions Service/CategoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
*/
class CategoryService
{
const ROOT_CATEGORY_ID = 'category_root_id';

/**
* @var Repository
*/
private $repository;

/**
* @var string
*/
private $rootId;

/**
* @var array
*/
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);
Expand Down
51 changes: 28 additions & 23 deletions Tests/Unit/Service/CategoryServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
*/
class CategoryServiceTest extends ElasticsearchTestCase
{
/**
* @var string
*/
private $rootId = 'root_id';

/**
* {@inheritdoc}
*/
Expand All @@ -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,
],
Expand All @@ -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,
],
Expand All @@ -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,
Expand All @@ -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,
],
Expand Down Expand Up @@ -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,
],
Expand All @@ -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,
],
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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,
],
Expand All @@ -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,
],
Expand All @@ -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,
Expand All @@ -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,
],
Expand Down Expand Up @@ -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,
],
Expand All @@ -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,
],
Expand All @@ -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']]);
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
]
);
Expand All @@ -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,
]
);
Expand All @@ -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,
Expand All @@ -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,
]
);
Expand Down Expand Up @@ -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,
],
Expand All @@ -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,
],
Expand All @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions Tests/app/config/config_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0f851de

Please sign in to comment.