PHP code example of daycry / phpunit-extension-vcr
1. Go to this page and download the library: Download daycry/phpunit-extension-vcr library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
daycry / phpunit-extension-vcr example snippets
use Daycry\PHPUnit\Vcr\Attributes\UseCassette;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[UseCassette("api_responses.yml")]
class ApiTest extends TestCase
{
#[Test]
public function testApiEndpoint(): void
{
// This HTTP request will be recorded to api_responses.yml
$response = file_get_contents('https://api.example.com/users');
$this->assertNotEmpty($response);
}
#[Test]
public function testAnotherEndpoint(): void
{
// This request will also be recorded to the same cassette
$response = file_get_contents('https://api.example.com/posts');
$this->assertNotEmpty($response);
}
}
use Daycry\PHPUnit\Vcr\Attributes\UseCassette;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
class MixedApiTest extends TestCase
{
#[Test]
#[UseCassette("users.yml")]
public function testUsersApi(): void
{
$response = file_get_contents('https://api.example.com/users');
$this->assertNotEmpty($response);
}
#[Test]
public function testWithoutRecording(): void
{
// This test won't use VCR - useful for unit tests
$this->assertTrue(true);
}
#[Test]
#[UseCassette("posts.yml")]
public function testPostsApi(): void
{
$response = file_get_contents('https://api.example.com/posts');
$this->assertNotEmpty($response);
}
}
use Daycry\PHPUnit\Vcr\Attributes\UseCassette;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[UseCassette("default.yml")] // Default cassette for the class
class OverrideTest extends TestCase
{
#[Test]
public function testUsesDefaultCassette(): void
{
// Uses default.yml
$response = file_get_contents('https://api.example.com/default');
$this->assertNotEmpty($response);
}
#[Test]
#[UseCassette("special.yml")] // Override for this specific test
public function testUsesSpecificCassette(): void
{
// Uses special.yml instead of default.yml
$response = file_get_contents('https://api.example.com/special');
$this->assertNotEmpty($response);
}
}
use Daycry\PHPUnit\Vcr\Attributes\UseCassette;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[UseCassette("http_clients.yml")]
class HttpClientTest extends TestCase
{
#[Test]
public function testFileGetContents(): void
{
$response = file_get_contents('https://httpbin.org/get');
$this->assertStringContainsString('httpbin.org', $response);
}
#[Test]
public function testCurl(): void
{
$ch = curl_init('https://httpbin.org/get');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$this->assertNotFalse($response);
}
#[Test]
public function testStreamContext(): void
{
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => 'Content-Type: application/json',
'content' => json_encode(['key' => 'value'])
]
]);
$response = file_get_contents('https://httpbin.org/post', false, $context);
$this->assertNotEmpty($response);
}
}
use Daycry\PHPUnit\Vcr\Attributes\UseCassette;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[UseCassette("data_provider_tests.yml")]
class DataProviderTest extends TestCase
{
public static function urlProvider(): array
{
return [
'users endpoint' => ['https://api.example.com/users'],
'posts endpoint' => ['https://api.example.com/posts'],
'comments endpoint' => ['https://api.example.com/comments'],
];
}
#[Test]
#[DataProvider('urlProvider')]
public function testMultipleEndpoints(string $url): void
{
$response = file_get_contents($url);
$this->assertNotEmpty($response);
$data = json_decode($response, true);
$this->assertIsArray($data);
}
}
#[UseCassette("github_api.yml")]
class GitHubApiTest extends TestCase
{
#[Test]
public function testFetchingUserRepositories(): void
{
$client = new GitHubApiClient();
$repos = $client->getUserRepositories('octocat');
$this->assertCount(8, $repos);
$this->assertEquals('Hello-World', $repos[0]['name']);
}
}
#[UseCassette("payment_service.yml")]
class PaymentServiceTest extends TestCase
{
#[Test]
public function testProcessPayment(): void
{
$service = new PaymentService();
$result = $service->processPayment(100.00, 'USD');
$this->assertTrue($result->isSuccessful());
$this->assertEquals('tx_123456', $result->getTransactionId());
}
}
#[UseCassette("webhook_verification.yml")]
class WebhookTest extends TestCase
{
#[Test]
public function testWebhookSignatureVerification(): void
{
$handler = new WebhookHandler();
// This will record the HTTP request to the verification service
$isValid = $handler->verifySignature($payload, $signature);
$this->assertTrue($isValid);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.