|
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; |
|
|
|
|
|
|
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]); |
|
|
|
|
|
|
54
|
|
|
$this->seeInDatabase('users', ['platform' => $visitorUser->platform]); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
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:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.