FOSSology  4.4.0
Open Source License Compliance by Open Source Software
UserGroupMemberTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2024 Valens Niyonsenga <valensniyonsenga2003@gmail.com>
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
12 
19 use Monolog\Test\TestCase;
20 use Mockery as M;
21 class UserGroupMemberTest extends TestCase
22 {
23 
24  private $dbHelper;
25  private $restHelper;
26  private $userDao;
27  protected function setup(): void
28  {
29  global $container;
30  $container = M::mock('ContainerBuilder');
31  $this->dbHelper = M::mock(DbHelper::class);
32  $this->restHelper = M::mock(RestHelper::class);
33  $this->userDao = M::mock(UserDao::class);
34 
35  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
36 
37  $container->shouldReceive('get')->withArgs(array(
38  'helper.restHelper'))->andReturn($this->restHelper);
39  $this->restHelper->shouldReceive('getUserDao')->andReturn($this->userDao);
40  }
48  private function getUserGroupMemberInfo($version = ApiVersion::V2)
49  {
50 
51  $user = new User(1, "fossy", "Admin user", "fossy@gmail.com", "admin", 4, "fossy@gmail.com", "monk", 3, null);
52  if ($version == ApiVersion::V1) {
53  $expectedArray = [
54  'user' => $user->getArray($version),
55  'group_perm' => 0
56  ];
57  } else {
58  $expectedArray = [
59  'user' => $user->getArray($version),
60  'groupPerm' => 0
61  ];
62  }
63 
64  $obj = new UserGroupMember($user, $expectedArray['group_perm']);
65  return [
66  'expectedArray' => $expectedArray,
67  'obj' => $obj
68  ];
69  }
70 
71  // Getter Tests
72  public function testGetUser()
73  {
74  $user = new User(1, "fossy", "Admin user", "fossy@gmail.com", "admin", 4, "fossy@gmail.com", "monk", 3, null);
75  $userGroupMember = new UserGroupMember($user, 3);
76  $this->assertEquals($user, $userGroupMember->getUser());
77  }
78 
79  public function testGetGroupPerm()
80  {
81  $user = new User(1, "fossy", "Admin user", "fossy@gmail.com", "admin", 4, "fossy@gmail.com", "monk", 3, null);
82  $userGroupMember = new UserGroupMember($user, 3);
83  $this->assertEquals(3, $userGroupMember->getGroupPerm());
84  }
85 
86  // Setter Tests
87  public function testSetUser()
88  {
89  $user1 = new User(1, "fossy", "Admin user", "fossy@gmail.com", "admin", 4, "fossy@gmail.com", 'monk', 3, null);
90  $user2 = new User(2, "newuser", "New user", "newuser@gmail.com", "user", 5, "newuser@gmail.com", 'monk', 4, null);
91  $userGroupMember = new UserGroupMember($user1, 3);
92 
93  $userGroupMember->setUser($user2);
94  $this->assertEquals($user2, $userGroupMember->getUser());
95  }
96 
97  public function testSetGroupPerm()
98  {
99  $user = new User(1, "fossy", "Admin user", "fossy@gmail.com", "admin", 4, "fossy@gmail.com", "monk", 3, null);
100  $userGroupMember = new UserGroupMember($user, 3);
101 
102  $userGroupMember->setGroupPerm(5);
103  $this->assertEquals(5, $userGroupMember->getGroupPerm());
104  }
105 
106  // Test for JSON output
107  public function testGetJSON()
108  {
109  $data = $this->getUserGroupMemberInfo(ApiVersion::V1);
110  $this->assertJsonStringEqualsJsonString(
111  json_encode($data['expectedArray']),
112  $data['obj']->getJSON()
113  );
114  }
115 
116  // Test for Array output
117  public function testGetArrayV1()
118  {
119  $data = $this->getUserGroupMemberInfo(ApiVersion::V1);
120  $this->assertEquals($data['expectedArray'], $data['obj']->getArray(ApiVersion::V1));
121  }
122 }
Provides helper methods to access database for REST api.
Definition: DbHelper.php:38
Provides various DAO helper functions for REST api.
Definition: RestHelper.php:32
Model to hold user information.
Definition: User.php:21