summaryrefslogtreecommitdiff
path: root/tests/Unit/Models/Auth/AuthModelTest.php
blob: 754e120a100ae29a65cd9e16dec7b89a24af16d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php

namespace Engelsystem\Test\Unit\Models\Auth;

use Engelsystem\Models\Auth\Permission;
use Engelsystem\Models\Auth\Role;
use Engelsystem\Models\BaseModel;
use Engelsystem\Models\Team;
use Engelsystem\Models\User\User;
use Engelsystem\Test\Unit\Models\ModelTest;

abstract class AuthModelTest extends ModelTest
{
    /** @var array */
    protected $user = [
        'name'     => 'Test User',
        'password' => 'nope',
        'email'    => 'foo@bar.batz',
        'api_key'  => 'Test123',
    ];

    /** @var array */
    protected $team = [
        'name' => 'Test Team',
    ];

    /** @var array */
    protected $role = [
        'name' => 'Test Role',
    ];

    /** @var array */
    protected $permission = [
        'name' => 'foo.bar',
    ];

    /**
     * @param array $data
     * @return Permission
     */
    protected function getPermission($data = [])
    {
        /** @var Permission $model */
        $model = $this->getModel(Permission::class, array_merge($this->permission, $data));

        return $model;
    }

    /**
     * @param Permission|Permission[] $permissions
     * @param array                   $data
     * @return Role
     */
    protected function getRole($permissions = [], $data = [])
    {
        /** @var Role $model */
        $model = $this->getModel(Role::class, array_merge($this->role, $data), ['permissions' => $permissions]);

        return $model;
    }

    /**
     * @param Role|Role[] $roles
     * @param array       $data
     * @return Team
     */
    protected function getTeam($roles = [], $data = [])
    {
        /** @var Team $model */
        $model = $this->getModel(Team::class, array_merge($this->team, $data), ['roles' => $roles]);

        return $model;
    }

    /**
     * @param Team|Team[] $teams
     * @param array       $data
     * @return User
     */
    protected function getUser($teams = [], $data = [])
    {
        /** @var User $model */
        $model = $this->getModel(User::class, array_merge($this->user, $data), ['teams' => $teams]);

        return $model;
    }

    /**
     * @param string $class BaseModel implementation name
     * @param array  $data
     * @param array  $attachments
     * @return BaseModel
     */
    protected function getModel($class, $data = [], $attachments = [])
    {
        /** @var BaseModel $instance */
        $instance = new $class($data);
        $instance->save();

        foreach ($attachments as $type => $attachment) {
            $objects = is_object($attachment) ? [$attachment] : $attachment;
            foreach ($objects as $object) {
                $instance->$type()->attach($object);
            }
        }

        return $instance;
    }
}