The TestUserService class creates test users in MercadoPago’s sandbox environment. Test users are required for testing payment flows without processing real transactions.Source: src/Services/TestUserService.php:9
Test users are only for sandbox/testing environments. They provide test credentials and test payment methods for development and QA.
HTTP client for making direct API calls to MercadoPago. Automatically injected by Laravel’s service container.
Unlike other services, TestUserService uses SdkHttpClient instead of MercadoPagoClientFactory because the MercadoPago SDK doesn’t provide a dedicated test user client.
Create test users programmatically in PHPUnit tests:
use Fitodac\LaravelMercadoPago\Services\TestUserService;use Tests\TestCase;class MercadoPagoPaymentTest extends TestCase{ protected function setUp(): void { parent::setUp(); if (!config('mercadopago.testing')) { $this->markTestSkipped('MercadoPago testing not enabled'); } } public function test_can_create_payment() { // Create test user $testUser = app(TestUserService::class)->create([ 'site_id' => 'MLA', ]); $this->assertArrayHasKey('email', $testUser); $this->assertArrayHasKey('nickname', $testUser); // Use test user credentials for payment testing // ... }}
Restrict test user creation to appropriate environments:
use Fitodac\LaravelMercadoPago\Services\TestUserService;public function createTestUser(Request $request){ // Only allow in non-production environments if (app()->environment('production')) { abort(403, 'Test users cannot be created in production'); } $testUser = app(TestUserService::class)->create([ 'site_id' => $request->input('site_id', 'MLA'), ]); return response()->json($testUser);}
namespace App\Http\Middleware;use Closure;use Illuminate\Http\Request;class RequireTestingEnvironment{ public function handle(Request $request, Closure $next) { if (!app()->environment('local', 'testing', 'staging')) { abort(403, 'This endpoint is only available in testing environments'); } return $next($request); }}// Apply to routesRoute::middleware('testing.environment')->group(function () { Route::post('/test-users', [TestUserController::class, 'store']);});