Skip to content

Added seeRequestTimeIsLessThan function #132

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 5 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Codeception/Module/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Codeception\Module\Symfony\SecurityAssertionsTrait;
use Codeception\Module\Symfony\ServicesAssertionsTrait;
use Codeception\Module\Symfony\SessionAssertionsTrait;
use Codeception\Module\Symfony\TimeAssertionsTrait;
use Codeception\Module\Symfony\TwigAssertionsTrait;
use Codeception\TestInterface;
use Exception;
Expand Down Expand Up @@ -138,6 +139,7 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
use SecurityAssertionsTrait;
use ServicesAssertionsTrait;
use SessionAssertionsTrait;
use TimeAssertionsTrait;
use TwigAssertionsTrait;

/**
Expand Down
50 changes: 50 additions & 0 deletions src/Codeception/Module/Symfony/TimeAssertionsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Codeception\Module\Symfony;

use Symfony\Component\HttpKernel\DataCollector\TimeDataCollector;
use function round;
use function sprintf;

trait TimeAssertionsTrait
{
/**
* Asserts that the time a request lasted is less than expected.
*
* If the page performed a HTTP redirect, only the time of the last request will be taken into account.
* You can modify this behavior using [stopFollowingRedirects()](https://codeception.com/docs/modules/Symfony#stopFollowingRedirects) first.
*
* Also, note that using code coverage can significantly increase the time it takes to resolve a request,
* which could lead to unreliable results when used together.
*
* It is recommended to set [`rebootable_client`](https://codeception.com/docs/modules/Symfony#Config) to `true` (=default),
* cause otherwise this assertion gives false results if you access multiple pages in a row, or if your app performs a redirect.
*
* @param int|float $expectedMilliseconds The expected time in milliseconds
*/
public function seeRequestTimeIsLessThan($expectedMilliseconds): void
{
$expectedMilliseconds = round($expectedMilliseconds, 2);

$timeCollector = $this->grabTimeCollector(__FUNCTION__);

$actualMilliseconds = round($timeCollector->getDuration(), 2);

$this->assertLessThan(
$expectedMilliseconds,
$actualMilliseconds,
sprintf(
'The request was expected to last less than %d ms, but it actually lasted %d ms.',
$expectedMilliseconds,
$actualMilliseconds
)
);
}

protected function grabTimeCollector(string $function): TimeDataCollector
{
return $this->grabCollector('time', $function);
}
}