Completed
Push — master ( 7700d1...23cd67 )
by Mahmoud
03:21
created

TestingUserTrait::getLoggedInTestingUserToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace App\Port\Test\PHPUnit\Traits;
4
5
use App;
6
use App\Containers\Authentication\Tasks\ApiLoginThisUserObjectTask;
7
use App\Containers\User\Models\User;
8
use Artisan;
9
use Dingo\Api\Http\Response as DingoAPIResponse;
10
use Illuminate\Support\Facades\Hash;
11
12
/**
13
 * Class TestingUserTrait.
14
 *
15
 * Tests helpers for the testing user.
16
 *
17
 * @author  Mahmoud Zalt <[email protected]>
18
 */
19
trait TestingUserTrait
20
{
21
22
    /**
23
     * the Logged in user (client).
24
     *
25
     * @var User
26
     */
27
    public $loggedInTestingUser;
28
29
    /**
30
     * the Logged in admin user.
31
     *
32
     * @var User
33
     */
34
    public $loggedInTestingAdmin;
35
36
37
    /**
38
     * get teh current logged in user OR create new one if no one exist
39
     *
40
     * @param null $access
41
     *
42
     * @return  \App\Containers\User\Models\User|mixed
43
     */
44
    public function getTestingUser($access = null)
45
    {
46
        if (!$user = $this->loggedInTestingUser) {
47
            $user = $this->createTestingUser($access);
48
        }
49
50
        return $user;
51
    }
52
53
    /**
54
     * @return  \App\Containers\User\Models\User|mixed
55
     */
56
    public function getTestingUserWithoutPermissions()
57
    {
58
        if (!$user = $this->loggedInTestingUser) {
59
            $user = $this->getTestingUser(['permissions' => null, 'roles' => null]);
60
        }
61
62
        return $user;
63
    }
64
65
    /**
66
     * @param null $permissions
67
     *
68
     * @return  \App\Containers\User\Models\User|mixed
69
     */
70
    public function getTestingAdmin($permissions = null)
71
    {
72
        if (!$admin = $this->loggedInTestingAdmin) {
73
            $admin = $this->loggedInTestingAdmin = $this->createTestingUser([
74
                'roles'       => 'admin',
75
                'permissions' => $permissions,
76
            ]);
77
        }
78
79
        return $admin;
80
    }
81
82
    /**
83
     * get teh current logged in user token.
84
     *
85
     * @return string
86
     */
87
    public function getLoggedInTestingUserToken()
88
    {
89
        return $this->getTestingUser()->token;
90
    }
91
92
    /**
93
     * @param null $access
94
     * @param null $userDetails
95
     *
96
     * @return  mixed
97
     */
98
    public function createTestingUser($access = null, $userDetails = null)
99
    {
100
        // if no user detail provided, use the default details.
101
        $userDetails = $userDetails ? : [
102
            'name'     => 'Testing User',
103
            'email'    => $this->faker->email,
0 ignored issues
show
Bug introduced by
The property faker does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
104
            'password' => 'testing-pass',
105
        ];
106
107
        // create new user and login
108
        $user = factory(User::class)->create([
109
            'email'    => $userDetails['email'],
110
            'password' => Hash::make($userDetails['password']),
111
            'name'     => $userDetails['name'],
112
        ]);
113
114
        // assign roles and permissions
115
        $user = $this->setupTestingUserAccess($user, $access ? : (isset($this->access) ? $this->access : null));
0 ignored issues
show
Bug introduced by
The property access does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
116
117
        // log the user in
118
        $user = App::make(ApiLoginThisUserObjectTask::class)->run($user);
119
120
        return $this->loggedInTestingUser = $user;
121
    }
122
123
    /**
124
     * @param $user
125
     * @param $access
126
     *
127
     * @return  mixed
128
     */
129
    private function setupTestingUserAccess($user, $access)
130
    {
131
        if (isset($access['permissions']) && !empty($access['permissions'])) {
132
            $user->givePermissionTo($access['permissions']);
133
        }
134
        if (isset($access['roles']) && !empty($access['roles'])) {
135
            if (!$user->hasRole($access['roles'])) {
136
                $user->assignRole($access['roles']);
137
            }
138
        }
139
140
        return $user;
141
    }
142
143
}
144