-
Notifications
You must be signed in to change notification settings - Fork 182
Allow tests to fail if the page executes too many database queries #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
df8740f
extend Client to check query count after each request
colinfrei e77f122
Add specific Exception
colinfrei 90c34e9
overwrite default test client
colinfrei 526712e
Reorganize a bit and allow using annotations to set the maximum numbe…
colinfrei b78d09d
require doctrine/common for the annotations
colinfrei e8e6027
disable by default
colinfrei b401f87
Only alias the test.client service if we actually have max_query_coun…
colinfrei ea40890
Add some docs
colinfrei dceabbc
adjust readme
colinfrei 6d9fb9d
require at least doctrine 2.x
colinfrei 274fdb1
Make overwriting the service be more robust.
colinfrei ee94276
comment copy/pasted
colinfrei ea54e98
Use early return instead of wrapping everything into the condition
colinfrei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Liip\FunctionalTestBundle\Annotations; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"METHOD"}) | ||
*/ | ||
class QueryCount | ||
{ | ||
/** @var integer */ | ||
public $maxQueries; | ||
|
||
public function __construct(array $values) | ||
{ | ||
if (isset($values['value'])) { | ||
$this->maxQueries = $values['value']; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Liip\FunctionalTestBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Alias; | ||
|
||
class SetTestClientPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (null === $container->getParameter('liip_functional_test.query_count.max_query_count')) { | ||
return; | ||
} | ||
|
||
if ($container->hasAlias('test.client')) { | ||
// test.client is an alias. | ||
// Register a private alias for this service to inject it as the parent | ||
$container->setAlias( | ||
'liip_functional_test.query_count.query_count_client.parent', | ||
new Alias((string) $container->getAlias('test.client'), false) | ||
); | ||
} else { | ||
// test.client is a definition. | ||
// Register it again as a private service to inject it as the parent | ||
$definition = $container->getDefinition('test.client'); | ||
$definition->setPublic(false); | ||
$container->setDefinition('liip_functional_test.query_count.query_count_client.parent', $definition); | ||
} | ||
|
||
$container->setAlias('test.client', 'liip_functional_test.query_count.query_count_client'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Liip\FunctionalTestBundle\Exception; | ||
|
||
class AllowedQueriesExceededException extends \Exception | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Liip\FunctionalTestBundle; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Client; | ||
|
||
class QueryCountClient extends Client | ||
{ | ||
/** @var QueryCounter */ | ||
private $queryCounter; | ||
|
||
public function setQueryCounter(QueryCounter $queryCounter) | ||
{ | ||
$this->queryCounter = $queryCounter; | ||
} | ||
|
||
public function request( | ||
$method, | ||
$uri, | ||
array $parameters = array(), | ||
array $files = array(), | ||
array $server = array(), | ||
$content = null, | ||
$changeHistory = true | ||
) { | ||
$crawler = parent::request($method, $uri, $parameters, $files, $server, $content, $changeHistory); | ||
|
||
$this->queryCounter->checkQueryCount($this->getProfile()->getCollector('db')->getQueryCount()); | ||
|
||
return $crawler; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Liip\FunctionalTestBundle; | ||
|
||
use Doctrine\Common\Annotations\Reader; | ||
use Liip\FunctionalTestBundle\Annotations\QueryCount; | ||
use Liip\FunctionalTestBundle\Exception\AllowedQueriesExceededException; | ||
|
||
class QueryCounter | ||
{ | ||
/** @var integer */ | ||
private $defaultMaxCount; | ||
|
||
/** @var \Doctrine\Common\Annotations\AnnotationReader */ | ||
private $annotationReader; | ||
|
||
public function __construct($defaultMaxCount, Reader $annotationReader) | ||
{ | ||
$this->defaultMaxCount = $defaultMaxCount; | ||
$this->annotationReader = $annotationReader; | ||
} | ||
|
||
public function checkQueryCount($actualQueryCount) | ||
{ | ||
$maxQueryCount = $this->getMaxQueryCount(); | ||
|
||
if (null === $maxQueryCount) { | ||
return; | ||
} | ||
|
||
if ($actualQueryCount > $maxQueryCount) { | ||
throw new AllowedQueriesExceededException( | ||
"Allowed amount of queries ($maxQueryCount) exceeded (actual: $actualQueryCount)." | ||
); | ||
} | ||
} | ||
|
||
private function getMaxQueryCount() | ||
{ | ||
if ($maxQueryCount = $this->getMaxQueryAnnotation()) { | ||
return $maxQueryCount; | ||
} | ||
|
||
return $this->defaultMaxCount; | ||
} | ||
|
||
private function getMaxQueryAnnotation() | ||
{ | ||
foreach (debug_backtrace() as $step) { | ||
if ('test' === substr($step['function'], 0, 4)) { //TODO: handle tests with the @test annotation | ||
$annotations = $this->annotationReader->getMethodAnnotations( | ||
new \ReflectionMethod($step['class'], $step['function']) | ||
); | ||
|
||
foreach ($annotations as $annotationClass) { | ||
if ($annotationClass instanceof QueryCount AND isset($annotationClass->maxQueries)) { | ||
/** @var $annotations \Liip\FunctionalTestBundle\Annotations\QueryCount */ | ||
|
||
return $annotationClass->maxQueries; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm .. i keep wondering if there is a better pattern to be able to add things on top of a service definition and then alias the new version to the old service name ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
symfony/symfony#5920