Skip to content

Commit e20bc5f

Browse files
committed
Incorrect transform schema - corrected tests
1 parent dfd568c commit e20bc5f

14 files changed

+197
-24
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
"Perfbase\\SDK\\": "src/"
1010
}
1111
},
12+
"autoload-dev": {
13+
"psr-4": {
14+
"Perfbase\\SDK\\Tests\\": "tests/"
15+
}
16+
},
1217
"authors": [
1318
{
1419
"name": "Ben Poulson",

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Perfbase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Perfbase\SDK;
44

5+
use Perfbase\SDK\Exception\PerfbaseApiKeyMissingException;
56
use Perfbase\SDK\Exception\PerfbaseStateException;
67
use Perfbase\SDK\Tracing\TraceInstance;
78
use Perfbase\SDK\Utils\ExtensionUtils;
@@ -52,6 +53,7 @@ public static function hasInstance(): bool
5253
* @param Config $config
5354
* @return TraceInstance
5455
* @throws PerfbaseStateException
56+
* @throws PerfbaseApiKeyMissingException
5557
*/
5658
public static function createInstance(Config $config): TraceInstance
5759
{

src/Tracing/Attributes.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,71 +12,71 @@ class Attributes
1212
* Eg, "GET /users/:userId", "Cron:DailyReport", "Queue:ProcessEmails"
1313
* @var string|null
1414
*/
15-
public ?string $action;
15+
public ?string $action = null;
1616

1717
/**
1818
* The unique user ID of the user that triggered this trace.
1919
* @var string|null
2020
*/
21-
public ?string $userId;
21+
public ?string $userId = null;
2222

2323
/**
2424
* The IP address of the user that triggered this trace.
2525
* @var string|null
2626
*/
27-
public ?string $userIp;
27+
public ?string $userIp = null;
2828

2929
/**
3030
* The user agent of the user that triggered this trace.
3131
* @var string|null
3232
*/
33-
public ?string $userAgent;
33+
public ?string $userAgent = null;
3434

3535
/**
3636
* The hostname of the server that this trace was collected on.
3737
* @var string|null
3838
*/
39-
public ?string $hostname;
39+
public ?string $hostname = null;
4040

4141
/**
4242
* The environment that this trace was collected in.
4343
* Eg, "production", "staging", "development"
4444
* @var string|null
4545
*/
46-
public ?string $environment;
46+
public ?string $environment = null;
4747

4848
/**
4949
* The version of the application that this trace was collected in.
5050
* This could be a git commit hash, a version number, or anything else.
5151
* @var string|null
5252
*/
53-
public ?string $appVersion;
53+
public ?string $appVersion = null;
5454

5555
/**
5656
* The version of PHP that this trace was collected in.
5757
* @var string|null
5858
*/
59-
public ?string $phpVersion;
59+
public ?string $phpVersion = null;
6060

6161
/**
6262
* The HTTP method that was used to trigger this trace, if applicable.
6363
* Eg, "GET", "POST", "PUT", "DELETE"
6464
* @var string|null
6565
*/
66-
public ?string $httpMethod;
66+
public ?string $httpMethod = null;
6767

6868
/**
6969
* The HTTP status code that was returned by the server, if applicable.
7070
* Eg, 200, 404, 500
7171
* @var int|null
7272
*/
73-
public ?int $httpStatusCode;
73+
public ?int $httpStatusCode = null;
7474

7575
/**
7676
* The full URL that was requested to trigger this trace, if applicable.
7777
* @var string|null
7878
*/
79-
public ?string $httpUrl;
79+
public ?string $httpUrl = null;
8080

8181
public function __construct()
8282
{

src/Tracing/TraceInstance.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Perfbase\SDK\Tracing;
44

5+
use http\Exception\RuntimeException;
56
use JsonException;
67
use Perfbase\SDK\Config;
78
use Perfbase\SDK\Exception\PerfbaseApiKeyMissingException;
@@ -146,6 +147,27 @@ public function sendProfilingData(): void
146147
*/
147148
public function transformData(): array
148149
{
150+
// Compress the performance data
151+
$compressedPerfData = gzencode(json_encode($this->performanceData), -1, FORCE_GZIP);
152+
if ($compressedPerfData === false) {
153+
throw new RuntimeException('Failed to compress performance data');
154+
}
155+
$perfData = base64_encode($compressedPerfData);
156+
unset($compressedPerfData);
157+
158+
// Compress the metadata, if any
159+
if (count($this->metaData)) {
160+
$compressedMetaData = gzencode(json_encode($this->metaData), -1, FORCE_GZIP);
161+
if ($compressedMetaData === false) {
162+
throw new RuntimeException('Failed to compress performance data');
163+
}
164+
$metaData = base64_encode($compressedMetaData);
165+
unset($compressedMetaData);
166+
} else {
167+
$metaData = null;
168+
}
169+
170+
// Return the transformed data
149171
return [
150172
'action' => $this->attributes->action,
151173
'user_id' => $this->attributes->userId,
@@ -158,8 +180,8 @@ public function transformData(): array
158180
'http_method' => $this->attributes->httpMethod,
159181
'http_status_code' => $this->attributes->httpStatusCode,
160182
'http_url' => $this->attributes->httpUrl,
161-
'perf_data' => json_encode($this->performanceData),
162-
'meta_data' => json_encode($this->metaData)
183+
'perf_data' => $perfData,
184+
'meta_data' => $metaData
163185
];
164186
}
165187
}

test.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
php -d memory_limit=-1 vendor/bin/phpstan analyse -c phpstan.neon --memory-limit=2G
3+
php -d memory_limit=-1 vendor/bin/phpunit

test_docker.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ for VERSION in "${PHP_VERSIONS[@]}"; do
1515

1616
if [ $? -eq 0 ]; then
1717
echo "Successfully built ${IMAGE_NAME}. Running tests..."
18-
docker run --rm "$IMAGE_NAME" -it 'sh -c php -d memory_limit=-1 vendor/bin/phpstan analyse -c phpstan.neon --memory-limit=2G && php -d memory_limit=-1 vendor/bin/phpunit'
18+
docker run --rm "$IMAGE_NAME" './test.sh'
1919

2020
if [ $? -eq 0 ]; then
2121
echo "Tests passed for PHP ${VERSION}."

tests/ApiClientTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests;
3+
namespace Perfbase\SDK\Tests;
44

55
use GuzzleHttp\Client as GuzzleClient;
66
use GuzzleHttp\Handler\MockHandler;
@@ -16,7 +16,7 @@
1616
/**
1717
* @coversDefaultClass \Perfbase\SDK\Http\ApiClient
1818
*/
19-
class ApiClientTest extends \PHPUnit\Framework\TestCase
19+
class ApiClientTest extends BaseTest
2020
{
2121

2222
/**

tests/BaseTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Perfbase\SDK\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use ReflectionException;
7+
8+
abstract class BaseTest extends TestCase
9+
{
10+
/**
11+
* Retrieves the value of a private or protected property from an object.
12+
*
13+
* @param object $object The object containing the private property.
14+
* @param string $propertyName The name of the property to access.
15+
* @return mixed The value of the private property.
16+
* @throws ReflectionException If the property does not exist.
17+
*/
18+
protected function getPrivateFieldValue(object $object, string $propertyName)
19+
{
20+
$reflection = new \ReflectionClass($object);
21+
$property = $reflection->getProperty($propertyName);
22+
$property->setAccessible(true);
23+
return $property->getValue($object);
24+
}
25+
26+
/**
27+
* Sets the value of a private or protected property on an object.
28+
*
29+
* @param object $object The object containing the private property.
30+
* @param string $propertyName The name of the property to modify.
31+
* @param mixed $value The new value to set.
32+
* @throws ReflectionException If the property does not exist.
33+
*/
34+
protected function setPrivateField(object $object, string $propertyName, $value): void
35+
{
36+
$reflection = new \ReflectionClass($object);
37+
$property = $reflection->getProperty($propertyName);
38+
$property->setAccessible(true);
39+
$property->setValue($object, $value);
40+
}
41+
}

tests/ConfigTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
22

3+
namespace Perfbase\SDK\Tests;
4+
35
use Perfbase\SDK\Config;
46

57
/**
68
* @coversDefaultClass \Perfbase\SDK\Config
79
*/
8-
class ConfigTest extends \PHPUnit\Framework\TestCase
10+
class ConfigTest extends BaseTest
911
{
1012
/**
1113
* Test the default configuration values

0 commit comments

Comments
 (0)