'OU=oddzial,DC=example,DC=com', 'ldap.defaults.computer_ou' => 'OU=Komputery,DC=example,DC=com', ]); $userOu = new class('OU=oddzial,DC=example,DC=com', 'oddzial') { public function __construct(public string $dn, public string $name) {} public function getDn(): string { return $this->dn; } public function getFirstAttribute(string $attribute): ?string { return $attribute === 'ou' ? $this->name : null; } }; $nestedUserOu = new class('OU=Users,OU=oddzial,DC=example,DC=com', 'Users') { public function __construct(public string $dn, public string $name) {} public function getDn(): string { return $this->dn; } public function getFirstAttribute(string $attribute): ?string { return $attribute === 'ou' ? $this->name : null; } }; $computerOu = new class('OU=Komputery,DC=example,DC=com', 'Komputery') { public function __construct(public string $dn, public string $name) {} public function getDn(): string { return $this->dn; } public function getFirstAttribute(string $attribute): ?string { return $attribute === 'ou' ? $this->name : null; } }; $computerChild = new class('OU=Workstations,OU=Komputery,DC=example,DC=com', 'Workstations') { public function __construct(public string $dn, public string $name) {} public function getDn(): string { return $this->dn; } public function getFirstAttribute(string $attribute): ?string { return $attribute === 'ou' ? $this->name : null; } }; $externalOu = new class('OU=External,DC=example,DC=com', 'External') { public function __construct(public string $dn, public string $name) {} public function getDn(): string { return $this->dn; } public function getFirstAttribute(string $attribute): ?string { return $attribute === 'ou' ? $this->name : null; } }; $query = Mockery::mock(); $query->shouldReceive('select')->with(['ou', 'distinguishedname'])->andReturnSelf(); $query->shouldReceive('get')->twice()->andReturn([ $userOu, $nestedUserOu, $computerOu, $computerChild, $externalOu, ]); Mockery::mock('alias:\LdapRecord\Models\ActiveDirectory\OrganizationalUnit') ->shouldReceive('query')->twice()->andReturn($query); $service = new OuTreeService; $userTree = $service->tree('users'); $computerTree = $service->tree('computers'); $this->assertCount(1, $userTree); $this->assertSame('oddzial', $userTree[0]['name']); $this->assertCount(1, $userTree[0]['children']); $this->assertSame('Users', $userTree[0]['children'][0]['name']); $this->assertCount(1, $computerTree); $this->assertSame('Komputery', $computerTree[0]['name']); $this->assertCount(1, $computerTree[0]['children']); $this->assertSame('Workstations', $computerTree[0]['children'][0]['name']); Mockery::close(); } #[Test] public function it_counts_actual_users_inside_each_ou_subtree(): void { config([ 'ldap.defaults.user_ou' => 'OU=Users,DC=example,DC=com', 'ldap.defaults.computer_ou' => 'OU=Komputery,DC=example,DC=com', 'ldap.defaults.groups_ou' => 'OU=Grupy,DC=example,DC=com', ]); $rootOu = new class('OU=Users,DC=example,DC=com', 'Users') { public function __construct(public string $dn, public string $name) {} public function getDn(): string { return $this->dn; } public function getFirstAttribute(string $attribute): ?string { return $attribute === 'ou' ? $this->name : null; } }; $childOu = new class('OU=Developers,OU=Users,DC=example,DC=com', 'Developers') { public function __construct(public string $dn, public string $name) {} public function getDn(): string { return $this->dn; } public function getFirstAttribute(string $attribute): ?string { return $attribute === 'ou' ? $this->name : null; } }; $ouQuery = Mockery::mock(); $ouQuery->shouldReceive('select')->with(['ou', 'distinguishedname'])->andReturnSelf(); $ouQuery->shouldReceive('get')->once()->andReturn([$rootOu, $childOu]); Mockery::mock('alias:\LdapRecord\Models\ActiveDirectory\OrganizationalUnit') ->shouldReceive('query')->once()->andReturn($ouQuery); // countObjectsInOu() zwraca liczbę obiektów w CAŁYM poddrzewie danego OU // (zapytanie rekursywne). Liczba przy OU nadrzędnym NIE może być sumą // z dziećmi - inaczej te same obiekty są liczone wielokrotnie // (błąd: root pokazywał 313 zamiast 121). $service = Mockery::mock(OuTreeService::class)->makePartial()->shouldAllowMockingProtectedMethods(); $service->shouldReceive('countObjectsInOu')->with('OU=Users,DC=example,DC=com', 'users')->once()->andReturn(5); $service->shouldReceive('countObjectsInOu')->with('OU=Developers,OU=Users,DC=example,DC=com', 'users')->once()->andReturn(2); $tree = $service->tree('users'); $this->assertCount(1, $tree); $this->assertSame(5, $tree[0]['count'], 'Licznik OU nadrzędnego = liczba w całym poddrzewie, bez podwójnego sumowania.'); $this->assertSame(2, $tree[0]['children'][0]['count']); Mockery::close(); } }