summaryrefslogtreecommitdiff
path: root/tests/Unit/Models/User/UserTest.php
blob: 478c02ddd86cfcacc50244369ae1e69adf1dbb96 (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
<?php

namespace Engelsystem\Test\Unit\Models;

use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use Engelsystem\Models\User\Contact;
use Engelsystem\Models\User\HasUserModel;
use Engelsystem\Models\User\PersonalData;
use Engelsystem\Models\User\Settings;
use Engelsystem\Models\User\State;
use Engelsystem\Models\User\User;
use Engelsystem\Test\Unit\HasDatabase;
use PHPUnit\Framework\TestCase;

class UserTest extends TestCase
{
    use ArraySubsetAsserts;
    use HasDatabase;

    protected $data = [
        'name'     => 'lorem',
        'password' => '',
        'email'    => 'foo@bar.batz',
        'api_key'  => '',
    ];

    /**
     * @return array
     */
    public function hasOneRelationsProvider()
    {
        return [
            [
                Contact::class,
                'contact',
                [
                    'dect'   => '1234567',
                    'email'  => 'foo@bar.batz',
                    'mobile' => '1234/12341234',
                ]
            ],
            [
                PersonalData::class,
                'personalData',
                [
                    'first_name' => 'Foo'
                ]
            ],
            [
                Settings::class,
                'settings',
                [
                    'language' => 'de_DE',
                    'theme'    => 4,
                ]
            ],
            [
                State::class,
                'state',
                [
                    'force_active' => true,
                ]
            ],
        ];
    }

    /**
     * @covers       \Engelsystem\Models\User\User::contact
     * @covers       \Engelsystem\Models\User\User::personalData
     * @covers       \Engelsystem\Models\User\User::settings
     * @covers       \Engelsystem\Models\User\User::state
     *
     * @dataProvider hasOneRelationsProvider
     *
     * @param string $class
     * @param string $name
     * @param array  $data
     */
    public function testHasOneRelations($class, $name, $data)
    {
        $user = new User($this->data);
        $user->save();

        /** @var HasUserModel $contact */
        $contact = new $class($data);
        $contact->user()
            ->associate($user)
            ->save();

        $this->assertArraySubset($data, (array)$user->{$name}->attributesToArray());
    }

    /**
     * Prepare test
     */
    protected function setUp(): void
    {
        $this->initDatabase();
    }
}