1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace whm\Smoke\Rules\Html; |
4
|
|
|
|
5
|
|
|
use phm\HttpWebdriverClient\Http\Response\ResourcesAwareResponse; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use whm\Smoke\Rules\Attribute; |
8
|
|
|
use whm\Smoke\Rules\CheckResult; |
9
|
|
|
use whm\Smoke\Rules\StandardRule; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This rule calculates the size of a html document. If the document is bigger than a given value |
13
|
|
|
* the test will fail. |
14
|
|
|
*/ |
15
|
|
|
class BigContentRule extends StandardRule |
16
|
|
|
{ |
17
|
|
|
private $maxSize; |
18
|
|
|
|
19
|
|
|
protected $contentTypes = array('text/html'); |
20
|
|
|
|
21
|
|
|
public function init($maxSize = 400) |
22
|
|
|
{ |
23
|
|
|
$this->maxSize = $maxSize; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function doValidation(ResponseInterface $response) |
27
|
|
|
{ |
28
|
|
|
$totalSize = 0; |
29
|
|
|
|
30
|
|
|
if ($response instanceof ResourcesAwareResponse) { |
31
|
|
|
foreach ($response->getResources() as $resource) { |
32
|
|
|
$resourceSize = round($resource['transferSize'] / 1000); |
33
|
|
|
$totalSize += $resourceSize; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($totalSize > $this->maxSize) { |
38
|
|
|
$message = 'The total size of the file (and all assets) was ' . $totalSize . ' KB (max: ' . $this->maxSize . ' KB).'; |
39
|
|
|
$result = new CheckResult(CheckResult::STATUS_FAILURE, $message, $totalSize); |
40
|
|
|
$result->addAttribute(new Attribute('resources', $response->getResources(), true)); |
|
|
|
|
41
|
|
|
return $result; |
42
|
|
|
} else { |
43
|
|
|
$message = 'The total size of the file (and all assets) was ' . $totalSize . ' KB (max: ' . $this->maxSize . ' KB).'; |
44
|
|
|
$result = new CheckResult(CheckResult::STATUS_SUCCESS, $message, $totalSize); |
45
|
|
|
$result->addAttribute(new Attribute('resources', $response->getResources(), true)); |
|
|
|
|
46
|
|
|
return $result; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: