|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Spatie\Permission\Test; |
| 4 | + |
| 5 | +class RoleWithNestingTest extends TestCase |
| 6 | +{ |
| 7 | + private static $old_migration; |
| 8 | + /** |
| 9 | + * @var RoleWithNesting[] |
| 10 | + */ |
| 11 | + protected $parent_roles=[]; |
| 12 | + /** |
| 13 | + * @var RoleWithNesting[] |
| 14 | + */ |
| 15 | + protected $child_roles=[]; |
| 16 | + |
| 17 | + public static function setUpBeforeClass(): void |
| 18 | + { |
| 19 | + parent::setUpBeforeClass(); |
| 20 | + self::$old_migration = self::$migration; |
| 21 | + self::$migration = self::getMigration(); |
| 22 | + } |
| 23 | + |
| 24 | + public function setUp(): void |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + $this->parent_roles = []; |
| 28 | + $this->child_roles = []; |
| 29 | + $this->parent_roles["has_no_children"] = RoleWithNesting::create(["name"=>"has_no_children"]); |
| 30 | + $this->parent_roles["has_1_child"] = RoleWithNesting::create(["name"=>"has_1_child"]); |
| 31 | + $this->parent_roles["has_3_children"] = RoleWithNesting::create(["name"=>"has_3_children"]); |
| 32 | + |
| 33 | + $this->child_roles["has_no_parents"] = RoleWithNesting::create(["name"=>"has_no_parents"]); |
| 34 | + $this->child_roles["has_1_parent"] = RoleWithNesting::create(["name"=>"has_1_parent"]); |
| 35 | + $this->child_roles["has_2_parents"] = RoleWithNesting::create(["name"=>"has_2_parents"]); |
| 36 | + $this->child_roles["third_child"] = RoleWithNesting::create(["name"=>"third_child"]); |
| 37 | + |
| 38 | + $this->parent_roles["has_1_child"]->children()->attach($this->child_roles["has_2_parents"]); |
| 39 | + $this->parent_roles["has_3_children"]->children()->attach($this->child_roles["has_2_parents"]); |
| 40 | + $this->parent_roles["has_3_children"]->children()->attach($this->child_roles["has_1_parent"]); |
| 41 | + $this->parent_roles["has_3_children"]->children()->attach($this->child_roles["third_child"]); |
| 42 | + } |
| 43 | + |
| 44 | + public static function tearDownAfterClass(): void |
| 45 | + { |
| 46 | + parent::tearDownAfterClass(); |
| 47 | + self::$migration = self::$old_migration; |
| 48 | + } |
| 49 | + |
| 50 | + protected function getEnvironmentSetUp($app) |
| 51 | + { |
| 52 | + parent::getEnvironmentSetUp($app); |
| 53 | + $app['config']->set('permission.models.role', RoleWithNesting::class); |
| 54 | + $app['config']->set('permission.table_names.roles', "nesting_role"); |
| 55 | + } |
| 56 | + |
| 57 | + protected static function getMigration() |
| 58 | + { |
| 59 | + require_once __DIR__."/customMigrations/roles_with_nesting_migration.php.stub"; |
| 60 | + return new \CreatePermissionTablesWithNested(); |
| 61 | + } |
| 62 | + |
| 63 | + /** @test |
| 64 | + * @dataProvider roles_list |
| 65 | + */ |
| 66 | + public function it_returns_correct_withCount_of_nested_roles($role_group,$index,$relation,$expectedCount) |
| 67 | + { |
| 68 | + $role = $this->$role_group[$index]; |
| 69 | + $count_field_name = sprintf("%s_count", $relation); |
| 70 | + |
| 71 | + $actualCount = intval(RoleWithNesting::query()->withCount($relation)->find($role->id)->$count_field_name); |
| 72 | + |
| 73 | + $this->assertSame( |
| 74 | + $expectedCount, |
| 75 | + $actualCount, |
| 76 | + sprintf("%s expects %d %s, %d found",$role->name,$expectedCount,$relation,$actualCount) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + public function roles_list(){ |
| 81 | + return [ |
| 82 | + ["parent_roles","has_no_children","children",0], |
| 83 | + ["parent_roles","has_1_child","children",1], |
| 84 | + ["parent_roles","has_3_children","children",3], |
| 85 | + ["child_roles","has_no_parents","parents",0], |
| 86 | + ["child_roles","has_1_parent","parents",1], |
| 87 | + ["child_roles","has_2_parents","parents",2], |
| 88 | + ]; |
| 89 | + } |
| 90 | +} |
0 commit comments