Skip to content

Chore/readme updates #43

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

Closed
Closed
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
45 changes: 45 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: CI Tests

on:
pull_request:
push:
branches:
- master
- v*

jobs:
tests:
runs-on: ubuntu-22.04

strategy:
fail-fast: false
matrix:
stability: [prefer-stable]
versions: [
{ php: 8.1, phpunit: 10 }, { php: 8.2, phpunit: 10 }, { php: 8.3, phpunit: 10 },
{ php: 8.2, phpunit: 11 }, { php: 8.3, phpunit: 11 }
]

name: PHP ${{ matrix.php }} - PHPUnit ${{ matrix.phpunit }}

steps:
- name: Checkout code
uses: actions/checkout@v4

# Docs: https://github.com/shivammathur/setup-php
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.versions.php }}
extensions: dom, curl, libxml, mbstring, zip
ini-values: error_reporting=E_ALL
tools: composer:v2
coverage: none

- name: Install dependencies
run: |
composer require "phpunit/phpunit=^${{ matrix.versions.phpunit }}" --no-update
composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader

- name: Run phpunit tests
run: composer test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
**/vendor/*
coverage/*
composer.lock
/.phpunit.result.cache
phpunit.xml
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ matrix:
- env: COMPOSER_FLAGS="--prefer-lowest"
php: 7.1
- php: 7.2
- php: 8.0
dist: bionic
- php: 8.1.0
dist: bionic

before_install:
- composer self-update
Expand Down
42 changes: 32 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# PHPUnit TestListener for PHP-VCR

Integrates PHPUnit with [PHP-VCR](http://github.com/php-vcr/php-vcr) using annotations.
[![CI Tests](https://github.com/covergenius/phpunit-testlistener-vcr/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/covergenius/phpunit-testlistener-vcr/actions)
[![License](https://img.shields.io/packagist/l/covergenius/phpunit-testlistener-vcr.svg?style=flat-square)](LICENSE)
[![Development Version](https://img.shields.io/packagist/vpre/covergenius/phpunit-testlistener-vcr.svg?style=flat-square)](https://packagist.org/packages/covergenius/phpunit-testlistener-vcr)
[![Monthly Installs](https://img.shields.io/packagist/dm/covergenius/phpunit-testlistener-vcr.svg?style=flat-square)](https://packagist.org/packages/covergenius/phpunit-testlistener-vcr)

Integrates PHPUnit with [PHP-VCR](http://github.com/covergenius/php-vcr) using annotations.

![PHP-VCR](https://user-images.githubusercontent.com/133832/27151811-0d95c6c4-514c-11e7-834e-eff1eec2ea16.png)

Use `@vcr cassette_name` on your tests to turn VCR automatically on and off.

[![Build Status](https://travis-ci.org/php-vcr/phpunit-testlistener-vcr.svg?branch=master)](https://travis-ci.org/php-vcr/phpunit-testlistener-vcr)


## Usage example

Expand All @@ -33,23 +38,40 @@ class VCRTest extends TestCase
1) Install using composer:

``` sh
composer require --dev php-vcr/phpunit-testlistener-vcr
composer require --dev covergenius/phpunit-testlistener-vcr
```

### For phpunit version 10+

2) Add listener to your `phpunit.xml`:

``` xml
<extensions>
<bootstrap class="VCR\PHPUnit\TestListener\VCRTestListener" />
</extensions>
```

### For phpunit version 9 and below
``` xml
<listeners>
<listener class="VCR\PHPUnit\TestListener\VCRTestListener" file="vendor/php-vcr/phpunit-testlistener-vcr/src/VCRTestListener.php" />
<listener class="VCR\PHPUnit\TestListener\VCRTestListener" file="vendor/covergenius/phpunit-testlistener-vcr/src/VCRTestListener.php" />
</listeners>
```

## Dependencies
PHPUnit-Testlistener-VCR depends on the following;

* [php-vcr/php-vcr](https://github.com/covergenius/php-vcr)

### Version 4
* PHP 8.1+
* PHPUnit >=10

### Version 3

* PHP 7.1+
* PHPUnit <10

PHPUnit-Testlistener-VCR depends on:
* PHP 7.1+
* PHP 7.0+ (use <3.0)
* [php-vcr/php-vcr](https://github.com/php-vcr/php-vcr)

## Run tests

Expand All @@ -62,8 +84,8 @@ composer install

## Changelog

**The changelog is manage at [PHPUnit testlistener for PHP-VCR releases page](https://github.com/php-vcr/phpunit-testlistener-vcr/releases).**
**The changelog is manage at [PHPUnit testlistener for PHP-VCR releases page](https://github.com/covergenius/covergenius/phpunit-testlistener-vcr/releases).**

## Copyright
Copyright (c) 2013-2018 Adrian Philipp. Released under the terms of the MIT license. See LICENSE for details.
[Contributors](https://github.com/php-vcr/phpunit-testlistener-vcr/graphs/contributors)
[Contributors](https://github.com/covergenius/phpunit-testlistener-vcr/graphs/contributors)
10 changes: 4 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "php-vcr/phpunit-testlistener-vcr",
"name": "covergenius/phpunit-testlistener-vcr",
"description": "Integrates PHPUnit with PHP-VCR.",
"license": "MIT",
"authors": [
Expand All @@ -19,10 +19,8 @@
}
},
"require": {
"php-vcr/php-vcr": "^1.4",
"php": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^7.0"
"covergenius/php-vcr": "^1.7",
"php": "^8.1",
"phpunit/phpunit": "^10.4 | ^11"
}
}
52 changes: 17 additions & 35 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/7.0/phpunit.xsd"
backupGlobals="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
colors="true"
bootstrap="tests/bootstrap.php"
cacheTokens="true"
forceCoversAnnotation="false"
printerClass="PHPUnit\TextUI\ResultPrinter"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
testSuiteLoaderClass="PHPUnit\Runner\StandardTestSuiteLoader"
verbose="true"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="tests/bootstrap.php"
colors="true"
backupGlobals="false"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
>

<testsuites>
<testsuite name="PHPUnit VCR Listener Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>

<listeners>
<listener class="VCR\PHPUnit\TestListener\VCRTestListener" file="src/VCRTestListener.php" />
</listeners>

<testsuites>
<testsuite name="PHPUnit VCR Listener Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<extensions>
<bootstrap class="VCR\PHPUnit\TestListener\VCRTestListener" />
</extensions>
</phpunit>
20 changes: 20 additions & 0 deletions src/Subscribers/BeforeTestEnds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace VCR\PHPUnit\TestListener\Subscribers;

use PHPUnit\Event;
use VCR\VCR;

class BeforeTestEnds implements Event\Test\BeforeTestMethodFinishedSubscriber
{
/**
* @param Event\Test\BeforeTestMethodFinished $event
* @return void
*/
public function notify(Event\Test\BeforeTestMethodFinished $event): void
{
VCR::turnOff();
}
}
93 changes: 93 additions & 0 deletions src/Subscribers/OnTestPrepared.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace VCR\PHPUnit\TestListener\Subscribers;

use PHPUnit\Event;
use PHPUnit\Event\Code\Test;
use PHPUnit\Event\Test\Prepared;
use VCR\VCR;

class OnTestPrepared implements Event\Test\PreparedSubscriber
{
/**
* @param Prepared $event
* @return void
* @throws \ReflectionException
*/
public function notify(Prepared $event): void
{
$cassette = $this->getCassette($event->test());

if (empty($cassette)) {
return;
}

// Use the JSON storage format if applicable
if (str_ends_with('.json', $cassette)) {
VCR::configure()->setStorage('json');
}

VCR::turnOn();
VCR::insertCassette($cassette);
}

/**
* @param Test $test
* @param string $tag
* @return string|null
* @throws \ReflectionException
*/
public function getCassette(Test $test, string $tag = '@vcr'): ?string
{
$reflection = new \ReflectionClass($test);
$class = $reflection->getProperty('className')->getValue($test);
if ($test instanceof Event\Code\TestMethod) {
$method = $test->methodName();
} else {
$method = $test->name();
}

$reflection = new \ReflectionMethod($class, $method);
$docblock = $reflection->getDocComment();

if (!empty($docblock)) {
$parsed = self::parseDocBlock($docblock, $tag);
return array_pop($parsed);
}

return null;
}

/**
* @param string $docBlock
* @param string $tag
* @return array<string>
*/
private static function parseDocBlock(string $docBlock, string $tag): array
{
$matches = [];

if (empty($docBlock)) {
return $matches;
}

$regex = "/{$tag} (.*)(\\r\\n|\\r|\\n)/U";
preg_match_all($regex, $docBlock, $matches);

if (empty($matches[1])) {
return array();
}

// Removed extra index
$matches = $matches[1];

// Trim the results, array item by array item
foreach ($matches as $ix => $match) {
$matches[$ix] = trim($match);
}

return $matches;
}
}
Loading