forked from xibosignage/xibo-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmdsTestCase.php
180 lines (159 loc) · 4.95 KB
/
XmdsTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/*
* Copyright (C) 2023 Xibo Signage Ltd
*
* Xibo - Digital Signage - https://xibosignage.com
*
* This file is part of Xibo.
*
* Xibo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Xibo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Xibo\Tests;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
class xmdsTestCase extends TestCase
{
/** @var ContainerInterface */
public static $container;
/** @var LoggerInterface */
public static $logger;
/** @var Client */
public $client;
/**
* @inheritDoc
*/
public function getGuzzleClient(array $requestOptions = []): Client
{
if ($this->client === null) {
$this->client = new Client($requestOptions);
}
return $this->client;
}
/**
* @param string $method
* @param string $body
* @param string $path
* @param array $headers
* @return ResponseInterface
* @throws GuzzleException
*/
protected function sendRequest(
string $method = 'POST',
string $body = '',
string $version = '7',
bool $httpErrors = true,
string $path = 'http://localhost/xmds.php?v=',
array $headers = ['HTTP_ACCEPT'=>'text/xml']
): ResponseInterface {
// Create a request for tests
return $this->client->request($method, $path . $version, [
'headers' => $headers,
'body' => $body,
'http_errors' => $httpErrors
]);
}
protected function getFile(
string $fileQuery = '',
string $method = 'GET',
string $basePath = 'http://localhost/xmds.php',
): ResponseInterface {
// Create a request for tests
return $this->client->request($method, $basePath . $fileQuery);
}
/**
* Create a global container for all tests to share.
* @throws \Exception
*/
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
}
/**
* Convenience function to skip a test with a reason and close output buffers nicely.
* @param string $reason
*/
public function skipTest(string $reason): void
{
$this->markTestSkipped($reason);
}
/**
* @return Logger|NullLogger|LoggerInterface
*/
public static function getLogger(): Logger|NullLogger|LoggerInterface
{
// Create if necessary
if (self::$logger === null) {
if (isset($_SERVER['PHPUNIT_LOG_TO_CONSOLE']) && $_SERVER['PHPUNIT_LOG_TO_CONSOLE']) {
self::$logger = new Logger('TESTS', [new StreamHandler(STDERR, Logger::DEBUG)]);
} else {
self::$logger = new NullLogger();
}
}
return self::$logger;
}
/**
* @inheritDoc
* @throws \Exception
*/
public function setUp(): void
{
self::getLogger()->debug('xmdsTestCase: setUp');
parent::setUp();
// Establish a local reference to the Slim app object
$this->client = $this->getGuzzleClient();
}
/**
* @inheritDoc
* @throws \Exception
*/
public function tearDown(): void
{
self::getLogger()->debug('xmdsTestCase: tearDown');
// Close and tidy up the app
$this->client = null;
parent::tearDown();
}
/**
* Set the _SERVER vars for the suite
* @param array $userSettings
*/
public static function setEnvironment(array $userSettings = []): void
{
$defaults = [
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/',
'SCRIPT_NAME' => '',
'PATH_INFO' => '/',
'QUERY_STRING' => '',
'SERVER_NAME' => 'local.dev',
'SERVER_PORT' => 80,
'HTTP_ACCEPT' => 'text/xml,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8',
'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'USER_AGENT' => 'Slim Framework',
'REMOTE_ADDR' => '127.0.0.1',
];
$environmentSettings = array_merge($userSettings, $defaults);
foreach ($environmentSettings as $key => $value) {
$_SERVER[$key] = $value;
}
}
}