alexislefebvre /
LiipFunctionalTestBundle
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* |
||
| 6 | * This file is part of the Liip/FunctionalTestBundle |
||
| 7 | * |
||
| 8 | * (c) Lukas Kahwe Smith <[email protected]> |
||
| 9 | * |
||
| 10 | * This source file is subject to the MIT license that is bundled |
||
| 11 | * with this source code in the file LICENSE. |
||
| 12 | */ |
||
| 13 | |||
| 14 | namespace Liip\FunctionalTestBundle; |
||
| 15 | |||
| 16 | use Doctrine\Common\Annotations\Reader; |
||
| 17 | use Liip\FunctionalTestBundle\Annotations\QueryCount; |
||
| 18 | use Liip\FunctionalTestBundle\Exception\AllowedQueriesExceededException; |
||
| 19 | |||
| 20 | final class QueryCounter |
||
| 21 | { |
||
| 22 | private ?int $defaultMaxCount; |
||
| 23 | private ?Reader $annotationReader; |
||
| 24 | |||
| 25 | public function __construct(?int $defaultMaxCount, ?Reader $annotationReader) |
||
| 26 | { |
||
| 27 | $this->defaultMaxCount = $defaultMaxCount; |
||
| 28 | 5 | $this->annotationReader = $annotationReader; |
|
| 29 | } |
||
| 30 | 5 | ||
| 31 | 5 | public function checkQueryCount(int $actualQueryCount): void |
|
| 32 | 5 | { |
|
| 33 | $maxQueryCount = $this->getMaxQueryCount(); |
||
| 34 | |||
| 35 | if (null === $maxQueryCount) { |
||
| 36 | return; |
||
| 37 | } |
||
| 38 | |||
| 39 | if ($actualQueryCount > $maxQueryCount) { |
||
| 40 | throw new AllowedQueriesExceededException("Allowed amount of queries ($maxQueryCount) exceeded (actual: $actualQueryCount)."); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | private function getMaxQueryCount(): ?int |
||
| 45 | { |
||
| 46 | $maxQueryCount = $this->getMaxQueryAnnotation(); |
||
| 47 | |||
| 48 | if (null !== $maxQueryCount) { |
||
| 49 | return $maxQueryCount; |
||
| 50 | } |
||
| 51 | |||
| 52 | return $this->defaultMaxCount; |
||
| 53 | } |
||
| 54 | |||
| 55 | private function getMaxQueryAnnotation(): ?int |
||
| 56 | { |
||
| 57 | if (null === $this->annotationReader) { |
||
| 58 | @trigger_error('The annotationReader is not available', \E_USER_ERROR); |
||
| 59 | } |
||
| 60 | |||
| 61 | foreach (debug_backtrace() as $step) { |
||
| 62 | if ('test' === substr($step['function'], 0, 4)) { //TODO: handle tests with the @test annotation |
||
| 63 | $annotations = $this->annotationReader->getMethodAnnotations( |
||
|
0 ignored issues
–
show
|
|||
| 64 | new \ReflectionMethod($step['class'], $step['function']) |
||
| 65 | ); |
||
| 66 | |||
| 67 | foreach ($annotations as $annotationClass) { |
||
| 68 | if ($annotationClass instanceof QueryCount && isset($annotationClass->maxQueries)) { |
||
| 69 | /* @var $annotations \Liip\FunctionalTestBundle\Annotations\QueryCount */ |
||
| 70 | |||
| 71 | return $annotationClass->maxQueries; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | return null; |
||
| 78 | } |
||
| 79 | } |
||
| 80 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.