Skip to content
Merged
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
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
}
},
"require": {
"php-vcr/php-vcr": "^1.4"
"php-vcr/php-vcr": "^1.4",
"php": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^7"
"phpunit/phpunit": "^7.0"
}
}
7 changes: 1 addition & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/7.0/phpunit.xsd"
backupGlobals="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand All @@ -10,15 +10,13 @@
bootstrap="tests/bootstrap.php"
cacheTokens="true"
forceCoversAnnotation="false"
mapTestClassNameToCoveredClassName="false"
printerClass="PHPUnit\TextUI\ResultPrinter"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
testSuiteLoaderClass="PHPUnit\Runner\StandardTestSuiteLoader"
strict="false"
verbose="true"
>

Expand All @@ -29,9 +27,6 @@
</testsuites>

<filter>
<blacklist>
<directory>./vendor</directory>
</blacklist>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
Expand Down
167 changes: 37 additions & 130 deletions src/VCRTestListener.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
declare(strict_types=1);

namespace VCR\PHPUnit\TestListener;

use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Framework\Warning;
Expand All @@ -15,124 +17,21 @@
* Here is an example XML configuration for activating this listener:
*
* <code>
* <listeners>
* <listener class="VCR\PHPUnit\TestListener\VCRTestListener" file="vendor/php-vcr/phpunit-testlistener-vcr/src/VCRTestListener.php" />
* </listeners>
* <listeners>
* <listener class="VCR\PHPUnit\TestListener\VCRTestListener" file="vendor/php-vcr/phpunit-testlistener-vcr/src/VCRTestListener.php" />
* </listeners>
* </code>
*
* @author Adrian Philipp <mail@adrian-philipp.com>
* @author Adrian Philipp <mail@adrian-philipp.com>
* @author Davide Borsatto <davide.borsatto@gmail.com>
* @copyright 2011-2017 Adrian Philipp <mail@adrian-philipp.com>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
*
* @version Release: @package_version@
*
* @see http://www.phpunit.de/
* @author Renato Mefi <gh@mefi.in>
*/
class VCRTestListener implements TestListener
final class VCRTestListener implements TestListener
{
/**
* @var array
*/
protected $runs = array();

/**
* @var array
*/
protected $options = array();

/**
* @var int
*/
protected $suites = 0;

/**
* Constructor.
*
* @param array $options
*/
public function __construct(array $options = array())
{
}

/**
* An error occurred.
*
* @param Test $test
* @param Exception $e
* @param float $time
*/
public function addError(Test $test, \Throwable $t, float $time): void
{
}

/**
* A warning occurred.
*
* @param Test $test
* @param Warning $e
* @param float $time
*
* @since Method available since Release 5.1.0
*/
public function addWarning(Test $test, Warning $e, float $time): void
{
}

/**
* A failure occurred.
*
* @param Test $test
* @param AssertionFailedError $e
* @param float $time
*/
public function addFailure(Test $test, AssertionFailedError $e, float $time): void
{
}

/**
* Incomplete test.
*
* @param Test $test
* @param \Exception $e
* @param float $time
*/
public function addIncompleteTest(Test $test, \Throwable $e, float $time): void
{
}

/**
* Skipped test.
*
* @param Test $test
* @param \Exception $e
* @param float $time
*/
public function addSkippedTest(Test $test, \Throwable $e, float $time): void
{
}

/**
* Risky test.
*
* @param Test $test
* @param \Exception $e
* @param float $time
*/
public function addRiskyTest(Test $test, \Throwable $e, float $time): void
{
}

/**
* A test started.
*
* @param Test $test
*
* @return bool|null
*/
public function startTest(Test $test): void
{
$class = get_class($test);
$class = \get_class($test);
\assert($test instanceof TestCase);
$method = $test->getName(false);

if (!method_exists($class, $method)) {
Expand All @@ -147,7 +46,7 @@ public function startTest(Test $test): void
$cassetteName = array_pop($parsed);

// If the cassette name ends in .json, then use the JSON storage format
if (substr($cassetteName, '-5') == '.json') {
if (substr($cassetteName, -5) === '.json') {
VCR::configure()->setStorage('json');
}

Expand All @@ -159,9 +58,9 @@ public function startTest(Test $test): void
VCR::insertCassette($cassetteName);
}

private static function parseDocBlock($docBlock, $tag)
private static function parseDocBlock($docBlock, $tag): array
{
$matches = array();
$matches = [];

if (empty($docBlock)) {
return $matches;
Expand All @@ -185,31 +84,39 @@ private static function parseDocBlock($docBlock, $tag)
return $matches;
}

/**
* A test ended.
*
* @param Test $test
* @param float $time
*/
public function endTest(Test $test, float $time): void
{
VCR::turnOff();
}

/**
* A test suite started.
*
* @param TestSuite $suite
*/
public function addError(Test $test, \Throwable $t, float $time): void
{
}

public function addWarning(Test $test, Warning $e, float $time): void
{
}

public function addFailure(Test $test, AssertionFailedError $e, float $time): void
{
}

public function addIncompleteTest(Test $test, \Throwable $e, float $time): void
{
}

public function addSkippedTest(Test $test, \Throwable $e, float $time): void
{
}

public function addRiskyTest(Test $test, \Throwable $e, float $time): void
{
}

public function startTestSuite(TestSuite $suite): void
{
}

/**
* A test suite ended.
*
* @param TestSuite $suite
*/
public function endTestSuite(TestSuite $suite): void
{
}
Expand Down
27 changes: 14 additions & 13 deletions tests/VCRTestListenerTest.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
<?php
declare(strict_types=1);

namespace Tests\VCR\PHPUnit\TestListener;

use PHPUnit\Framework\TestCase;

/**
* Test integration of PHPVCR with PHPUnit using annotations.
*/
class VCRTestListenerTest extends TestCase
final class VCRTestListenerTest extends TestCase
{
/**
* @vcr unittest_annotation_test
*/
public function testInterceptsWithAnnotations()
public function testInterceptsWithAnnotations(): void
{
// Content of tests/fixtures/unittest_annotation_test: "This is a annotation test dummy".
$result = file_get_contents('http://google.com');
Expand All @@ -22,28 +20,31 @@ public function testInterceptsWithAnnotations()
/**
* @vcr unittest_annotation_test.yml
*/
public function testInterceptsWithAnnotationsAndFileExtension()
public function testInterceptsWithAnnotationsAndFileExtension(): void
{
$result = file_get_contents('http://google.com');
$this->assertEquals('This is a annotation test dummy.', $result, 'Call was not intercepted (using annotations).');
}

/**
* @vcr unittest_annotation_test
* @dataProvider aDataProvider
*
* @dataProvider dummyDataProvider
*/
public function testInterceptsWithAnnotationsWhenUsingDataProvider($dummyValue)
public function testInterceptsWithAnnotationsWhenUsingDataProvider(int $dummyValue): void
{
// Content of tests/fixtures/unittest_annotation_test: "This is an annotation test dummy".
$result = file_get_contents('http://google.com');
// Just to avoid the dummy to annoy the static analyzers
\assert(\is_int($dummyValue));
$this->assertEquals('This is a annotation test dummy.', $result, 'Call was not intercepted (using annotations with data provider).');
}

public function aDataProvider()
public function dummyDataProvider(): array
{
return array(
array(1),
array(2),
);
return [
[1],
[2],
];
}
}