Skip to content

Commit 32e8a0b

Browse files
authored
fix(meta): Add OverrideStrategy enum for the fakeable trait to support merge/replace (#646)
* Add `OverrideStrategy` enum and enable merge/replace options for faking responses, updating `Fakeable::fake()` and its attribute builder accordingly. * Fix and add a test for the OverrideStrategy in `Fakeable::fake()` implementation. fixes: #581
1 parent 0787025 commit 32e8a0b

File tree

3 files changed

+66
-25
lines changed

3 files changed

+66
-25
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Testing\Enums;
6+
7+
enum OverrideStrategy: string
8+
{
9+
case Merge = 'merge';
10+
case Replace = 'replace';
11+
}

src/Testing/Responses/Concerns/Fakeable.php

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,26 @@
55
namespace OpenAI\Testing\Responses\Concerns;
66

77
use OpenAI\Responses\Meta\MetaInformation;
8+
use OpenAI\Testing\Enums\OverrideStrategy;
89

910
trait Fakeable
1011
{
1112
/**
1213
* @param array<string, mixed> $override
1314
*/
14-
public static function fake(array $override = [], ?MetaInformation $meta = null): static
15-
{
15+
public static function fake(
16+
array $override = [],
17+
?MetaInformation $meta = null,
18+
OverrideStrategy $strategy = OverrideStrategy::Merge,
19+
): static {
1620
$class = str_replace('OpenAI\\Responses\\', 'OpenAI\\Testing\\Responses\\Fixtures\\', static::class).'Fixture';
1721

1822
return static::from(
19-
self::buildAttributes($class::ATTRIBUTES, $override),
23+
self::buildAttributes($class::ATTRIBUTES, $override, $strategy),
2024
$meta ?? self::fakeResponseMetaInformation(),
2125
);
2226
}
2327

24-
/**
25-
* @return mixed[]
26-
*/
27-
private static function buildAttributes(array $original, array $override): array
28-
{
29-
$new = [];
30-
31-
foreach ($original as $key => $entry) {
32-
$new[$key] = is_array($entry)
33-
? self::buildAttributes($entry, $override[$key] ?? [])
34-
: $override[$key] ?? $entry;
35-
unset($override[$key]);
36-
}
37-
38-
// we are going to append all remaining overrides
39-
foreach ($override as $key => $value) {
40-
$new[$key] = $value;
41-
}
42-
43-
return $new;
44-
}
45-
4628
public static function fakeResponseMetaInformation(): MetaInformation
4729
{
4830
return MetaInformation::from([
@@ -59,4 +41,15 @@ public static function fakeResponseMetaInformation(): MetaInformation
5941
'x-request-id' => ['3813fa4fa3f17bdf0d7654f0f49ebab4'],
6042
]);
6143
}
44+
45+
private static function buildAttributes(
46+
array $original,
47+
array $override,
48+
OverrideStrategy $strategy = OverrideStrategy::Merge): array
49+
{
50+
return match ($strategy) {
51+
OverrideStrategy::Replace => array_replace($original, $override),
52+
OverrideStrategy::Merge => array_replace_recursive($original, $override),
53+
};
54+
}
6255
}

tests/Responses/Responses/CreateResponse.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use OpenAI\Responses\Responses\CreateResponseFormat;
66
use OpenAI\Responses\Responses\CreateResponseReasoning;
77
use OpenAI\Responses\Responses\CreateResponseUsage;
8+
use OpenAI\Testing\Enums\OverrideStrategy;
89

910
test('from', function () {
1011
$response = CreateResponse::from(createResponseResource(), meta());
@@ -76,6 +77,42 @@
7677
->id->toBe('resp_67ccf18ef5fc8190b16dbee19bc54e5f087bb177ab789d5c');
7778
});
7879

80+
test('fake with the "replace" and "merge" strategy override', function () {
81+
$attributes = [
82+
'output' => [
83+
[
84+
'id' => 'msg_67ccd2bf17f0819081ff3bb2cf6508e60bb6a6b452d3795b',
85+
'role' => 'assistant',
86+
'type' => 'message',
87+
'status' => 'completed',
88+
'content' => [
89+
[
90+
'type' => 'output_text',
91+
'text' => 'This is the fake test output',
92+
'annotations' => [],
93+
],
94+
],
95+
],
96+
],
97+
];
98+
$response = CreateResponse::fake(
99+
$attributes,
100+
strategy: OverrideStrategy::Replace
101+
);
102+
103+
expect($response)
104+
->output->toBeArray()->toHaveCount(1)
105+
->outputText->toBe('This is the fake test output');
106+
107+
$response = CreateResponse::fake(
108+
$attributes,
109+
);
110+
111+
expect($response)
112+
->output->toBeArray()->toHaveCount(2)
113+
->outputText->toBe('This is the fake test output As of today, March 9, 2025, one notable positive news story...');
114+
});
115+
79116
test('fake with override', function () {
80117
$response = CreateResponse::fake([
81118
'id' => 'resp_1234',

0 commit comments

Comments
 (0)