Passed
Push — master ( 1dc943...5f0a93 )
by Arthur
08:24 queued 35s
created

HelpersTest::testGetClassConstants()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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 Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
18
19
class HelpersTest extends TestCase
20
{
21
    const TEST_CONSTANT = 'test';
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
        $this->assertTrue(array_is_subset_of($keyValueSubset, $keyValueArray));
143
        $this->assertFalse(array_is_subset_of($keyValueInvalidSubset, $keyValueArray));
144
        $this->assertFalse(array_is_subset_of($keyValueInvalidSubset2, $keyValueArray));
145
146
        $this->assertTrue(array_is_subset_of($valueSubset, $valueArray));
147
        $this->assertTrue(array_is_subset_of($valueSubset2, $valueArray));
148
        $this->assertFalse(array_is_subset_of($invalidValueSubset, $valueArray));
149
        $this->assertFalse(array_is_subset_of($invalidValueSubset2, $valueArray));
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
    public function testGetClassPropertyHelper()
175
    {
176
        $this->assertEquals($this->randomTestVariable, get_class_property(static::class, 'randomTestVariable'));
177
    }
178
179
    public function testGetClassConstants()
180
    {
181
        $this->assertArrayHasKey('TEST_CONSTANT', get_class_constants(static::class));
182
        $this->assertEquals(self::TEST_CONSTANT, get_class_constants(static::class)['TEST_CONSTANT']);
183
    }
184
}
185