php-vcr/phpunit-testlistener-vcr

Add option to specify cassette path

tylercollier opened this issue · 5 comments

What a great package!

I wrote a change that allows you to specify the cassette path.

I didn't create a pull request because unfortunately I'm not sure how to handle the testing situation. I couldn't think of how to change the phpunit.xml's listeners dynamically for the sake of the two different tests.

Here's the diff of what I've done: tylercollier/phpunit-testlistener-vcr@5cce21b...ecfc580

One solution I can see is to parse the doc block differently. Perhaps multiple options could be allowed, one for the cassette path and one for the cassette name. While that would add the flexibility of changing the cassette path per test, it would remove the convenience of setting it in one place.

I'm sure someone's dealt with this general problem before but my google search didn't turn up anything helpful. Any thoughts?

adri commented

Thanks @tylercollier!

Does the cassette path in your case change during testing? I always put a configuration block in the tests/bootstrap.php file like this:

\VCR\VCR::configure()
    ->setCassettePath('tests/fixtures/http');

More info: http://php-vcr.github.io/documentation/configuration/
Not sure if I would bother adding it to the annotation. For me it is more a general setting and just set it once in the beginning. What do you think?

I'm using CakePHP and tests are in app/Test and the vendor code goes into app/Vendor, so my phpunit.xml looks like this:

<phpunit>
    <listeners>
        <listener class="PHPUnit_Util_Log_VCR" file="Vendor/php-vcr/phpunit-testlistener-vcr/PHPUnit/Util/Log/VCR.php">
            <arguments>
                <array>
                    <element key="cassette_path">
                        <string>Test/Cassettes</string>
                    </element>
                </array>
            </arguments>
        </listener>
    </listeners>
</phpunit>

I'm not sure if you're asking if my cassette path changes for different tests; it doesn't. I just have one fixtures/cassettes directory currently. If it did, I'd need a different fix. The fix I put in is working, the problem is the test. Ideally I'd have a listener for the testInterceptsWithAnnotations and a different listener for the testCanSpecifyAlternativeCassettePath, the latter having the <arguments/> node. I don't know how to make that happen.

If you'll accept a pull request without a test for the functionality, I can submit one. But it feels wrong not to have a test. I just can't think of how to test it!

adri commented

If your cassette path doesn't change for different tests, I wouldn't introduce any new annotations but use the already existing way to configure PHP-VCR.

This is how it works: In your phpunit.xml you can add a bootstrap file:

<phpunit bootstrap="Tests/bootstrap.php">
...

In the Tests/bootstrap.php add:

// if you are using composer, you can load the autoloader here
require_once __DIR__ . '/../vendor/autoload.php';

\VCR\VCR::configure()->setCassettePath('Test/Cassettes');
\VCR\VCR::turnOn();

One reason for doing it like this is, that there are other testing library integrations than PHPUnit (phpspec, behat, ...) planned. When there is a new configuration option I would have to add that in every integration. Using the bootstrap-file works in every case and eases development.

Oh now I see. By using the bootstrap, it obviates the need for my change completely! It's actually a better solution than going through goofy XML. Thanks.

adri commented

Ok cool :-) I'm glad you are using php-vcr. If there are any issues or other feedback please let me know. Have a nice day!