@@ -30,41 +30,78 @@ 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
3943<?php
44+ require 'vendor/autoload.php';
45+
46+ use VoltTest\DataSourceConfiguration;
4047use VoltTest\VoltTest;
4148
4249// 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
50+ $test = new VoltTest(
51+ 'User Login Flow',
52+ 'Tests user authentication process'
53+ );
54+ // Configure test parameters
55+ $test
56+ ->setVirtualUsers(1) // number of VUs (Virtual Users)
57+ // ->setDuration('1s') // to run the test for 1 second
58+ ->setHttpDebug(true); // to enable http debug mode remove it if you don't want to see the http debug
59+
60+ // Create login scenario
61+ $loginScenario = $test->scenario('User Login')
62+ ->autoHandleCookies() // this will save cookies with all requests in this scenario
63+ ->setDataSourceConfiguration(new DataSourceConfiguration(__DIR__ .'/data-file.csv', 'sequential', true));
64+
65+
66+ $loginScenario->step('Register')
67+ ->get('http://localhost:8001/register')
68+ ->extractFromRegex('csrf_token_register', 'name="_token" value="(.+?)"') // Extract the csrf token to submit a form
69+ ->header('Accept', 'text/html');
70+
71+ $loginScenario->step('Submit Register')
72+ ->post(
73+ 'http://localhost:8001/register',
74+ '_token=${csrf_token_register}&name=Test-v&email=${email}&password=${password}&password_confirmation=${password}') // send data with extracted data and source file
75+ ->header('Content-Type', 'application/x-www-form-urlencoded')
76+ ->validateStatus('status validator from php',302);
77+
78+
79+ // Add first step - Get login page
80+ $loginScenario->step('get_login_page')
81+ ->get('http://localhost:8001/login')
82+ ->header('Accept', 'text/html')
83+ ->extractFromRegex('csrf_token', 'name="_token" value="(.+?)"')
84+ ->validateStatus('status validator from php',200);
85+
86+ // Add second step - Submit login
87+ $loginScenario->step('submit_login')
88+ ->post(
89+ 'http://localhost:8001/login',
90+ '_token=${csrf_token}&email=${email}&password=${password}')
91+ ->header('Content-Type', 'application/x-www-form-urlencoded')
92+ ->validateStatus('status validator from php',302);
93+
94+
95+ // Add third step - Visit dashboard
96+ $loginScenario->step('visit_dashboard')
97+ ->get('http://localhost:8001/dashboard')
98+ ->header('Accept', 'text/html')
99+ ->validateStatus('status_code', 200);
100+ // Run the test
101+ // This will start the test and block until it completes
102+ // pass true to run() to run the test with progress and real time results
67103$result = $test->run();
104+ // OR $test->run(true); to run the test with progress and real time results
68105
69106// Access test results
70107echo "Success Rate: " . $result->getSuccessRate() . "%\n";
0 commit comments