UserAPITest::testUserInDatabaseAreListedByAPI()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
use App\User;
4
use Chrisbjr\ApiGuard\Models\ApiKey;
5
use Illuminate\Foundation\Testing\DatabaseMigrations;
6
7
class UserAPITest extends TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    use DatabaseMigrations;
10
11
    /**
12
     * Create fake user.
13
     *
14
     * @return mixed
15
     */
16
    public function createUser()
17
    {
18
        $user = factory(User::class)->create();
19
        $this->createUserApiKey($user);
0 ignored issues
show
Documentation introduced by
$user is of type object<Illuminate\Databa...se\Eloquent\Collection>, but the function expects a object<App\User>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
20
21
        return $user;
22
    }
23
24
    /**
25
     * @param User $user
26
     *
27
     * @return mixed
28
     */
29
    private function createUserApiKey(User $user)
30
    {
31
        $apiKey = ApiKey::make($user->id);
32
        $user->apiKey()->save($apiKey);
33
    }
34
35
    /**
36
     * Test user in database are listed by API.
37
     *
38
     * @return void
39
     */
40
    public function testUserInDatabaseAreListedByAPI()
41
    {
42
        $user = $this->createUser();
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43
        $this->get('/api/users/')
44
            ->seeJsonStructure([
45
                '*' => [
46
                    '*' => [
47
                        'id', 'name', 'email', 'avatar',
48
                    ],
49
                ],
50
            ])->seeStatusCode(200);
51
    }
52
53
    /**
54
     * Test user is an api then returns JSON.
55
     *
56
     * @return void
57
     */
58
    public function testCreateUserAndShowUseJson()
59
    {
60
        $user = $this->createUser();
61
        $this->get('/api/users/'.$user->id)->seeJson()->seeStatusCode(200);
62
    }
63
64
    /**
65
     * Test upate user and see in DB.
66
     *
67
     * @return void
68
     */
69
    public function testCanBeUpdateAndSeeInDB()
70
    {
71
        $user = $this->createUser();
72
        $file = storage_path('app/public/images/avatars/demo.jpg');
73
        $image = new Symfony\Component\HttpFoundation\File\UploadedFile(
74
            $file,
75
            'demo.jpg',
76
            'image/jpg',
77
            null,
78
            null,
79
            true
80
        );
81
82
        $data = ['id' => $user->id, 'name' => 'Test User', 'email' => '[email protected]', 'password' => '123456', 'avatar' => $image];
83
        $this->post('/api/users/'.$user->id, $data, ['X-Authorization' => $user->apiKey->key]);
84
        $user = User::find($user->id);
85
        $this->assertEquals('Test User', $user->name);
86
        $this->assertEquals('[email protected]', $user->email);
87
        $this->assertEquals('/storage/images/avatars/'.$user->id.'.jpg', $user->avatar);
88
        $this->get('/api/users')->seeJsonContains(['id' => $user->id, 'name' => 'Test User', 'email' => '[email protected]', 'avatar' => '/storage/images/avatars/'.$user->id.'.jpg'])->seeStatusCode(200);
89
    }
90
91
    /**
92
     * Test upate user without avatar and see in DB.
93
     *
94
     * @return void
95
     */
96
    public function testCanBeUpdateWithoutAvatarAndSeeInDB()
97
    {
98
        $user = $this->createUser();
99
100
        $data = ['id' => $user->id, 'name' => 'Test User', 'email' => '[email protected]', 'password' => '123456'];
101
        $this->post('/api/users/'.$user->id, $data, ['X-Authorization' => $user->apiKey->key]);
102
        $user = User::find($user->id);
103
        $this->assertEquals('Test User', $user->name);
104
        $this->assertEquals('[email protected]', $user->email);
105
        $this->assertEquals('/img/user2-160x160.png', $user->avatar);
106
        $this->get('/api/users')->seeJsonContains(['id' => $user->id, 'name' => 'Test User', 'email' => '[email protected]', 'avatar' => '/img/user2-160x160.png'])->seeStatusCode(200);
107
    }
108
109
    /**
110
     * Test delete user and  not see in DB.
111
     *
112
     * @return void
113
     */
114 View Code Duplication
    public function testCanBeDeleteAndNotSeeInDB()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    {
116
        $user = $this->createUser();
117
        $data = ['name' => $user->name, 'email' => $user->email, 'avatar' => $user->avatar];
118
119
        $this->delete('/api/users/'.$user->id, ['X-Authorization' => $user->apiKey->key])->notSeeInDatabase('users', $data);
120
        $this->get('/api/users')->dontSeeJson($data)->seeStatusCode(200);
121
    }
122
}
123