浏览代码

Implemented getAdminsByGroup() / hasGroup() functions

Joost Farla 12 年之前
父节点
当前提交
1f7184d68c
共有 2 个文件被更改,包括 79 次插入0 次删除
  1. 37 0
      Admin/Pool.php
  2. 42 0
      Tests/Admin/PoolTest.php

+ 37 - 0
Admin/Pool.php

@@ -60,6 +60,17 @@ class Pool
         return $groups;
     }
 
+    /**
+     * Returns whether an admin group exists or not.
+     *
+     * @param string $group
+     * @return bool
+     */
+    public function hasGroup($group)
+    {
+        return isset($this->adminGroups[$group]);
+    }
+
     /**
      * @return array
      */
@@ -88,6 +99,32 @@ class Pool
         return $groups;
     }
 
+    /**
+     * Returns all admins related to the given $group
+     *
+     * @param string $group
+     * @return array
+     * @throws \InvalidArgumentException
+     */
+    public function getAdminsByGroup($group)
+    {
+        if (!isset($this->adminGroups[$group])) {
+            throw new \InvalidArgumentException(sprintf('Group "%s" not found in admin pool.', $group));
+        }
+
+        $admins = array();
+
+        if (!isset($this->adminGroups[$group]['items'])) {
+            return $admins;
+        }
+
+        foreach ($this->adminGroups[$group]['items'] as $id) {
+            $admins[] = $this->getInstance($id);
+        }
+
+        return $admins;
+    }
+
     /**
      * return the admin related to the given $class
      *

+ 42 - 0
Tests/Admin/PoolTest.php

@@ -40,6 +40,16 @@ class PoolTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($expectedOutput, $this->pool->getGroups());
     }
 
+    public function testHasGroup()
+    {
+        $this->pool->setAdminGroups(array(
+                'adminGroup1' => array()
+            ));
+
+        $this->assertTrue($this->pool->hasGroup('adminGroup1'));
+        $this->assertFalse($this->pool->hasGroup('adminGroup2'));
+    }
+
     public function testGetDashboardGroups()
     {
 
@@ -77,6 +87,38 @@ class PoolTest extends \PHPUnit_Framework_TestCase
         $this->assertCount(1, $groups);
     }
 
+    /**
+     * @expectedException \InvalidArgumentException
+     */
+    public function testGetAdminsByGroupWhenGroupNotSet()
+    {
+        $this->pool->setAdminGroups(array(
+                'adminGroup1' => array()
+            ));
+
+        $this->pool->getAdminsByGroup('adminGroup2');
+    }
+
+    public function testGetAdminsByGroupWhenGroupIsEmpty()
+    {
+        $this->pool->setAdminGroups(array(
+                'adminGroup1' => array()
+            ));
+
+        $this->assertEquals(array(), $this->pool->getAdminsByGroup('adminGroup1'));
+    }
+
+    public function testGetAdminsByGroup()
+    {
+        $this->pool->setAdminGroups(array(
+            'adminGroup1' => array('items' => array('sonata.admin1', 'sonata.admin2')),
+            'adminGroup2' => array('items' => array('sonata.admin3'))
+        ));
+
+        $this->assertCount(2, $this->pool->getAdminsByGroup('adminGroup1'));
+        $this->assertCount(1, $this->pool->getAdminsByGroup('adminGroup2'));
+    }
+
     public function testGetAdminForClassWhenAdminClassIsNotSet()
     {
         $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));