|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* antibot |
|
5
|
|
|
* |
|
6
|
|
|
* @category Jkphl |
|
7
|
|
|
* @package Jkphl\Antibot |
|
8
|
|
|
* @subpackage Jkphl\Antibot\Tests |
|
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
|
10
|
|
|
* @copyright Copyright © 2020 Joschi Kuphal <[email protected]> / @jkphl |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/*********************************************************************************** |
|
15
|
|
|
* The MIT License (MIT) |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright © 2020 Joschi Kuphal <[email protected]> |
|
18
|
|
|
* |
|
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
21
|
|
|
* the Software without restriction, including without limitation the rights to |
|
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
|
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
|
24
|
|
|
* subject to the following conditions: |
|
25
|
|
|
* |
|
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
27
|
|
|
* copies or substantial portions of the Software. |
|
28
|
|
|
* |
|
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
|
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
|
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
35
|
|
|
***********************************************************************************/ |
|
36
|
|
|
|
|
37
|
|
|
namespace Jkphl\Antibot\Tests; |
|
38
|
|
|
|
|
39
|
|
|
use Jkphl\Antibot\Infrastructure\Model\InputElement; |
|
40
|
|
|
use Jkphl\Antibot\Ports\Antibot; |
|
41
|
|
|
use Monolog\Handler\StreamHandler; |
|
42
|
|
|
use Monolog\Logger; |
|
43
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
|
44
|
|
|
use Nyholm\Psr7Server\ServerRequestCreator; |
|
45
|
|
|
use PHPUnit\Framework\TestCase; |
|
46
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Abstract PHPUnit Test Base |
|
50
|
|
|
* |
|
51
|
|
|
* @package Jkphl\Antibot |
|
52
|
|
|
* @subpackage Jkphl\Antibot\Tests |
|
53
|
|
|
*/ |
|
54
|
|
|
class AbstractTestBase extends TestCase |
|
55
|
|
|
{ |
|
56
|
|
|
/** |
|
57
|
|
|
* Create and return a server request |
|
58
|
|
|
* |
|
59
|
|
|
* @param array $server $_SERVER data |
|
60
|
|
|
* @param array $get $_GET data |
|
61
|
|
|
* @param array $post $_POST data |
|
62
|
|
|
* |
|
63
|
|
|
* @return ServerRequestInterface Server request |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function createRequest(array $server, array $get = [], array $post = []): ServerRequestInterface |
|
66
|
|
|
{ |
|
67
|
|
|
$psr17Factory = new Psr17Factory(); |
|
68
|
|
|
$creator = new ServerRequestCreator( |
|
69
|
|
|
$psr17Factory, // ServerRequestFactory |
|
70
|
|
|
$psr17Factory, // UriFactory |
|
71
|
|
|
$psr17Factory, // UploadedFileFactory |
|
72
|
|
|
$psr17Factory // StreamFactory |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
return $creator->fromArrays( |
|
76
|
|
|
$server, // $_SERVER |
|
77
|
|
|
[], // Headers |
|
78
|
|
|
[], // Cookies |
|
79
|
|
|
$get, // GET |
|
80
|
|
|
$post, // POST |
|
81
|
|
|
[], // FILES |
|
82
|
|
|
null // Body |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Create an Antibot instance |
|
88
|
|
|
* |
|
89
|
|
|
* @param string|null $session Session-persistent unique ID |
|
90
|
|
|
* |
|
91
|
|
|
* @return Antibot Antibot instance |
|
92
|
|
|
* @throws \Exception |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function createAntibot(string &$session = null): Antibot |
|
95
|
|
|
{ |
|
96
|
|
|
if ($session === null) { |
|
97
|
|
|
$session = md5(rand()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$antibot = new Antibot($session, Antibot::DEFAULT_PREFIX); |
|
101
|
|
|
|
|
102
|
|
|
$log = new Logger('ANTIBOT'); |
|
103
|
|
|
$log->pushHandler(new StreamHandler('php://stdout')); |
|
104
|
|
|
$antibot->setLogger($log); |
|
105
|
|
|
|
|
106
|
|
|
return $antibot; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Translate armor input elements to GET / POST parameters |
|
111
|
|
|
* |
|
112
|
|
|
* @param array $armor Armor input elements |
|
113
|
|
|
* |
|
114
|
|
|
* @return array GET / POST Parameters |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function getArmorParams(array $armor): array |
|
117
|
|
|
{ |
|
118
|
|
|
// Prepare the second call |
|
119
|
|
|
$params = []; |
|
120
|
|
|
/** @var InputElement $input */ |
|
121
|
|
|
foreach ($armor as $input) { |
|
122
|
|
|
$inputAttrs = $input->getAttributes(); |
|
123
|
|
|
$params[$inputAttrs['name']] = $inputAttrs['value']; |
|
124
|
|
|
} |
|
125
|
|
|
parse_str(http_build_query($params), $params); |
|
126
|
|
|
|
|
127
|
|
|
return $params; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|