Skip to content

Commit

Permalink
minor #3 Add a note about testing in a Symfony context (romain-jacquart)
Browse files Browse the repository at this point in the history
This PR was merged into the main branch.

Discussion
----------

Add a note about testing in a Symfony context

This pull request adds a note to avoid common pitfalls when testing a class which uses Endeavor.

Commits
-------

f828362 Add a note about testing in a Symfony context
  • Loading branch information
GromNaN committed Feb 25, 2022
2 parents b40cf50 + f828362 commit c7f7ca0
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,39 @@ $endeavor->setErrorHandler(function (Endeavor $endeavor, \Throwable $e, int $att
```


## Testing

Testing a class which uses Endeavor can dramatically slow down the execution of tests.

### Symfony context

On a Symfony project, this can be resolved using the [`symfony/phpunit-bridge`](https://github.com/symfony/phpunit-bridge) package and the included [`ClockMock`](https://github.com/symfony/phpunit-bridge/blob/5.3/ClockMock.php).

_See [the documentation](https://symfony.com/doc/current/components/phpunit_bridge.html#clock-mocking) on how to setup the bridge and use the `@group time-sensitive` annotation._

Finally, on the `tests/bootstrap.php` ([_documentation_](https://symfony.com/doc/current/testing/bootstrap.html)), register the `Endeavor` class:

```php
# tests/boostrap.php
<?php

use PrismaMedia\Endeavor\Endeavor;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Component\Dotenv\Dotenv;

require dirname(__DIR__).'/vendor/autoload.php';

if (file_exists(dirname(__DIR__).'/config/bootstrap.php')) {
require dirname(__DIR__).'/config/bootstrap.php';
} elseif (method_exists(Dotenv::class, 'bootEnv')) {
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
}

// Register Endeavor in ClockMock to skip the waiting time between retries
ClockMock::register(Endeavor::class);
```


## Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Expand Down

0 comments on commit c7f7ca0

Please sign in to comment.