Passed
Push — master ( cf340d...4e06a8 )
by Arthur
06:10
created

HelpersTest::testArrayKeysExistHelper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 21
rs 9.7998
c 0
b 0
f 0
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
23
    private $randomTestVariable = "blablabla";
24
25
    public function testClassImplementsHelper()
26
    {
27
        $this->assertFalse(class_implements_interface(self::class, Filesystem::class));
28
        $this->assertTrue(class_implements_interface(self::class, \PHPUnit\Framework\Test::class));
29
        $this->assertFalse(class_implements_interface(new self(), Filesystem::class));
30
        $this->assertTrue(class_implements_interface(new self(), \PHPUnit\Framework\Test::class));
31
    }
32
33
    public function testUnauthenticatedUserHelper()
34
    {
35
        $this->expectException(UnauthorizedHttpException::class);
36
        get_authenticated_user();
37
    }
38
39
    public function testAuthenticatedUserHelper()
40
    {
41
        $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

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