1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Created by PhpStorm. |
6
|
|
|
* User: benedikt |
7
|
|
|
* Date: 9/16/17 |
8
|
|
|
* Time: 2:04 PM |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Tfboe\FmLib\TestHelpers; |
12
|
|
|
|
13
|
|
|
use Doctrine\DBAL\Connection; |
14
|
|
|
use Illuminate\Support\Facades\Auth; |
15
|
|
|
use LaravelDoctrine\ORM\Facades\EntityManager; |
16
|
|
|
use Tfboe\FmLib\Entity\UserInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class AuthenticatedTestCase |
20
|
|
|
* @package Tfboe\FmLib\TestHelpers |
21
|
|
|
*/ |
22
|
|
|
abstract class AuthenticatedTestCase extends DatabaseTestCase |
23
|
|
|
{ |
24
|
|
|
//<editor-fold desc="Fields"> |
25
|
|
|
/** |
26
|
|
|
* Authentication token if logged in |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $token; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* User corresponding to authentication token if logged in |
33
|
|
|
* @var UserInterface |
34
|
|
|
*/ |
35
|
|
|
protected $user; |
36
|
|
|
//</editor-fold desc="Fields"> |
37
|
|
|
|
38
|
|
|
//<editor-fold desc="Protected Methods"> |
39
|
|
|
/** |
40
|
|
|
* sends a json request with an authentication token |
41
|
|
|
* @param string $method the method to use (GET, POST, ...) |
42
|
|
|
* @param string $uri the uri of the request |
43
|
|
|
* @param array $data the post data |
44
|
|
|
* @param array $headers the request headers |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
protected function jsonAuth(string $method, string $uri, array $data = [], array $headers = []) |
48
|
|
|
{ |
49
|
|
|
$headers['Authorization'] = 'Bearer ' . $this->token; |
50
|
|
|
return $this->json($method, $uri, $data, $headers); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function workOnDatabaseDestroy() |
54
|
|
|
{ |
55
|
|
|
$this->clearUsers(); |
56
|
|
|
parent::workOnDatabaseDestroy(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function getUserDb() |
60
|
|
|
{ |
61
|
|
|
return "users"; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function workOnDatabaseSetUp() |
65
|
|
|
{ |
66
|
|
|
$this->clearUsers(); |
67
|
|
|
parent::workOnDatabaseSetUp(); |
68
|
|
|
|
69
|
|
|
$userInfo = $this->createUser(); |
70
|
|
|
$this->user = $userInfo['user']; |
71
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
72
|
|
|
$this->token = Auth::attempt(['email' => $this->user->getEmail(), 'password' => $userInfo['password']]); |
|
|
|
|
73
|
|
|
$this->refreshApplication(); |
74
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
75
|
|
|
$this->user = EntityManager::find(UserInterface::class, $this->user->getId()); |
76
|
|
|
} |
77
|
|
|
//</editor-fold desc="Protected Methods"> |
78
|
|
|
|
79
|
|
|
//<editor-fold desc="Private Methods"> |
80
|
|
|
private function clearUsers() |
81
|
|
|
{ |
82
|
|
|
/** @var Connection $connection */ |
83
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
84
|
|
|
$connection = EntityManager::getConnection(); |
85
|
|
|
$sql = sprintf('SET FOREIGN_KEY_CHECKS=0;TRUNCATE TABLE %s;SET FOREIGN_KEY_CHECKS=1;', $this->getUserDb()); |
86
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
87
|
|
|
$connection->query($sql); |
88
|
|
|
} |
89
|
|
|
//</editor-fold desc="Private Methods"> |
90
|
|
|
} |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.