|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Liip\FunctionalTestBundle; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
6
|
|
|
use Liip\FunctionalTestBundle\Annotations\QueryCount; |
|
7
|
|
|
use Liip\FunctionalTestBundle\Exception\AllowedQueriesExceededException; |
|
8
|
|
|
|
|
9
|
|
|
class QueryCounter |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var int */ |
|
12
|
|
|
private $defaultMaxCount; |
|
13
|
|
|
|
|
14
|
|
|
/** @var \Doctrine\Common\Annotations\AnnotationReader */ |
|
15
|
|
|
private $annotationReader; |
|
16
|
|
|
|
|
17
|
14 |
|
public function __construct($defaultMaxCount, Reader $annotationReader) |
|
18
|
|
|
{ |
|
19
|
14 |
|
$this->defaultMaxCount = $defaultMaxCount; |
|
20
|
14 |
|
$this->annotationReader = $annotationReader; |
|
|
|
|
|
|
21
|
14 |
|
} |
|
22
|
|
|
|
|
23
|
5 |
|
public function checkQueryCount($actualQueryCount) |
|
24
|
|
|
{ |
|
25
|
5 |
|
$maxQueryCount = $this->getMaxQueryCount(); |
|
26
|
|
|
|
|
27
|
5 |
|
if (null === $maxQueryCount) { |
|
28
|
|
|
return; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
5 |
|
if ($actualQueryCount > $maxQueryCount) { |
|
32
|
2 |
|
throw new AllowedQueriesExceededException( |
|
33
|
2 |
|
"Allowed amount of queries ($maxQueryCount) exceeded (actual: $actualQueryCount)." |
|
34
|
2 |
|
); |
|
35
|
|
|
} |
|
36
|
3 |
|
} |
|
37
|
|
|
|
|
38
|
5 |
|
private function getMaxQueryCount() |
|
39
|
|
|
{ |
|
40
|
5 |
|
$maxQueryCount = $this->getMaxQueryAnnotation(); |
|
41
|
|
|
|
|
42
|
5 |
|
if (false !== $maxQueryCount) { |
|
43
|
1 |
|
return $maxQueryCount; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
4 |
|
return $this->defaultMaxCount; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
5 |
|
private function getMaxQueryAnnotation() |
|
50
|
|
|
{ |
|
51
|
5 |
|
foreach (debug_backtrace() as $step) { |
|
52
|
5 |
|
if ('test' === substr($step['function'], 0, 4)) { //TODO: handle tests with the @test annotation |
|
53
|
5 |
|
$annotations = $this->annotationReader->getMethodAnnotations( |
|
54
|
5 |
|
new \ReflectionMethod($step['class'], $step['function']) |
|
55
|
5 |
|
); |
|
56
|
|
|
|
|
57
|
5 |
|
foreach ($annotations as $annotationClass) { |
|
58
|
1 |
|
if ($annotationClass instanceof QueryCount && isset($annotationClass->maxQueries)) { |
|
59
|
|
|
/* @var $annotations \Liip\FunctionalTestBundle\Annotations\QueryCount */ |
|
60
|
|
|
|
|
61
|
1 |
|
return $annotationClass->maxQueries; |
|
62
|
|
|
} |
|
63
|
4 |
|
} |
|
64
|
4 |
|
} |
|
65
|
5 |
|
} |
|
66
|
|
|
|
|
67
|
4 |
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.