|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File part of the Redmine User Provider bundle |
|
4
|
|
|
* |
|
5
|
|
|
* @category SymfonyBundle |
|
6
|
|
|
* @package GMaissa.RedmineUserProviderBundle |
|
7
|
|
|
* @author Guillaume Maïssa <[email protected]> |
|
8
|
|
|
* @copyright 2017 Guillaume Maïssa |
|
9
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace GMaissa\RedmineUserProviderBundle\Tester\Mock\ApiClient; |
|
13
|
|
|
|
|
14
|
|
|
use GMaissa\RedmineUserProviderBundle\ApiClient\RedmineApiClientInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Mock class for User provider testing |
|
18
|
|
|
* @codeCoverageIgnore |
|
19
|
|
|
*/ |
|
20
|
|
|
class RedmineApiClientMock implements RedmineApiClientInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private $connectedUser = false; |
|
26
|
|
|
|
|
27
|
|
|
private $usersData = [ |
|
28
|
|
|
'test' => [ |
|
29
|
|
|
'user' => [ |
|
30
|
|
|
'mail' => '[email protected]', |
|
31
|
|
|
'login' => 'test', |
|
32
|
|
|
'api_key' => 'api-key', |
|
33
|
|
|
'password' => 'test', |
|
34
|
|
|
'firstname' => 'Te', |
|
35
|
|
|
'lastname' => 'St' |
|
36
|
|
|
] |
|
37
|
|
|
], |
|
38
|
|
|
'test-unauthorized' => [ |
|
39
|
|
|
'user' => [ |
|
40
|
|
|
'mail' => '[email protected]', |
|
41
|
|
|
'login' => 'test-unauthorized', |
|
42
|
|
|
'api_key' => 'api-key2', |
|
43
|
|
|
'password' => 'test', |
|
44
|
|
|
'firstname' => 'Te', |
|
45
|
|
|
'lastname' => 'St 2' |
|
46
|
|
|
] |
|
47
|
|
|
] |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct(string $url) |
|
54
|
|
|
{ |
|
55
|
|
|
// No need to store the parameter which will not be used |
|
56
|
|
|
unset($url); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Set the Redmine client Class |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $clientClass |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setClientClass(string $clientClass) |
|
65
|
|
|
{ |
|
66
|
|
|
// Do nothing |
|
67
|
|
|
unset($clientClass); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function connect(string $login, string $password) |
|
74
|
|
|
{ |
|
75
|
|
|
if (isset($this->usersData[$login]) && $password == $this->usersData[$login]['user']['password']) { |
|
76
|
|
|
$this->connectedUser = $login; |
|
77
|
|
|
} else { |
|
78
|
|
|
throw new \Exception('Invalid credentials'); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function api(string $apiName) |
|
86
|
|
|
{ |
|
87
|
|
|
if (!$this->connectedUser) { |
|
88
|
|
|
throw new \Exception('Connection to server not instantiated'); |
|
89
|
|
|
} |
|
90
|
|
|
$this->apiName = $apiName; |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Retrieve current connected user |
|
97
|
|
|
* |
|
98
|
|
|
* @return mixed|null |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getCurrentUser() |
|
101
|
|
|
{ |
|
102
|
|
|
return (isset($this->usersData[$this->connectedUser])) ? $this->usersData[$this->connectedUser] : null; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: