| 1 |
<?php |
| 2 |
|
| 3 |
namespace HtaccessCapabilityTester\Testers; |
| 4 |
|
| 5 |
use \HtaccessCapabilityTester\HtaccessCapabilityTester; |
| 6 |
use \HtaccessCapabilityTester\HttpRequesterInterface; |
| 7 |
use \HtaccessCapabilityTester\HttpResponse; |
| 8 |
use \HtaccessCapabilityTester\SimpleHttpRequester; |
| 9 |
use \HtaccessCapabilityTester\SimpleTestFileLineUpper; |
| 10 |
use \HtaccessCapabilityTester\TestFilesLineUpperInterface; |
| 11 |
use \HtaccessCapabilityTester\TestResult; |
| 12 |
|
| 13 |
abstract class AbstractTester |
| 14 |
{ |
| 15 |
/** @var string The dir where the test files should be put */ |
| 16 |
protected $baseDir; |
| 17 |
|
| 18 |
/** @var string The base url that the tests can be run from (corresponds to $baseDir) */ |
| 19 |
protected $baseUrl; |
| 20 |
|
| 21 |
/** @var string Subdir to put .htaccess files in */ |
| 22 |
protected $subDir; |
| 23 |
|
| 24 |
/** @var array Test files for the test */ |
| 25 |
protected $testFiles; |
| 26 |
|
| 27 |
/** @var HttpRequesterInterface An object for making the HTTP request */ |
| 28 |
protected $httpRequester; |
| 29 |
|
| 30 |
/** @var HttpResponse The response of the previous HTTP request (if any) */ |
| 31 |
public $lastHttpResponse; |
| 32 |
|
| 33 |
/** @var TestFilesLineUpperInterface An object for lining up the test-files */ |
| 34 |
protected $testFilesLineUpper; |
| 35 |
|
| 36 |
/** @var HtaccessCapabilityTester The HtaccessCapabilityTester to use for subtests */ |
| 37 |
private $hct; |
| 38 |
|
| 39 |
/** |
| 40 |
* Register the test files using the "registerTestFile" method |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
abstract protected function registerTestFiles(); |
| 45 |
|
| 46 |
/** |
| 47 |
* Child classes must implement this method, which tells which subdir the |
| 48 |
* test files are to be put. |
| 49 |
* |
| 50 |
* @return string A subdir for the test files |
| 51 |
*/ |
| 52 |
abstract protected function getSubDir(); |
| 53 |
|
| 54 |
/** |
| 55 |
* Get key for caching purposes. |
| 56 |
* |
| 57 |
* Return a unique key. The default is to use the subdir. However, if a concrete Tester class |
| 58 |
* can test different things, it must override this method and make sure to return a different |
| 59 |
* key per thing it can test |
| 60 |
* |
| 61 |
* @return string A key it can be cached under |
| 62 |
*/ |
| 63 |
public function getCacheKey() |
| 64 |
{ |
| 65 |
return $this->getSubDir(); |
| 66 |
} |
| 67 |
|
| 68 |
public function getBaseDir() |
| 69 |
{ |
| 70 |
return $this->baseDir; |
| 71 |
} |
| 72 |
|
| 73 |
public function getBaseUrl() |
| 74 |
{ |
| 75 |
return $this->baseUrl; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Child classes must that implement the registerTestFiles method must call |
| 80 |
* this method to register each test file. |
| 81 |
* |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
protected function registerTestFile($filename, $content) |
| 85 |
{ |
| 86 |
$this->testFiles[] = [$this->baseDir . '/' . $filename, $content]; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Last moment preparations before running the test |
| 91 |
* |
| 92 |
* @param string $baseDir Directory on the server where the test files can be put |
| 93 |
* @param string $baseUrl The base URL of the test files |
| 94 |
* |
| 95 |
* @throws \Exception In case the test cannot be prepared due to serious issues |
| 96 |
*/ |
| 97 |
protected function prepareForRun($baseDir, $baseUrl) |
| 98 |
{ |
| 99 |
$this->baseDir = $baseDir; |
| 100 |
$this->baseUrl = $baseUrl; |
| 101 |
$this->testFiles = []; |
| 102 |
$this->registerTestFiles(); |
| 103 |
$this->lineUpTestFiles(); |
| 104 |
} |
| 105 |
|
| 106 |
abstract public function run($baseDir, $baseUrl); |
| 107 |
|
| 108 |
/** |
| 109 |
* Constructor. |
| 110 |
* |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public function __construct() |
| 114 |
{ |
| 115 |
$this->subDir = $this->getSubDir(); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Make a HTTP request to a URL. |
| 120 |
* |
| 121 |
* @param string $url The URL to make the HTTP request to |
| 122 |
* |
| 123 |
* @return HttpResponse A HttpResponse object, which simply contains body and status code. |
| 124 |
*/ |
| 125 |
protected function makeHttpRequest($url) |
| 126 |
{ |
| 127 |
if (!isset($this->httpRequester)) { |
| 128 |
$this->httpRequester = new SimpleHttpRequester(); |
| 129 |
} |
| 130 |
$this->lastHttpResponse = $this->httpRequester->makeHttpRequest($url); |
| 131 |
return $this->lastHttpResponse; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Set HTTP requester object, which handles making HTTP requests. |
| 136 |
* |
| 137 |
* @param HttpRequesterInterface $httpRequester The HTTPRequester to use |
| 138 |
* @return void |
| 139 |
*/ |
| 140 |
public function setHttpRequester($httpRequester) |
| 141 |
{ |
| 142 |
$this->httpRequester = $httpRequester; |
| 143 |
if (isset($this->hct)) { |
| 144 |
$this->hct->setHttpRequester($this->httpRequester); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
public function lineUpTestFiles() |
| 149 |
{ |
| 150 |
if (!isset($this->testFilesLineUpper)) { |
| 151 |
$this->testFilesLineUpper = new SimpleTestFileLineUpper(); |
| 152 |
} |
| 153 |
$this->testFilesLineUpper->lineUp($this->testFiles); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Set object responsible for lining up the test files. |
| 158 |
* |
| 159 |
* @param TestFilesLineUpperInterface $testFilesLineUpper |
| 160 |
* @return void |
| 161 |
*/ |
| 162 |
public function setTestFilesLineUpper($testFilesLineUpper) |
| 163 |
{ |
| 164 |
$this->testFilesLineUpper = $testFilesLineUpper; |
| 165 |
if (isset($this->hct)) { |
| 166 |
$this->hct->setTestFilesLineUpper($this->testFilesLineUpper); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Get HtaccessCapabilityTester. |
| 172 |
* |
| 173 |
* Some tests use HtaccessCapabilityTester to run other tests. |
| 174 |
* This gets such object with baseDir and baseUrl set up |
| 175 |
* |
| 176 |
* @return HtaccessCapabilityTester |
| 177 |
*/ |
| 178 |
public function getHtaccessCapabilityTester() |
| 179 |
{ |
| 180 |
if (!isset($this->hct)) { |
| 181 |
$this->hct = new HtaccessCapabilityTester($this->baseDir, $this->baseUrl); |
| 182 |
if (isset($this->testFilesLineUpper)) { |
| 183 |
$this->hct->setTestFilesLineUpper($this->testFilesLineUpper); |
| 184 |
} |
| 185 |
if (isset($this->httpRequester)) { |
| 186 |
$this->hct->setHttpRequester($this->httpRequester); |
| 187 |
} |
| 188 |
} |
| 189 |
return $this->hct; |
| 190 |
} |
| 191 |
} |
| 192 |
|