Completed
Push — master ( db3c82...df4949 )
by Joschi
03:02
created

AbstractTestBase::createAntibot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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 © 2018 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 © 2018 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\Ports\Antibot;
40
use Nyholm\Psr7\Factory\Psr17Factory;
41
use Nyholm\Psr7Server\ServerRequestCreator;
42
use PHPUnit\Framework\TestCase;
43
use Psr\Http\Message\ServerRequestInterface;
44
45
/**
46
 * Abstract PHPUnit Test Base
47
 *
48
 * @package    Jkphl\Antibot
49
 * @subpackage Jkphl\Antibot\Tests
50
 */
51
class AbstractTestBase extends TestCase
52
{
53
    /**
54
     * Create and return a server request
55
     *
56
     * @param array $server $_SERVER data
57
     * @param array $get    $_GET data
58
     * @param array $post   $_POST data
59
     *
60
     * @return ServerRequestInterface Server request
61
     */
62
    protected function createRequest(array $server, array $get = [], array $post = []): ServerRequestInterface
63
    {
64
        $psr17Factory = new Psr17Factory();
65
        $creator      = new ServerRequestCreator(
66
            $psr17Factory, // ServerRequestFactory
67
            $psr17Factory, // UriFactory
68
            $psr17Factory, // UploadedFileFactory
69
            $psr17Factory  // StreamFactory
70
        );
71
72
        return $creator->fromArrays(
73
            $server, // $_SERVER
74
            [], // Headers
75
            [], // Cookies
76
            $get, // GET
77
            $post, // POST
78
            [], // FILES
79
            null // Body
80
        );
81
    }
82
83
    /**
84
     * Create an Antibot instance
85
     *
86
     * @param string|null $session Session-persistent unique ID
87
     *
88
     * @return Antibot Antibot instance
89
     */
90
    protected function createAntibot(string &$session = null): Antibot
91
    {
92
        if ($session === null) {
93
            $session = md5(rand());
94
        }
95
96
        return new Antibot([], $session);
97
    }
98
}
99