AbstractTester   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Test Coverage

Coverage 82.98%

Importance

Changes 0
Metric Value
wmc 18
eloc 39
c 0
b 0
f 0
dl 0
loc 177
ccs 39
cts 47
cp 0.8298
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getHtaccessCapabilityTester() 0 12 4
A prepareForRun() 0 7 1
A setTestFilesLineUpper() 0 5 2
A __construct() 0 3 1
A setHttpRequester() 0 5 2
A registerTestFile() 0 3 1
A getCacheKey() 0 3 1
A lineUpTestFiles() 0 6 2
A makeHttpRequest() 0 7 2
A getBaseUrl() 0 3 1
A getBaseDir() 0 3 1
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 35
    public function getCacheKey()
64
    {
65 35
        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 80
    protected function registerTestFile($filename, $content)
85
    {
86 80
        $this->testFiles[] = [$this->baseDir . '/' . $filename, $content];
87 80
    }
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 80
    protected function prepareForRun($baseDir, $baseUrl)
98
    {
99 80
        $this->baseDir = $baseDir;
100 80
        $this->baseUrl = $baseUrl;
101 80
        $this->testFiles = [];
102 80
        $this->registerTestFiles();
103 80
        $this->lineUpTestFiles();
104 80
    }
105
106
    abstract public function run($baseDir, $baseUrl);
107
108
    /**
109
     * Constructor.
110
     *
111
     * @return void
112
     */
113 80
    public function __construct()
114
    {
115 80
        $this->subDir = $this->getSubDir();
116 80
    }
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 80
    protected function makeHttpRequest($url)
126
    {
127 80
        if (!isset($this->httpRequester)) {
128
            $this->httpRequester = new SimpleHttpRequester();
129
        }
130 80
        $this->lastHttpResponse = $this->httpRequester->makeHttpRequest($url);
131 80
        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 80
    public function setHttpRequester($httpRequester)
141
    {
142 80
        $this->httpRequester = $httpRequester;
143 80
        if (isset($this->hct)) {
144
            $this->hct->setHttpRequester($this->httpRequester);
145
        }
146 80
    }
147
148 80
    public function lineUpTestFiles()
149
    {
150 80
        if (!isset($this->testFilesLineUpper)) {
151
            $this->testFilesLineUpper = new SimpleTestFileLineUpper();
152
        }
153 80
        $this->testFilesLineUpper->lineUp($this->testFiles);
154 80
    }
155
156
    /**
157
     * Set object responsible for lining up the test files.
158
     *
159
     * @param  TestFilesLineUpperInterface  $testFilesLineUpper
160
     * @return void
161
     */
162 80
    public function setTestFilesLineUpper($testFilesLineUpper)
163
    {
164 80
        $this->testFilesLineUpper = $testFilesLineUpper;
165 80
        if (isset($this->hct)) {
166
            $this->hct->setTestFilesLineUpper($this->testFilesLineUpper);
167
        }
168 80
    }
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 30
    public function getHtaccessCapabilityTester()
179
    {
180 30
        if (!isset($this->hct)) {
181 30
            $this->hct = new HtaccessCapabilityTester($this->baseDir, $this->baseUrl);
182 30
            if (isset($this->testFilesLineUpper)) {
183 30
                $this->hct->setTestFilesLineUpper($this->testFilesLineUpper);
184
            }
185 30
            if (isset($this->httpRequester)) {
186 30
                $this->hct->setHttpRequester($this->httpRequester);
187
            }
188
        }
189 30
        return $this->hct;
190
    }
191
}
192