Skip to content

Commit b6d564e

Browse files
authored
release/v0.9.2 (#23)
* prepare for relase v0.9.2 * feat(extractor): Add HTMLExtractor to PHP SDK for structured HTML data extraction (#24) * feat(extractor): Add HTMLExtractor to PHP SDK for structured HTML data extraction - Implemented `HTMLExtractor.php` to define HTML extraction rules in the Volt-Test PHP SDK. - Supports: - CSS selectors for targeting specific elements. - Extracting text content from nested elements. - Extracting attributes (e.g., `value`, `href`, `src`). - Added `toArray()` method for serialization, ensuring consistency with existing extractors. - Aligns with the existing `JsonExtractor`, `RegexExtractor`, `HeaderExtractor`, and `CookieExtractor`.
1 parent ec99713 commit b6d564e

File tree

6 files changed

+160
-2
lines changed

6 files changed

+160
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "volt-test/php-sdk",
33
"description": "Volt Test PHP SDK - A performance testing tool for PHP applications",
44
"type": "library",
5-
"version": "0.9.1",
5+
"version": "0.9.2",
66
"keywords": [
77
"volt-test",
88
"php-sdk",

src/Extractors/HtmlExtractor.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace VoltTest\Extractors;
4+
5+
use VoltTest\Exceptions\VoltTestException;
6+
7+
class HtmlExtractor implements Extractor
8+
{
9+
10+
private string $variableName;
11+
12+
private string $selector;
13+
14+
private ?string $attribute;
15+
16+
public function __construct(string $variableName, string $selector, ?string $attribute = null)
17+
{
18+
$this->variableName = $variableName;
19+
$this->selector = $selector;
20+
$this->attribute = $attribute;
21+
}
22+
23+
public function toArray(): array
24+
{
25+
if (! $this->validate()) {
26+
throw new VoltTestException('Invalid HTML Extractor');
27+
}
28+
29+
return [
30+
'variable_name' => $this->variableName,
31+
'selector' => $this->selector,
32+
'attribute' => $this->attribute,
33+
'type' => 'html',
34+
];
35+
}
36+
37+
public function validate(): bool
38+
{
39+
if (trim($this->variableName) === '') {
40+
return false;
41+
}
42+
43+
// Check for empty or whitespace-only selector
44+
if (trim($this->selector) === '') {
45+
return false;
46+
}
47+
48+
// Check for empty or whitespace-only attribute
49+
if ($this->attribute !== null && trim($this->attribute) === '') {
50+
return false;
51+
}
52+
53+
return true;
54+
}
55+
}

src/Platform.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Platform
66
{
77
private const BINARY_NAME = 'volt-test';
88

9-
private const ENGINE_CURRENT_VERSION = 'v0.9.1';
9+
private const ENGINE_CURRENT_VERSION = 'v0.9.2';
1010
private const BASE_DOWNLOAD_URL = 'https://github.com/volt-test/binaries/releases/download';
1111
private const SUPPORTED_PLATFORMS = [
1212
'linux-amd64' => 'volt-test-linux-amd64',

src/Step.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use VoltTest\Extractors\CookieExtractor;
1111
use VoltTest\Extractors\Extractor;
1212
use VoltTest\Extractors\HeaderExtractor;
13+
use VoltTest\Extractors\HtmlExtractor;
1314
use VoltTest\Extractors\JsonExtractor;
1415
use VoltTest\Extractors\RegexExtractor;
1516
use VoltTest\Validators\StatusValidator;
@@ -191,6 +192,14 @@ public function extractFromRegex(string $variableName, string $selector): self
191192
return $this;
192193
}
193194

195+
196+
public function extractFromHtml(string $variableName, string $selector, ?string $attribute = null): self
197+
{
198+
$htmlExtractor = new HtmlExtractor($variableName, $selector, $attribute);
199+
$this->extracts[] = $htmlExtractor;
200+
return $this;
201+
}
202+
194203
public function validateStatus(string $name, int $expected): self
195204
{
196205
$this->validations[] = new StatusValidator($name, $expected);

tests/Units/HtmlExtractorTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Tests\Units;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class HtmlExtractorTest extends TestCase
8+
{
9+
private \VoltTest\Extractors\HtmlExtractor $htmlExtractor;
10+
11+
protected function setUp(): void
12+
{
13+
$this->htmlExtractor = new \VoltTest\Extractors\HtmlExtractor('testVar', 'div#test', 'data-id');
14+
}
15+
16+
public function testToArray(): void
17+
{
18+
$expected = [
19+
'variable_name' => 'testVar',
20+
'selector' => 'div#test',
21+
'attribute' => 'data-id',
22+
'type' => 'html',
23+
];
24+
$this->assertEquals($expected, $this->htmlExtractor->toArray());
25+
}
26+
27+
public function testValidate(): void
28+
{
29+
$this->assertTrue($this->htmlExtractor->validate());
30+
}
31+
32+
public function testEmptyVariableName(): void
33+
{
34+
$extractor = new \VoltTest\Extractors\HtmlExtractor('', 'div#test', 'data-id');
35+
$this->assertFalse($extractor->validate());
36+
}
37+
38+
public function testEmptySelector(): void
39+
{
40+
$extractor = new \VoltTest\Extractors\HtmlExtractor('testVar', '', 'data-id');
41+
$this->assertFalse($extractor->validate());
42+
}
43+
44+
public function testEmptyAttribute(): void
45+
{
46+
$extractor = new \VoltTest\Extractors\HtmlExtractor('testVar', 'div#test', '');
47+
$this->assertFalse($extractor->validate());
48+
}
49+
50+
public function testNullAttribute(): void
51+
{
52+
$extractor = new \VoltTest\Extractors\HtmlExtractor('testVar', 'div#test', null);
53+
$this->assertTrue($extractor->validate());
54+
}
55+
56+
public function testEmptyAttributeWithNullSelector(): void
57+
{
58+
$this->expectException(\VoltTest\Exceptions\VoltTestException::class);
59+
$extractor = new \VoltTest\Extractors\HtmlExtractor('testVar', 'div#date', '');
60+
$extractor->toArray();
61+
62+
}
63+
}

tests/Units/StepTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,37 @@ public function testExtractFromJson(): void
117117
$this->assertEquals('json', $extract['type']);
118118
}
119119

120+
public function testExtractFromHtml(): void
121+
{
122+
$this->step->get(self::TEST_URL)
123+
->extractFromHtml('userId', 'div#user-id');
124+
$stepArray = $this->step->toArray();
125+
$extract = $stepArray['extract'][0];
126+
$this->assertEquals('userId', $extract['variable_name']);
127+
$this->assertEquals('div#user-id', $extract['selector']);
128+
$this->assertEquals('html', $extract['type']);
129+
}
130+
131+
132+
public function testExtractFromHtmlWithAttribute(): void
133+
{
134+
$this->step->get(self::TEST_URL)
135+
->extractFromHtml('userId', 'div#user-id', 'data-id');
136+
$stepArray = $this->step->toArray();
137+
$extract = $stepArray['extract'][0];
138+
$this->assertEquals('userId', $extract['variable_name']);
139+
$this->assertEquals('div#user-id', $extract['selector']);
140+
$this->assertEquals('html', $extract['type']);
141+
}
142+
143+
public function testExtractFromHtmlThrowsException(): void
144+
{
145+
$this->expectException(VoltTestException::class);
146+
$this->step->get(self::TEST_URL)
147+
->extractFromHtml('userId', '#div#user-id', '');
148+
$this->step->toArray();
149+
}
150+
120151
public function testInvalidJsonPathThrowsException(): void
121152
{
122153
$this->expectException(InvalidJsonPathException::class);

0 commit comments

Comments
 (0)