Skip to content

Commit 4700c57

Browse files
authored
Merge pull request #24 from higidi/test_client
FEATURE: Custom VideoRecorderClient & helpful TestCaseTrait added
2 parents be4f739 + 08874db commit 4700c57

File tree

11 files changed

+706
-24
lines changed

11 files changed

+706
-24
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
- dependencies: "lowest"
3939
stability: "stable"
4040
php-version: "7.2"
41+
symfony-deprecations-helper: "weak"
4142
steps:
4243
- name: "Checkout"
4344
uses: "actions/checkout@v2"

README.md

Lines changed: 89 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ VCRBundle
33

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

78
<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"/>
89
<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"/>
@@ -20,41 +21,109 @@ composer require php-vcr/vcr-bundle
2021
And declare the bundle in your `config/bundles.php` file:
2122

2223
```php
24+
<?php
25+
declare(strict_types = 1);
26+
2327
return [
2428
// ...
2529
VCR\VCRBundle\VCRBundle::class => ['test' => true],
2630
];
31+
```
32+
33+
## Usage
34+
35+
Enable the required library hooks for your purpose and write test cases.
36+
37+
### VideoRecorderBrowser (without Trait)
38+
39+
```php
40+
<?php
41+
declare(strict_types = 1);
42+
43+
class ExampleTest extends \VCR\VCRBundle\Tests\Functional\WebTestCase
44+
{
45+
public function test(): void
46+
{
47+
$kernel = static::bootKernel();
48+
/** @var \VCR\VCRBundle\VideoRecorderBrowser $client */
49+
$client = $kernel->getContainer()->get('test.client.vcr');
50+
51+
$client->insertVideoRecorderCassette('my-test-cassette-name');
52+
53+
// this is an example, normally services inside you project do stuff like this and you trigger them by
54+
// execute requests via the KernelBrowser client
55+
file_get_contents('https://www.google.de');
56+
57+
// cassette.path is configured to '%kernel.project_dir%/tests/Fixtures'
58+
// recordings are written to %kernel.project_dir%/tests/Fixtures/my-test-cassette-name
59+
// cassette.path + cassetteName (done by inserting the cassette)
60+
}
61+
}
62+
```
63+
64+
### VideoRecorderBrowser (with Trait)
65+
66+
```php
67+
<?php
68+
declare(strict_types = 1);
69+
70+
namespace MyCompany\MyProject\Tests\Functional;
71+
72+
class ExampleTest extends \VCR\VCRBundle\Tests\Functional\WebTestCase
73+
{
74+
use \VCR\VCRBundle\Test\VCRTestCaseTrait;
75+
76+
/**
77+
* Specify a namespace prefix which should be ignored while generating the base path for this test case.
78+
*/
79+
protected $ignoredTestSuiteNamespacePrefix = 'MyCompany\\MyProject\\Tests\\';
2780

81+
public function test(): void
82+
{
83+
/** @var \VCR\VCRBundle\VideoRecorderBrowser $client */
84+
$client = static::createVideoRecorderClient();
85+
86+
// this is an example, normally services inside you project do stuff like this and you trigger them by
87+
// execute requests via the KernelBrowser client
88+
file_get_contents('https://www.google.de');
89+
90+
// cassette.path is configured to '%kernel.project_dir%/tests/Fixtures'
91+
// recordings are written to %kernel.project_dir%/tests/Fixtures/Functional/ExampleTest/test
92+
// cassette.path + TestCasePath (- ignoredTestSuiteNamespacePrefix) + TestName
93+
}
94+
}
2895
```
2996

3097
## Configuration reference
3198

3299
```yaml
33100
vcr:
34-
enabled: true
35-
library_hooks:
36-
stream_wrapper: false
37-
curl: false
38-
soap: false
39-
request_matchers:
40-
method: true
41-
url: true
42-
query_string: true
43-
host: true
44-
headers: true
45-
body: true
46-
post_fields: true
47-
cassette:
48-
type: json
49-
path: '%kernel.cache_dir%/vcr'
50-
name: vcr
101+
enabled: true
102+
library_hooks:
103+
stream_wrapper: false
104+
curl: false
105+
soap: false
106+
request_matchers:
107+
method: true
108+
url: true
109+
query_string: true
110+
host: true
111+
headers: true
112+
body: true
113+
post_fields: true
114+
cassette:
115+
type: json
116+
path: '%kernel.cache_dir%/vcr'
117+
name: vcr
51118
```
52119
53120
## Credits
54121
55-
* [Kévin Gomez](http://github.com/K-Phoen/)
56-
* [Ludovic Fleury](https://github.com/ludofleury) - to whom I borrowed the
57-
design of the web profiler part from his [GuzzleBundle](https://github.com/ludofleury/GuzzleBundle/).
122+
* [Kévin Gomez](http://github.com/K-Phoen/)
123+
* [Ludovic Fleury](https://github.com/ludofleury) - to whom I borrowed the
124+
design of the web profiler part from his [GuzzleBundle](https://github.com/ludofleury/GuzzleBundle/).
125+
* [Simon Hübner](https://github.com/simonhard) - making the bundle SF 5.4 compatible
126+
* [Daniel Hürtgen](https://github.com/higidi) - making the bundle PHP 8 compatible and providing extra TestCase helper
58127
59128
## License
60129

composer.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
{
88
"name": "Kévin Gomez",
99
"email": "[email protected]"
10+
},
11+
{
12+
"name": "Daniel Hürtgen",
13+
"email": "[email protected]"
14+
},
15+
{
16+
"name": "Simon Hübner",
17+
"email": "[email protected]"
1018
}
1119
],
1220
"config": {
@@ -16,18 +24,20 @@
1624
"php": "^7.2|^8",
1725
"php-vcr/php-vcr": "^1.5",
1826
"symfony/config": "^4|^5",
27+
"symfony/browser-kit": "^4|^5",
1928
"symfony/dependency-injection": "^4|^5",
2029
"symfony/filesystem": "^4|^5",
2130
"symfony/event-dispatcher": "^4|^5",
31+
"symfony/framework-bundle": "^4.4|^5.4",
2232
"symfony/http-foundation": "^4|^5",
2333
"symfony/http-kernel": "^4.4|^5",
34+
"symfony/polyfill-php80": "^1.16",
2435
"symfony/yaml": "^4|^5"
2536
},
2637
"require-dev": {
2738
"dms/phpunit-arraysubset-asserts": "^0.4",
2839
"neutron/temporary-filesystem": "^3",
2940
"phpunit/phpunit": "^8.5|^9.5",
30-
"symfony/framework-bundle": "^4.4|^5.4",
3141
"symfony/phpunit-bridge": "^4.4|^5.4"
3242
},
3343
"autoload": {

src/Resources/config/services.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ services:
3838
calls:
3939
- [ setEventDispatcher, [ '@event_dispatcher' ] ]
4040
public: true
41+
42+
test.client.vcr:
43+
class: VCR\VCRBundle\VideoRecorderBrowser
44+
parent: test.client

0 commit comments

Comments
 (0)