Skip to content

Commit

Permalink
Rename getAccessTree() to getHierarchy() in `ItemsStorageInterfac…
Browse files Browse the repository at this point in the history
…e` implentations
  • Loading branch information
arogachev committed Feb 15, 2024
1 parent ef4ddcf commit 3a5af17
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 24 deletions.
1 change: 0 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
| Is bugfix? | ✔️/❌
| New feature? | ✔️/❌
| Breaks BC? | ✔️/❌
| Fixed issues | comma-separated list of tickets # fixed by the PR, if any
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- Enh #53: Use snake case for item attribute names (ease migration from Yii 2) (@arogachev)
- Enh #45: Decrease size for string columns from 128 to 126 for PostgreSQL optimization (@arogachev)
- Chg #58: Sync with changes in db package (simplify quoting and recursive query) (@arogachev)
- Chg #: Rename `getAccessTree()` to `getHierarchy()` in `ItemsStorageInterface` implentations (@arogachev)

## 1.0.0 April 20, 2023

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The package provides [Yii Database](https://github.com/yiisoft/db) storage for

## Requirements

- PHP 8.0 or higher.
- PHP 8.1 or higher.
- `PDO` PHP extension.
- One of the following drivers:
- [SQLite](https://github.com/yiisoft/db-sqlite) (minimal required version is 3.8.3)
Expand Down
6 changes: 3 additions & 3 deletions src/ItemTreeTraversal/CteItemTreeTraversal.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @internal
*
* @psalm-import-type RawItem from ItemsStorage
* @psalm-import-type AccessTree from ItemTreeTraversalInterface
* @psalm-import-type Hierarchy from ItemTreeTraversalInterface
*/
abstract class CteItemTreeTraversal implements ItemTreeTraversalInterface
{
Expand Down Expand Up @@ -52,7 +52,7 @@ public function getParentRows(string $name): array
return $this->getRowsCommand($name, baseOuterQuery: $baseOuterQuery)->queryAll();
}

public function getAccessTree(string $name): array
public function getHierarchy(string $name): array
{
$baseOuterQuery = (new Query($this->database))->select(['item.*', 'parent_of.children']);
$cteSelectRelationQuery = (new Query($this->database))
Expand All @@ -74,7 +74,7 @@ public function getAccessTree(string $name): array
['item.name' => new Expression('{{parent_of}}.[[child_name]]')],
);

/** @psalm-var AccessTree */
/** @psalm-var Hierarchy */
return $outerQuery->all();
}

Expand Down
6 changes: 3 additions & 3 deletions src/ItemTreeTraversal/ItemTreeTraversalInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @internal
*
* @psalm-type AccessTree = non-empty-list<array{
* @psalm-type Hierarchy = non-empty-list<array{
* type: Item::TYPE_*,
* name: string,
* description: string|null,
Expand Down Expand Up @@ -43,9 +43,9 @@ public function getParentRows(string $name): array;
*
* @param string $name The child name.
*
* @psalm-return AccessTree
* @psalm-return Hierarchy
*/
public function getAccessTree(string $name): array;
public function getHierarchy(string $name): array;

/**
* Get all children rows for an item by the given name.
Expand Down
6 changes: 3 additions & 3 deletions src/ItemTreeTraversal/MysqlItemTreeTraversal.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @internal
*
* @psalm-import-type RawItem from ItemsStorage
* @psalm-import-type AccessTree from ItemTreeTraversalInterface
* @psalm-import-type Hierarchy from ItemTreeTraversalInterface
*/
final class MysqlItemTreeTraversal implements ItemTreeTraversalInterface
{
Expand Down Expand Up @@ -60,7 +60,7 @@ public function getParentRows(string $name): array
->queryAll();
}

public function getAccessTree(string $name): array
public function getHierarchy(string $name): array

Check warning on line 63 in src/ItemTreeTraversal/MysqlItemTreeTraversal.php

View check run for this annotation

Codecov / codecov/patch

src/ItemTreeTraversal/MysqlItemTreeTraversal.php#L63

Added line #L63 was not covered by tests
{
$sql = "SELECT item.*, access_tree_base.children FROM (
SELECT
Expand All @@ -75,7 +75,7 @@ public function getAccessTree(string $name): array
) access_tree_base
LEFT JOIN $this->tableName AS item ON item.name = access_tree_base.child_name";

/** @psalm-var AccessTree */
/** @psalm-var Hierarchy */
return $this
->database
->createCommand($sql, [':name' => $name])
Expand Down
4 changes: 2 additions & 2 deletions src/ItemsStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,12 @@ public function getParents(string $name): array
return $this->getItemsIndexedByName($rawItems);
}

public function getAccessTree(string $name): array
public function getHierarchy(string $name): array
{
$tree = [];
$childrenNamesMap = [];

foreach ($this->getTreeTraversal()->getAccessTree($name) as $data) {
foreach ($this->getTreeTraversal()->getHierarchy($name) as $data) {
$childrenNamesMap[$data['name']] = $data['children'] !== '' && $data['children'] !== null
? explode($this->namesSeparator, $data['children'])
: [];
Expand Down
31 changes: 20 additions & 11 deletions tests/Base/ItemsStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ abstract class ItemsStorageTest extends TestCase
setUp as protected traitSetUp;
tearDown as protected traitTearDown;
testClear as protected traitTestClear;
dataRemove as protected traitDataRemove;
testRemove as protected traitTestRemove;
testClearPermissions as protected traitTestClearPermissions;
testClearRoles as protected traitTestClearRoles;
Expand All @@ -30,7 +31,7 @@ abstract class ItemsStorageTest extends TestCase

protected function setUp(): void
{
if ($this->name() === 'testGetAccessTreeWithCustomSeparator') {
if ($this->name() === 'testGetHierarchyWithCustomSeparator') {
ClockMock::freeze(new DateTime('2023-12-24 17:51:18'));
}

Expand All @@ -43,7 +44,7 @@ protected function tearDown(): void
parent::tearDown();
$this->traitTearDown();

if ($this->name() === 'testGetAccessTreeWithCustomSeparator') {
if ($this->name() === 'testGetHierarchyWithCustomSeparator') {
ClockMock::reset();
}
}
Expand All @@ -58,12 +59,15 @@ public function testClear(): void
$this->assertFalse($itemsChildrenExist);
}

public function testRemove(): void
/**
* @dataProvider traitDataRemove
*/
public function testRemove(string $name): void
{
$storage = $this->getItemsStorage();
$initialItemChildrenCount = count($storage->getAllChildren('Parent 2'));
$initialItemChildrenCount = count($storage->getAllChildren($name));

$this->traitTestRemove();
$this->traitTestRemove($name);

$itemsChildren = (new Query($this->getDatabase()))
->from(self::$itemsChildrenTable)
Expand Down Expand Up @@ -91,14 +95,14 @@ public function testClearRoles(): void
$this->assertSame($this->initialBothPermissionsChildrenCount, $itemsChildrenCount);
}

public function testGetAccessTreeSeparatorCollision(): void
public function testGetHierarchySeparatorCollision(): void
{
$this->expectException(SeparatorCollisionException::class);
$this->expectExceptionMessage('Separator collision has been detected.');
$this->getItemsStorage()->getAccessTree('posts.view');
$this->getItemsStorage()->getHierarchy('posts.view');
}

public function testGetAccessTreeWithCustomSeparator(): void
public function testGetHierarchyWithCustomSeparator(): void
{
$createdAt = (new DateTime('2023-12-24 17:51:18'))->getTimestamp();
$postsViewPermission = (new Permission('posts.view'))->withCreatedAt($createdAt)->withUpdatedAt($createdAt);
Expand All @@ -123,7 +127,7 @@ public function testGetAccessTreeWithCustomSeparator(): void
],
],
],
$this->getItemsStorage()->getAccessTree('posts.view')
$this->getItemsStorage()->getHierarchy('posts.view')
);
}

Expand Down Expand Up @@ -174,10 +178,15 @@ protected function populateDatabase(): void
}

protected function getItemsStorage(): ItemsStorageInterface
{
return $this->createItemsStorage();
}

protected function createItemsStorage(): ItemsStorageInterface
{
return match ($this->name()) {
'testGetAccessTreeSeparatorCollision' => new ItemsStorage($this->getDatabase(), namesSeparator: '.'),
'testGetAccessTreeWithCustomSeparator' => new ItemsStorage($this->getDatabase(), namesSeparator: '|'),
'testGetHierarchySeparatorCollision' => new ItemsStorage($this->getDatabase(), namesSeparator: '.'),
'testGetHierarchyWithCustomSeparator' => new ItemsStorage($this->getDatabase(), namesSeparator: '|'),
default => new ItemsStorage($this->getDatabase()),
};
}
Expand Down

0 comments on commit 3a5af17

Please sign in to comment.