Completed
Push — master ( fcc0ff...8f456d )
by Mahmoud
03:36
created

SwitchVisitorToUserTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 46
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B testRegisterExistingVisitorUser_() 0 38 1
1
<?php
2
3
namespace App\Containers\User\UI\API\Tests\Functional;
4
5
use App\Containers\User\Models\User;
6
use App\Port\Tests\PHPUnit\Abstracts\TestCase;
7
8
/**
9
 * Class SwitchVisitorToUserTest.
10
 *
11
 * @author Mahmoud Zalt <[email protected]>
12
 */
13
class SwitchVisitorToUserTest extends TestCase
14
{
15
16
    private $endpoint = '/register/visitor';
17
18
    public function testRegisterExistingVisitorUser_()
19
    {
20
        $data = [
21
            'email'    => '[email protected]',
22
            'password' => 'first#Password',
23
            'name'     => 'First time name',
24
        ];
25
26
        $visitorId = str_random(40);
27
28
        $visitorUser = User::create([
29
            'visitor_id' => $visitorId,
30
            'device'   => 'Android',
31
            'platform' => 'Google Super OS',
32
        ]);
33
34
        $headers['Visitor-Id'] = $visitorId;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$headers was never initialized. Although not strictly required by PHP, it is generally a good practice to add $headers = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
35
36
        // send the HTTP request
37
        $response = $this->apiCall($this->endpoint, 'post', $data, false, $headers);
38
39
        // assert response status is correct
40
        $this->assertEquals($response->getStatusCode(), '200');
41
42
        $this->assertResponseContainKeyValue([
43
            'email' => $data['email'],
44
            'name'  => $data['name'],
45
        ], $response);
46
47
        // assert response contain the token
48
        $this->assertResponseContainKeys(['id', 'token'], $response);
49
50
        // assert the data is stored in the database
51
        $this->seeInDatabase('users', ['email' => $data['email']]);
52
        $this->seeInDatabase('users', ['visitor_id' => $visitorId]);
53
        $this->seeInDatabase('users', ['device' => $visitorUser->device]);
0 ignored issues
show
Documentation introduced by
The property device does not exist on object<App\Containers\User\Models\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
54
        $this->seeInDatabase('users', ['platform' => $visitorUser->platform]);
0 ignored issues
show
Documentation introduced by
The property platform does not exist on object<App\Containers\User\Models\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
55
    }
56
57
58
}
59