Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Testing/Enums/OverrideStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace OpenAI\Testing\Enums;

enum OverrideStrategy: string
{
case Merge = 'merge';
case Replace = 'replace';
}
43 changes: 18 additions & 25 deletions src/Testing/Responses/Concerns/Fakeable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,26 @@
namespace OpenAI\Testing\Responses\Concerns;

use OpenAI\Responses\Meta\MetaInformation;
use OpenAI\Testing\Enums\OverrideStrategy;

trait Fakeable
{
/**
* @param array<string, mixed> $override
*/
public static function fake(array $override = [], ?MetaInformation $meta = null): static
{
public static function fake(
array $override = [],
?MetaInformation $meta = null,
OverrideStrategy $strategy = OverrideStrategy::Merge,
): static {
$class = str_replace('OpenAI\\Responses\\', 'OpenAI\\Testing\\Responses\\Fixtures\\', static::class).'Fixture';

return static::from(
self::buildAttributes($class::ATTRIBUTES, $override),
self::buildAttributes($class::ATTRIBUTES, $override, $strategy),
$meta ?? self::fakeResponseMetaInformation(),
);
}

/**
* @return mixed[]
*/
private static function buildAttributes(array $original, array $override): array
{
$new = [];

foreach ($original as $key => $entry) {
$new[$key] = is_array($entry)
? self::buildAttributes($entry, $override[$key] ?? [])
: $override[$key] ?? $entry;
unset($override[$key]);
}

// we are going to append all remaining overrides
foreach ($override as $key => $value) {
$new[$key] = $value;
}

return $new;
}

public static function fakeResponseMetaInformation(): MetaInformation
{
return MetaInformation::from([
Expand All @@ -59,4 +41,15 @@ public static function fakeResponseMetaInformation(): MetaInformation
'x-request-id' => ['3813fa4fa3f17bdf0d7654f0f49ebab4'],
]);
}

private static function buildAttributes(
array $original,
array $override,
OverrideStrategy $strategy = OverrideStrategy::Merge): array
{
return match ($strategy) {
OverrideStrategy::Replace => array_replace($original, $override),
OverrideStrategy::Merge => array_replace_recursive($original, $override),
};
}
}
37 changes: 37 additions & 0 deletions tests/Responses/Responses/CreateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use OpenAI\Responses\Responses\CreateResponseFormat;
use OpenAI\Responses\Responses\CreateResponseReasoning;
use OpenAI\Responses\Responses\CreateResponseUsage;
use OpenAI\Testing\Enums\OverrideStrategy;

test('from', function () {
$response = CreateResponse::from(createResponseResource(), meta());
Expand Down Expand Up @@ -76,6 +77,42 @@
->id->toBe('resp_67ccf18ef5fc8190b16dbee19bc54e5f087bb177ab789d5c');
});

test('fake with the "replace" and "merge" strategy override', function () {
$attributes = [
'output' => [
[
'id' => 'msg_67ccd2bf17f0819081ff3bb2cf6508e60bb6a6b452d3795b',
'role' => 'assistant',
'type' => 'message',
'status' => 'completed',
'content' => [
[
'type' => 'output_text',
'text' => 'This is the fake test output',
'annotations' => [],
],
],
],
],
];
$response = CreateResponse::fake(
$attributes,
strategy: OverrideStrategy::Replace
);

expect($response)
->output->toBeArray()->toHaveCount(1)
->outputText->toBe('This is the fake test output');

$response = CreateResponse::fake(
$attributes,
);

expect($response)
->output->toBeArray()->toHaveCount(2)
->outputText->toBe('This is the fake test output As of today, March 9, 2025, one notable positive news story...');
});

test('fake with override', function () {
$response = CreateResponse::fake([
'id' => 'resp_1234',
Expand Down