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
|
|
|
/** |
18
|
|
|
* "query.max_query_count" is an array, it is only accessible |
19
|
|
|
* through "query" node and getting the "max_query_count" array |
20
|
|
|
* key with PHP. |
21
|
|
|
*/ |
22
|
11 |
|
public function __construct($query, Reader $annotationReader) |
23
|
|
|
{ |
24
|
11 |
|
$this->defaultMaxCount = $query['max_query_count']; |
25
|
11 |
|
$this->annotationReader = $annotationReader; |
|
|
|
|
26
|
11 |
|
} |
27
|
|
|
|
28
|
5 |
|
public function checkQueryCount($actualQueryCount) |
29
|
|
|
{ |
30
|
5 |
|
$maxQueryCount = $this->getMaxQueryCount(); |
31
|
|
|
|
32
|
5 |
|
if (null === $maxQueryCount) { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
5 |
|
if ($actualQueryCount > $maxQueryCount) { |
37
|
2 |
|
throw new AllowedQueriesExceededException( |
38
|
2 |
|
"Allowed amount of queries ($maxQueryCount) exceeded (actual: $actualQueryCount)." |
39
|
2 |
|
); |
40
|
|
|
} |
41
|
3 |
|
} |
42
|
|
|
|
43
|
5 |
|
private function getMaxQueryCount() |
44
|
|
|
{ |
45
|
5 |
|
$maxQueryCount = $this->getMaxQueryAnnotation(); |
46
|
|
|
|
47
|
5 |
|
if (false !== $maxQueryCount) { |
48
|
1 |
|
return $maxQueryCount; |
49
|
|
|
} |
50
|
|
|
|
51
|
4 |
|
return $this->defaultMaxCount; |
52
|
|
|
} |
53
|
|
|
|
54
|
5 |
|
private function getMaxQueryAnnotation() |
55
|
|
|
{ |
56
|
5 |
|
foreach (debug_backtrace() as $step) { |
57
|
5 |
|
if ('test' === substr($step['function'], 0, 4)) { //TODO: handle tests with the @test annotation |
58
|
5 |
|
$annotations = $this->annotationReader->getMethodAnnotations( |
59
|
5 |
|
new \ReflectionMethod($step['class'], $step['function']) |
60
|
5 |
|
); |
61
|
|
|
|
62
|
5 |
|
foreach ($annotations as $annotationClass) { |
63
|
1 |
|
if ($annotationClass instanceof QueryCount && isset($annotationClass->maxQueries)) { |
64
|
|
|
/* @var $annotations \Liip\FunctionalTestBundle\Annotations\QueryCount */ |
65
|
|
|
|
66
|
1 |
|
return $annotationClass->maxQueries; |
67
|
|
|
} |
68
|
4 |
|
} |
69
|
4 |
|
} |
70
|
5 |
|
} |
71
|
|
|
|
72
|
4 |
|
return false; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
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.