Skip to content
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

Add setApiKey method #156

Open
badasswp opened this issue Feb 6, 2025 · 0 comments · May be fixed by #157
Open

Add setApiKey method #156

badasswp opened this issue Feb 6, 2025 · 0 comments · May be fixed by #157

Comments

@badasswp
Copy link

badasswp commented Feb 6, 2025

Describe the feature or improvement you're requesting

At the moment, it is difficult to effectively mock our OpenAi class in unit tests because we are currently forced to pass in the API key as an argument during instantiation like so:

new OpenAi(getenv('OPENAI_API_KEY'));

I'm proposing a new setApiKey method that will enable us do this anytime so that we are not restricted to the constructor. In this way, we can create and pass our objects via dependency injection like so:

interface AiClient
{
    public function chat($payload): void;
}

public function getAiClient(AiClient $aiClient)
{
    return $aiClient;
}

public function getOpenAiClient()
{
    $this->getAiClient(new OpenAi())->setApiKey($keys);
}

Additional context

In this way, we should be able to easily mock our OpenAi class for use in unit tests like so:

public function testOpenAiDoesSomething()
{
    $openAiMock = Mockery::mock(OpenAi::class)->makePartial();
    // write your other test logic....
}
@badasswp badasswp linked a pull request Feb 6, 2025 that will close this issue
@badasswp badasswp changed the title Add setApiKey method Add setApiKey method Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant