Passed
Push — master ( e5b31a...cf340d )
by Arthur
04:48
created

HelpersTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 80
dl 0
loc 152
rs 10
c 0
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testUnauthenticatedUserHelper() 0 4 1
A testClassUsesTraitHelper() 0 4 1
A testAuthenticatedUserHelper() 0 4 1
A testIsAssocativeArrayHelper() 0 20 1
A testArrayIsSubsetOfHelper() 0 58 1
A testRandomArrayElementHelper() 0 13 1
A testClassShortNameHelper() 0 3 1
A testClassImplementsHelper() 0 6 1
A testArrayKeysExistHelper() 0 21 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 03.10.18
6
 * Time: 22:58.
7
 */
8
9
namespace Foundation\Tests;
10
11
use Foundation\Abstracts\Tests\CreatesApplication;
12
use Foundation\Abstracts\Tests\TestCase;
13
use Illuminate\Contracts\Auth\Authenticatable;
14
use Illuminate\Contracts\Filesystem\Filesystem;
15
use Illuminate\Foundation\Auth\Access\Authorizable;
16
use Modules\User\Entities\User;
17
use PHPUnit\Framework\Constraint\ArraySubset;
18
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
19
20
class HelpersTest extends TestCase
21
{
22
    public function testClassImplementsHelper()
23
    {
24
        $this->assertFalse(class_implements_interface(self::class, Filesystem::class));
25
        $this->assertTrue(class_implements_interface(self::class, \PHPUnit\Framework\Test::class));
26
        $this->assertFalse(class_implements_interface(new self(), Filesystem::class));
27
        $this->assertTrue(class_implements_interface(new self(), \PHPUnit\Framework\Test::class));
28
    }
29
30
    public function testUnauthenticatedUserHelper()
31
    {
32
        $this->expectException(UnauthorizedHttpException::class);
33
        get_authenticated_user();
34
    }
35
36
    public function testAuthenticatedUserHelper()
37
    {
38
        $this->actingAs(factory(User::class)->make());
0 ignored issues
show
Bug introduced by
It seems like factory(Modules\User\Ent...es\User::class)->make() can also be of type Illuminate\Database\Eloquent\Model; however, parameter $user of Illuminate\Foundation\Testing\TestCase::actingAs() does only seem to accept Illuminate\Contracts\Auth\Authenticatable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        $this->actingAs(/** @scrutinizer ignore-type */ factory(User::class)->make());
Loading history...
39
        $this->assertTrue(class_implements_interface(get_authenticated_user(), Authenticatable::class));
40
    }
41
42
    public function testClassShortNameHelper()
43
    {
44
        $this->assertEquals('HelpersTest', get_short_class_name(self::class));
45
    }
46
47
    public function testRandomArrayElementHelper()
48
    {
49
        $array = [
50
            'test',
51
            'x',
52
            'blabla',
53
            'hello',
54
            'hey',
55
        ];
56
57
        $randomArrayElement = get_random_array_element($array);
58
59
        $this->assertContains($randomArrayElement, $array);
60
    }
61
62
    public function testClassUsesTraitHelper()
63
    {
64
        $this->assertTrue(class_uses_trait(self::class, CreatesApplication::class));
65
        $this->assertFalse(class_uses_trait(self::class, Authorizable::class));
66
    }
67
68
    public function testArrayKeysExistHelper()
69
    {
70
        $requiredKeys = [
71
            "input1",
72
            "input3",
73
            "input4"
74
        ];
75
76
        $invalidArray = [
77
            "input1" => 5,
78
            "input3" => 4,
79
            "input5" => 3
80
        ];
81
82
        $validArray = [
83
            "input1" => 5,
84
            "input3" => 4,
85
            "input4" => 3
86
        ];
87
        $this->assertTrue(array_keys_exists($requiredKeys, $validArray));
88
        $this->assertFalse(array_keys_exists($requiredKeys, $invalidArray));
89
    }
90
91
    public function testArrayIsSubsetOfHelper()
92
    {
93
        $keyValueArray = [
94
            "input1" => 5,
95
            "input3" => 4,
96
            "input4" => 3
97
        ];
98
99
        $valueArray = [
100
            "value1",
101
            "value2",
102
            "value4"
103
        ];
104
105
        $keyValueSubset = [
106
            "input1" => 5,
107
            "input3" => 4
108
        ];
109
110
        $keyValueInvalidSubset = [
111
            "input1" => 4,
112
            "input3" => 4
113
        ];
114
115
        $keyValueInvalidSubset2 = [
116
            "input2" => 5,
117
        ];
118
119
        $valueSubset = [
120
            "value2",
121
            "value4"
122
        ];
123
124
        $valueSubset2 = [
125
            "value1"
126
        ];
127
128
        $invalidValueSubset = [
129
            "value5"
130
        ];
131
132
        $invalidValueSubset2 = [
133
            "value1",
134
            "value2",
135
            "value3",
136
            "value4"
137
        ];
138
139
140
        $this->assertTrue(array_is_subset_of($keyValueSubset, $keyValueArray));
141
        $this->assertFalse(array_is_subset_of($keyValueInvalidSubset, $keyValueArray));
142
        $this->assertFalse(array_is_subset_of($keyValueInvalidSubset2, $keyValueArray));
143
144
145
        $this->assertTrue(array_is_subset_of($valueSubset, $valueArray));
146
        $this->assertTrue(array_is_subset_of($valueSubset2, $valueArray));
147
        $this->assertFalse(array_is_subset_of($invalidValueSubset, $valueArray));
148
        $this->assertFalse(array_is_subset_of($invalidValueSubset2, $valueArray));
149
150
    }
151
152
    public function testIsAssocativeArrayHelper()
153
    {
154
        $associativeArray = [
155
            "key1" => "value1",
156
            "key3" => "value3"
157
        ];
158
159
        $array = [
160
            0, 1, 2, 3
161
        ];
162
163
        $mixedArray = [
164
            "key1" => "value1",
165
            "randomvalue",
166
            "key3" => "value3"
167
        ];
168
169
        $this->assertTrue(is_associative_array($associativeArray));
170
        $this->assertTrue(is_associative_array($mixedArray));
171
        $this->assertFalse(is_associative_array($array));
172
    }
173
}
174