@@ -30,47 +30,89 @@ For detailed documentation, visit [https://php.volt-test.com](https://php.volt-t
3030### Installation
3131
3232``` bash
33- composer require volt-test/php-sdk
33+ git clone
[email protected] :volt-test/php-sdk.git
34+ cd php-sdk
35+ composer install
3436```
3537
3638### Basic Usage
37-
39+ ``` bash
40+ touch test.php
41+ ```
3842``` php
43+ // test.php
3944<?php
45+ require 'vendor/autoload.php';
46+
47+ use VoltTest\DataSourceConfiguration;
4048use VoltTest\VoltTest;
4149
4250// Create a new test
43- $test = new VoltTest('API Load Test', 'Testing API endpoints under load');
44-
45- // Configure the test
46- $test->setVirtualUsers(10)
47- ->setDuration('1m')
48- ->setRampUp('10s')
49- ->setTarget('https://api.example.com');
50-
51- // Create a test scenario
52- $scenario = $test->scenario('Basic API Flow')
53- ->setWeight(1)
54- ->autoHandleCookies();
55-
56- // Add test steps
57- $scenario->step('Get Users')
58- ->get('/api/users')
59- ->validateStatus('success', 200)
60- ->extractFromJson('userId', 'data[0].id');
61-
62- $scenario->step('Get User Details')
63- ->get('/api/users/${userId}')
64- ->validateStatus('success', 200);
65-
66- // Run the test
51+ $test = new VoltTest(
52+ 'User Login Flow',
53+ 'Tests user authentication process'
54+ );
55+ // Configure test parameters
56+ $test
57+ ->setVirtualUsers(1) // number of VUs (Virtual Users)
58+ // ->setDuration('1s') // to run the test for 1 second
59+ ->setHttpDebug(true); // to enable http debug mode remove it if you don't want to see the http debug
60+
61+ // Create login scenario
62+ $loginScenario = $test->scenario('User Login')
63+ ->autoHandleCookies() // this will save cookies with all requests in this scenario
64+ ->setDataSourceConfiguration(new DataSourceConfiguration(__DIR__ .'/data-file.csv', 'sequential', true));
65+
66+
67+ $loginScenario->step('Register')
68+ ->get('http://localhost:8001/register')
69+ ->extractFromRegex('csrf_token_register', 'name="_token" value="(.+?)"') // Extract the csrf token to submit a form
70+ ->header('Accept', 'text/html');
71+
72+ $loginScenario->step('Submit Register')
73+ ->post(
74+ 'http://localhost:8001/register',
75+ '_token=${csrf_token_register}&name=Test-v&email=${email}&password=${password}&password_confirmation=${password}') // send data with extracted data and source file
76+ ->header('Content-Type', 'application/x-www-form-urlencoded')
77+ ->validateStatus('status validator from php',302);
78+
79+
80+ // Add first step - Get login page
81+ $loginScenario->step('get_login_page')
82+ ->get('http://localhost:8001/login')
83+ ->header('Accept', 'text/html')
84+ ->extractFromRegex('csrf_token', 'name="_token" value="(.+?)"')
85+ ->validateStatus('status validator from php',200);
86+
87+ // Add second step - Submit login
88+ $loginScenario->step('submit_login')
89+ ->post(
90+ 'http://localhost:8001/login',
91+ '_token=${csrf_token}&email=${email}&password=${password}')
92+ ->header('Content-Type', 'application/x-www-form-urlencoded')
93+ ->validateStatus('status validator from php',302);
94+
95+
96+ // Add third step - Visit dashboard
97+ $loginScenario->step('visit_dashboard')
98+ ->get('http://localhost:8001/dashboard')
99+ ->header('Accept', 'text/html')
100+ ->validateStatus('status_code', 200);
101+ // Run the test
102+ // This will start the test and block until it completes
103+ // pass true to run() to run the test with progress and real time results
67104$result = $test->run();
105+ // OR $test->run(true); to run the test with progress and real time results
68106
69107// Access test results
70108echo "Success Rate: " . $result->getSuccessRate() . "%\n";
71109echo "Average Response Time: " . $result->getAvgResponseTime() . "\n";
72110```
73111
112+ ``` bash
113+ php test.php
114+ ```
115+
74116## Features
75117
76118- Cross-platform support (Windows, Linux, MacOS)
0 commit comments