Skip to content

[Platform] Add InMemoryPlatform and InMemoryRawResult for testing #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

RamyHakam
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
Docs? yes
Issues none
License MIT

This PR adds two test-friendly classes to the Symfony AI Platform component:

InMemoryPlatform

The InMemoryPlatform is a test-specific implementation of PlatformInterface that returns a TextResult wrapped in a ResultPromise.
It supports:

  • A fixed string response (e.g. 'Fake reply')
  • A callable response based on the Model, input, and options

This makes it ideal for unit or integration tests, or even functional tests where a full AI stack is not needed.

InMemoryRawResult

The InMemoryRawResult implements RawResultInterface and is used internally by InMemoryPlatform. It wraps:

  • A structured array (getData())
  • A generic object (getObject())

to simulate what real raw results look like in production usage.

The Problem

While testing new code that uses the AI platform, developers often need to manually create PlatformInterface instances. The existing Platform and PlatformFactory classes are tightly coupled to real AI providers.
This in-memory implementation simplifies unit and integration testing by allowing developers to inject a dummy platform and return mock results without any external API calls.

Example

$platform = new InMemoryPlatform('Test reply');
$response = $platform->invoke(new Model('test'), 'Hello');
echo $response->asText(); // "Test reply"

@OskarStark OskarStark changed the title [Platform] Add InMemoryPlatform and InMemoryRawResult for testing [Platform] Add InMemoryPlatform and InMemoryRawResult for testing Jul 24, 2025
@RamyHakam RamyHakam requested a review from OskarStark July 24, 2025 13:42
*/
private $mockResult;

public function __construct(callable|string $mockResult = 'Default test response')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use constructor property promotion here - and I'm not sure if a default value makes sense here 🤔

isn't the usual case that you want to provide something explicitly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chr-hertel I already tried that, but property promotion does not allow callable
PHP will throw Fatal error: Property cannot have type 'callable'

And regarding the default value, you are right, it's a valid point, I will remove it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about using Closure instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chr-hertel Done , Thaks

@RamyHakam RamyHakam requested a review from chr-hertel July 24, 2025 20:38
@chr-hertel chr-hertel added the Platform Issues & PRs about the AI Platform component label Jul 24, 2025
@chr-hertel chr-hertel force-pushed the Add-InMemoryPlatform-and-InMemoryRawResult-for-testing-PlatformInterface branch from 27b46e3 to 1e9b80e Compare July 26, 2025 08:29
@chr-hertel
Copy link
Member

Thank you @RamyHakam.

@chr-hertel chr-hertel merged commit 4b469a1 into symfony:main Jul 26, 2025
12 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform Issues & PRs about the AI Platform component
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants