Skip to content

Commit

Permalink
Merge pull request #24 from higidi/test_client
Browse files Browse the repository at this point in the history
FEATURE: Custom VideoRecorderClient & helpful TestCaseTrait added
  • Loading branch information
higidi authored Dec 15, 2022
2 parents be4f739 + 08874db commit 4700c57
Show file tree
Hide file tree
Showing 11 changed files with 706 additions and 24 deletions.
1 change: 1 addition & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
- dependencies: "lowest"
stability: "stable"
php-version: "7.2"
symfony-deprecations-helper: "weak"
steps:
- name: "Checkout"
uses: "actions/checkout@v2"
Expand Down
109 changes: 89 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ VCRBundle

Integrates [php-vcr](https://github.com/php-vcr/php-vcr) into Symfony and its
web profiler.
It also provides a VideoRecorderBrowser for testing purpose with extra helper methods handling php-vcr recordings.

<img src="https://cloud.githubusercontent.com/assets/66958/5232274/b841676e-774b-11e4-8f4e-1f3e8cb7739e.png" width="280" height="175" alt="PHP-VCR Symfony web profiler panel"/>
<img src="https://cloud.githubusercontent.com/assets/66958/5232275/b84288d8-774b-11e4-803c-7b72f75e59b0.png" width="280" height="175" alt="PHP-VCR Symfony web profiler panel - request details"/>
Expand All @@ -20,41 +21,109 @@ composer require php-vcr/vcr-bundle
And declare the bundle in your `config/bundles.php` file:

```php
<?php
declare(strict_types = 1);

return [
// ...
VCR\VCRBundle\VCRBundle::class => ['test' => true],
];
```

## Usage

Enable the required library hooks for your purpose and write test cases.

### VideoRecorderBrowser (without Trait)

```php
<?php
declare(strict_types = 1);

class ExampleTest extends \VCR\VCRBundle\Tests\Functional\WebTestCase
{
public function test(): void
{
$kernel = static::bootKernel();
/** @var \VCR\VCRBundle\VideoRecorderBrowser $client */
$client = $kernel->getContainer()->get('test.client.vcr');

$client->insertVideoRecorderCassette('my-test-cassette-name');

// this is an example, normally services inside you project do stuff like this and you trigger them by
// execute requests via the KernelBrowser client
file_get_contents('https://www.google.de');

// cassette.path is configured to '%kernel.project_dir%/tests/Fixtures'
// recordings are written to %kernel.project_dir%/tests/Fixtures/my-test-cassette-name
// cassette.path + cassetteName (done by inserting the cassette)
}
}
```

### VideoRecorderBrowser (with Trait)

```php
<?php
declare(strict_types = 1);

namespace MyCompany\MyProject\Tests\Functional;

class ExampleTest extends \VCR\VCRBundle\Tests\Functional\WebTestCase
{
use \VCR\VCRBundle\Test\VCRTestCaseTrait;

/**
* Specify a namespace prefix which should be ignored while generating the base path for this test case.
*/
protected $ignoredTestSuiteNamespacePrefix = 'MyCompany\\MyProject\\Tests\\';

public function test(): void
{
/** @var \VCR\VCRBundle\VideoRecorderBrowser $client */
$client = static::createVideoRecorderClient();

// this is an example, normally services inside you project do stuff like this and you trigger them by
// execute requests via the KernelBrowser client
file_get_contents('https://www.google.de');

// cassette.path is configured to '%kernel.project_dir%/tests/Fixtures'
// recordings are written to %kernel.project_dir%/tests/Fixtures/Functional/ExampleTest/test
// cassette.path + TestCasePath (- ignoredTestSuiteNamespacePrefix) + TestName
}
}
```

## Configuration reference

```yaml
vcr:
enabled: true
library_hooks:
stream_wrapper: false
curl: false
soap: false
request_matchers:
method: true
url: true
query_string: true
host: true
headers: true
body: true
post_fields: true
cassette:
type: json
path: '%kernel.cache_dir%/vcr'
name: vcr
enabled: true
library_hooks:
stream_wrapper: false
curl: false
soap: false
request_matchers:
method: true
url: true
query_string: true
host: true
headers: true
body: true
post_fields: true
cassette:
type: json
path: '%kernel.cache_dir%/vcr'
name: vcr
```
## Credits
* [Kévin Gomez](http://github.com/K-Phoen/)
* [Ludovic Fleury](https://github.com/ludofleury) - to whom I borrowed the
design of the web profiler part from his [GuzzleBundle](https://github.com/ludofleury/GuzzleBundle/).
* [Kévin Gomez](http://github.com/K-Phoen/)
* [Ludovic Fleury](https://github.com/ludofleury) - to whom I borrowed the
design of the web profiler part from his [GuzzleBundle](https://github.com/ludofleury/GuzzleBundle/).
* [Simon Hübner](https://github.com/simonhard) - making the bundle SF 5.4 compatible
* [Daniel Hürtgen](https://github.com/higidi) - making the bundle PHP 8 compatible and providing extra TestCase helper
## License
Expand Down
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
{
"name": "Kévin Gomez",
"email": "[email protected]"
},
{
"name": "Daniel Hürtgen",
"email": "[email protected]"
},
{
"name": "Simon Hübner",
"email": "[email protected]"
}
],
"config": {
Expand All @@ -16,18 +24,20 @@
"php": "^7.2|^8",
"php-vcr/php-vcr": "^1.5",
"symfony/config": "^4|^5",
"symfony/browser-kit": "^4|^5",
"symfony/dependency-injection": "^4|^5",
"symfony/filesystem": "^4|^5",
"symfony/event-dispatcher": "^4|^5",
"symfony/framework-bundle": "^4.4|^5.4",
"symfony/http-foundation": "^4|^5",
"symfony/http-kernel": "^4.4|^5",
"symfony/polyfill-php80": "^1.16",
"symfony/yaml": "^4|^5"
},
"require-dev": {
"dms/phpunit-arraysubset-asserts": "^0.4",
"neutron/temporary-filesystem": "^3",
"phpunit/phpunit": "^8.5|^9.5",
"symfony/framework-bundle": "^4.4|^5.4",
"symfony/phpunit-bridge": "^4.4|^5.4"
},
"autoload": {
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ services:
calls:
- [ setEventDispatcher, [ '@event_dispatcher' ] ]
public: true

test.client.vcr:
class: VCR\VCRBundle\VideoRecorderBrowser
parent: test.client
Loading

0 comments on commit 4700c57

Please sign in to comment.