paulgoughbooks_old
This repository has no backups
This repository's network speed is throttled to 100KB/sec
Upgrade your account to fix these warnings, or use backups.vc for automated backups
@ 2
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
<?php /** * CodeIgniter * * An open source application development framework for PHP * * This content is released under the MIT License (MIT) * * Copyright (c) 2014-2019 British Columbia Institute of Technology * Copyright (c) 2019-2020 CodeIgniter Foundation * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * * @package CodeIgniter * @author CodeIgniter Dev Team * @copyright 2019-2020 CodeIgniter Foundation * @license https://opensource.org/licenses/MIT MIT License * @link https://codeigniter.com * @since Version 4.0.0 * @filesource */ namespace CodeIgniter\Test; use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\HTTP\ResponseInterface; use Config\Format; use PHPUnit\Framework\TestCase; /** * Assertions for a response */ class FeatureResponse extends TestCase { /** * The response. * * @var \CodeIgniter\HTTP\ResponseInterface */ public $response; /** * DOM for the body. * * @var \CodeIgniter\Test\DOMParser */ protected $domParser; /** * Constructor. * * @param ResponseInterface $response */ public function __construct(ResponseInterface $response = null) { $this->response = $response; $body = $response->getBody(); if (! empty($body) && is_string($body)) { $this->domParser = (new DOMParser())->withString($body); } } //-------------------------------------------------------------------- // Simple Response Checks //-------------------------------------------------------------------- /** * Boils down the possible responses into a bolean valid/not-valid * response type. * * @return boolean */ public function isOK(): bool { $status = $this->response->getStatusCode(); // Only 200 and 300 range status codes // are considered valid. if ($status >= 400 || $status < 200) { return false; } // Empty bodies are not considered valid, unless in redirects if ($status < 300 && empty($this->response->getBody())) { return false; } return true; } /** * Returns whether or not the Response was a redirect response * * @return boolean */ public function isRedirect(): bool { return $this->response instanceof RedirectResponse; } /** * Assert that the given response was a redirect. * * @throws \Exception */ public function assertRedirect() { $this->assertTrue($this->isRedirect(), 'Response is not a RedirectResponse.'); } /** * Returns the URL set for redirection. * * @return string|null */ public function getRedirectUrl(): ?string { if (! $this->isRedirect()) { return null; } if ($this->response->hasHeader('Location')) { return $this->response->getHeaderLine('Location'); } elseif ($this->response->hasHeader('Refresh')) { return str_replace('0;url=', '', $this->response->getHeaderLine('Refresh')); } return null; } /** * Asserts that the status is a specific value. * * @param integer $code * * @throws \Exception */ public function assertStatus(int $code) { $this->assertEquals($code, (int) $this->response->getStatusCode()); } /** * Asserts that the Response is considered OK. * * @throws \Exception */ public function assertOK() { $this->assertTrue($this->isOK(), "{$this->response->getStatusCode()} is not a successful status code, or the Response has an empty body."); } //-------------------------------------------------------------------- // Session Assertions //-------------------------------------------------------------------- /** * Asserts that an SESSION key has been set and, optionally, test it's value. * * @param string $key * @param null $value * * @throws \Exception */ public function assertSessionHas(string $key, $value = null) { $this->assertTrue(array_key_exists($key, $_SESSION), "'{$key}' is not in the current \$_SESSION"); if ($value !== null) { $this->assertEquals($value, $_SESSION[$key], "The value of '{$key}' ({$value}) does not match expected value."); } } /** * Asserts the session is missing $key. * * @param string $key * * @throws \Exception */ public function assertSessionMissing(string $key) { $this->assertFalse(array_key_exists($key, $_SESSION), "'{$key}' should not be present in \$_SESSION."); } //-------------------------------------------------------------------- // Header Assertions //-------------------------------------------------------------------- /** * Asserts that the Response contains a specific header. * * @param string $key * @param null $value * * @throws \Exception */ public function assertHeader(string $key, $value = null) { $this->assertTrue($this->response->hasHeader($key), "'{$key}' is not a valid Response header."); if ($value !== null) { $this->assertEquals($value, $this->response->getHeaderLine($key), "The value of '{$key}' header ({$this->response->getHeaderLine($key)}) does not match expected value."); } } /** * Asserts the Response headers does not contain the specified header. * * @param string $key * * @throws \Exception */ public function assertHeaderMissing(string $key) { $this->assertFalse($this->response->hasHeader($key), "'{$key}' should not be in the Response headers."); } //-------------------------------------------------------------------- // Cookie Assertions //-------------------------------------------------------------------- /** * Asserts that the response has the specified cookie. * * @param string $key * @param null $value * @param string|null $prefix * * @throws \Exception */ public function assertCookie(string $key, $value = null, string $prefix = '') { $this->assertTrue($this->response->hasCookie($key, $value, $prefix), "No cookie found named '{$key}'."); } /** * Assert the Response does not have the specified cookie set. * * @param string $key */ public function assertCookieMissing(string $key) { $this->assertFalse($this->response->hasCookie($key), "Cookie named '{$key}' should not be set."); } /** * Asserts that a cookie exists and has an expired time. * * @param string $key * @param string $prefix * * @throws \Exception */ public function assertCookieExpired(string $key, string $prefix = '') { $this->assertTrue($this->response->hasCookie($key, null, $prefix)); $this->assertGreaterThan(time(), $this->response->getCookie($key, $prefix)['expires']); } //-------------------------------------------------------------------- // DomParser Assertions //-------------------------------------------------------------------- /** * Assert that the desired text can be found in the result body. * * @param string|null $search * @param string|null $element * * @throws \Exception */ public function assertSee(string $search = null, string $element = null) { $this->assertTrue($this->domParser->see($search, $element), "Do not see '{$search}' in response."); } /** * Asserts that we do not see the specified text. * * @param string|null $search * @param string|null $element * * @throws \Exception */ public function assertDontSee(string $search = null, string $element = null) { $this->assertTrue($this->domParser->dontSee($search, $element), "I should not see '{$search}' in response."); } /** * Assert that we see an element selected via a CSS selector. * * @param string $search * * @throws \Exception */ public function assertSeeElement(string $search) { $this->assertTrue($this->domParser->seeElement($search), "Do not see element with selector '{$search} in response.'"); } /** * Assert that we do not see an element selected via a CSS selector. * * @param string $search * * @throws \Exception */ public function assertDontSeeElement(string $search) { $this->assertTrue($this->domParser->dontSeeElement($search), "I should not see an element with selector '{$search}' in response.'"); } /** * Assert that we see a link with the matching text and/or class. * * @param string $text * @param string|null $details * * @throws \Exception */ public function assertSeeLink(string $text, string $details = null) { $this->assertTrue($this->domParser->seeLink($text, $details), "Do no see anchor tag with the text {$text} in response."); } /** * Assert that we see an input with name/value. * * @param string $field * @param string|null $value * * @throws \Exception */ public function assertSeeInField(string $field, string $value = null) { $this->assertTrue($this->domParser->seeInField($field, $value), "Do no see input named {$field} with value {$value} in response."); } //-------------------------------------------------------------------- // JSON Methods //-------------------------------------------------------------------- /** * Returns the response's body as JSON * * @return mixed|false */ public function getJSON() { $response = $this->response->getJSON(); if (is_null($response)) { return false; } return $response; } /** * Test that the response contains a matching JSON fragment. * * @param array $fragment * * @throws \Exception */ public function assertJSONFragment(array $fragment) { $json = json_decode($this->getJSON(), true); $this->assertArraySubset($fragment, $json, false, 'Response does not contain a matching JSON fragment.'); } /** * Asserts that the JSON exactly matches the passed in data. * If the value being passed in is a string, it must be a json_encoded string. * * @param string|array $test * * @throws \Exception */ public function assertJSONExact($test) { $json = $this->getJSON(); if (is_array($test)) { $config = new Format(); $formatter = $config->getFormatter('application/json'); $test = $formatter->format($test); } $this->assertJsonStringEqualsJsonString($test, $json, 'Response does not contain matching JSON.'); } //-------------------------------------------------------------------- // XML Methods //-------------------------------------------------------------------- /** * Returns the response' body as XML * * @return mixed|string */ public function getXML() { return $this->response->getXML(); } } |
Commits for paulgoughbooks_old/trunk/system/Test/FeatureResponse.php
Revision | Author | Commited | Message |
---|---|---|---|
2 | tporter | Tue 03 Nov, 2020 08:50:21 +0000 | Migration of Paul Goughs Books site to Codeignitor 4 |