diff --git a/src/Extension/TestFinishedSubscriber.php b/src/Extension/TestFinishedSubscriber.php new file mode 100644 index 0000000..1fe8273 --- /dev/null +++ b/src/Extension/TestFinishedSubscriber.php @@ -0,0 +1,17 @@ +test(); + if (!$testMethod instanceof TestMethod) { + return; + } + + VCRTestHandler::onStart($testMethod->className(), $testMethod->name()); + } +} diff --git a/src/Extension/VCRExtension.php b/src/Extension/VCRExtension.php new file mode 100644 index 0000000..a99b63e --- /dev/null +++ b/src/Extension/VCRExtension.php @@ -0,0 +1,32 @@ += 10. + * + * + * + * + * + * + */ +final class VCRExtension implements Extension +{ + public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void + { + $facade->registerSubscribers( + new TestPreparationStartedSubscriber(), + new TestFinishedSubscriber() + ); + } +} diff --git a/src/VCRTestHandler.php b/src/VCRTestHandler.php new file mode 100644 index 0000000..01dc6b6 --- /dev/null +++ b/src/VCRTestHandler.php @@ -0,0 +1,74 @@ +getDocComment(); + + // Use regex to parse the doc_block for a specific annotation + $parsed = self::parseDocBlock($docBlock, '@vcr'); + $cassetteName = array_pop($parsed); + + if (empty($cassetteName)) { + return; + } + + // If the cassette name ends in .json, then use the JSON storage format + if (substr($cassetteName, -5) === '.json') { + VCR::configure()->setStorage('json'); + } + + VCR::turnOn(); + VCR::insertCassette($cassetteName); + } + + public static function onEnd(): void + { + VCR::turnOff(); + } + + private static function parseDocBlock($docBlock, $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; + } +} diff --git a/src/VCRTestListener.php b/src/VCRTestListener.php index 0c8cae2..fe4a1b3 100644 --- a/src/VCRTestListener.php +++ b/src/VCRTestListener.php @@ -14,7 +14,7 @@ /** * A TestListener that integrates with PHP-VCR. * - * Here is an example XML configuration for activating this listener: + * Here is an example XML configuration for activating this listener in PHPUnit < 10. * * * @@ -32,61 +32,13 @@ public function startTest(Test $test): void { $class = \get_class($test); \assert($test instanceof TestCase); - $method = $test->getName(false); - if (!method_exists($class, $method)) { - return; - } - - $reflection = new \ReflectionMethod($class, $method); - $docBlock = $reflection->getDocComment(); - - // Use regex to parse the doc_block for a specific annotation - $parsed = self::parseDocBlock($docBlock, '@vcr'); - $cassetteName = array_pop($parsed); - - if (empty($cassetteName)) { - return; - } - - // If the cassette name ends in .json, then use the JSON storage format - if (substr($cassetteName, -5) === '.json') { - VCR::configure()->setStorage('json'); - } - - VCR::turnOn(); - VCR::insertCassette($cassetteName); - } - - private static function parseDocBlock($docBlock, $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; + VCRTestHandler::onStart($class, $test->getName(false)); } public function endTest(Test $test, float $time): void { - VCR::turnOff(); + VCRTestHandler::onEnd(); } public function addError(Test $test, \Throwable $t, float $time): void