Skip to content
Snippets Groups Projects
Commit 10afa66b authored by Jeroen Tubex's avatar Jeroen Tubex Committed by Carsten Logemann
Browse files

Issue #3109152 by JeroenT, C_Logemann: Move _linkchecker_isvalid_response_code...

Issue #3109152 by JeroenT, C_Logemann: Move _linkchecker_isvalid_response_code out of .module file to a dedicated service-part 2
parent 1f650c7e
No related branches found
No related tags found
No related merge requests found
<?php
namespace Drupal\linkchecker;
/**
* Class LinkcheckerResponseCodes.
*
* @package Drupal\linkchecker
*/
class LinkCheckerResponseCodes implements LinkCheckerResponseCodesInterface {
/**
* List of allowed response codes for form input validation.
*
* @var array
*/
protected static $responseCodes = [
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Time-out',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Large',
415 => 'Unsupported Media Type',
416 => 'Requested range not satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Time-out',
505 => 'HTTP Version not supported',
];
/**
* {@inheritdoc}
*/
public function isValid(int $code) {
return array_key_exists($code, self::$responseCodes);
}
}
<?php
namespace Drupal\linkchecker;
/**
* Class LinkcheckerResponseCodes.
*
* @package Drupal\linkchecker
*/
interface LinkCheckerResponseCodesInterface {
/**
* Check if the given HTTP response code is valid.
*
* @param int $code
* An numeric response code.
*
* @return bool
* TRUE if the status code is valid, otherwise FALSE.
*/
public function isValid(int $code);
}
<?php
namespace Drupal\Tests\linkchecker\Unit;
use Drupal\linkchecker\LinkCheckerResponseCodes;
use Drupal\Tests\UnitTestCase;
/**
* Tests \Drupal\linkchecker\LinkCheckerResponseCodes.
*
* @group linkchecker
*
* @coversDefaultClass \Drupal\linkchecker\LinkCheckerResponseCodes
*/
class LinkCheckerResponseCodesTest extends UnitTestCase {
/**
* The linkchecker response codes service.
*
* @var \Drupal\linkchecker\LinkCheckerResponseCodes
*/
protected $linkCheckerResponseCodes;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->linkCheckerResponseCodes = new LinkCheckerResponseCodes();
}
/**
* Tests the the ::isValid method.
*
* @param int $code
* The HTTP response code.
* @param bool $result
* The expected result from calling the function.
*
* @dataProvider isValidDataProvider
*
* @covers ::isValid
*/
public function testIsValid(int $code, bool $result) {
$this->assertEquals($result, $this->linkCheckerResponseCodes->isValid($code));
}
/**
* Data provider for testIsValid().
*
* @see testIsValid()
*/
public function isValidDataProvider() {
return [
[100, TRUE],
[101, TRUE],
[102, FALSE],
[103, FALSE],
[200, TRUE],
[201, TRUE],
[202, TRUE],
[203, TRUE],
[204, TRUE],
[205, TRUE],
[206, TRUE],
[207, FALSE],
[208, FALSE],
[226, FALSE],
[300, TRUE],
[301, TRUE],
[302, TRUE],
[303, TRUE],
[304, TRUE],
[305, TRUE],
[306, FALSE],
[307, TRUE],
[308, FALSE],
[400, TRUE],
[401, TRUE],
[402, TRUE],
[403, TRUE],
[404, TRUE],
[405, TRUE],
[406, TRUE],
[407, TRUE],
[408, TRUE],
[409, TRUE],
[410, TRUE],
[411, TRUE],
[412, TRUE],
[413, TRUE],
[414, TRUE],
[415, TRUE],
[416, TRUE],
[417, TRUE],
[418, FALSE],
[421, FALSE],
[422, FALSE],
[423, FALSE],
[424, FALSE],
[425, FALSE],
[426, FALSE],
[428, FALSE],
[429, FALSE],
[431, FALSE],
[451, FALSE],
[500, TRUE],
[501, TRUE],
[502, TRUE],
[503, TRUE],
[504, TRUE],
[505, TRUE],
[506, FALSE],
[507, FALSE],
[508, FALSE],
[510, FALSE],
[511, FALSE],
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment