|
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
|
|
|
|
|
34
|
|
|
$totalSize += $resourceSize; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if ($totalSize > $this->maxSize) { |
|
39
|
|
|
$message = 'The total size of the file (and all assets) was ' . $totalSize . ' KB (max: ' . $this->maxSize . ' KB).'; |
|
40
|
|
|
$result = new CheckResult(CheckResult::STATUS_FAILURE, $message, $totalSize); |
|
41
|
|
|
$result->addAttribute(new Attribute('resources', $response->getResources(), true)); |
|
|
|
|
|
|
42
|
|
|
return $result; |
|
43
|
|
|
} else { |
|
44
|
|
|
$message = 'The total size of the file (and all assets) was ' . $totalSize . ' KB (max: ' . $this->maxSize . ' KB).'; |
|
45
|
|
|
$result = new CheckResult(CheckResult::STATUS_SUCCESS, $message, $totalSize); |
|
46
|
|
|
$result->addAttribute(new Attribute('resources', $response->getResources(), true)); |
|
|
|
|
|
|
47
|
|
|
return $result; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
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: