Skip to content

Commit a36536a

Browse files
committed
refactor: improve GroqClient and service provider setup
- Move config merging to the top of the service provider's register method - Add a getApiKey method in GroqClient to access the API key via reflection - Simplify test assertions in TranslationsTest by using direct array comparisons
2 parents 8d56d70 + 139764a commit a36536a

File tree

5 files changed

+41
-36
lines changed

5 files changed

+41
-36
lines changed

.env.testing

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
APP_ENV=testing
2+
GROQ_API_KEY="${GROQ_API_KEY}"

phpunit.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@
1616
</coverage>
1717
<php>
1818
<env name="APP_ENV" value="testing"/>
19-
<env name="GROQ_API_KEY" value=""/>
2019
</php>
21-
</phpunit>
20+
</phpunit>

src/GroqClient.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public function __construct(?string $apiKey = null)
3939
'1. Constructor parameter' . PHP_EOL .
4040
'2. Environment variable (GROQ_API_KEY)' . PHP_EOL .
4141
'3. Config file (config/groq.php)',
42-
GroqException::CODE_INVALID_REQUEST,
43-
GroqException::TYPE_INVALID_REQUEST,
42+
400, // HTTP 400 Bad Request code
43+
'invalid_request_error' // Error type
4444
);
4545
}
4646

@@ -153,7 +153,7 @@ public function setConfig(array $options): void
153153

154154
// Create new client with updated options
155155
$this->client = new Groq(
156-
$this->client->apiKey,
156+
$this->getApiKey(), // Use the getter method instead of direct property access
157157
$mergedOptions
158158
);
159159
}
@@ -178,4 +178,18 @@ public function getClient(): Groq
178178
{
179179
return $this->client;
180180
}
181-
}
181+
182+
/**
183+
* Get the API key from the client
184+
*
185+
* @return string
186+
*/
187+
private function getApiKey(): string
188+
{
189+
// Access the API key through reflection since it's not directly accessible
190+
$reflection = new \ReflectionClass($this->client);
191+
$property = $reflection->getProperty('apiKey');
192+
$property->setAccessible(true);
193+
return $property->getValue($this->client);
194+
}
195+
}

src/GroqServiceProvider.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
namespace LucianoTonet\GroqLaravel;
44

55
use Illuminate\Support\ServiceProvider;
6+
use LucianoTonet\GroqPHP\Groq;
67

78
class GroqServiceProvider extends ServiceProvider
89
{
910
public function register(): void
1011
{
12+
// Merge config
13+
$this->mergeConfigFrom(__DIR__ . '/../config/groq.php', 'groq');
14+
1115
$this->app->singleton(Groq::class, function ($app, $parameters = []) {
1216
return new Groq(
1317
$parameters['apiKey'] ?? config('groq.api_key'),
@@ -17,6 +21,14 @@ public function register(): void
1721

1822
// Register the alias 'groq' -> Groq::class
1923
$this->app->alias(Groq::class, 'groq');
24+
25+
// Register the GroqClient singleton
26+
$this->app->singleton(GroqClient::class, function ($app) {
27+
return new GroqClient(config('groq.api_key'));
28+
});
29+
30+
// Register facade
31+
$this->app->alias(GroqClient::class, 'groq');
2032
}
2133

2234
/**
@@ -32,21 +44,4 @@ public function boot()
3244

3345
$this->loadTranslationsFrom(__DIR__ . '/../lang', 'groq');
3446
}
35-
36-
/**
37-
* Register the application services.
38-
*/
39-
public function register()
40-
{
41-
// Merge config
42-
$this->mergeConfigFrom(__DIR__ . '/../config/groq.php', 'groq');
43-
44-
// Register the GroqClient singleton
45-
$this->app->singleton(GroqClient::class, function ($app) {
46-
return new GroqClient(config('groq.api_key'));
47-
});
48-
49-
// Register facade
50-
$this->app->alias(GroqClient::class, 'groq');
51-
}
5247
}

tests/Feature/TranslationsTest.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ public function it_can_create_translation_with_file()
3939
$mockTranslations = $this->createMock(Translations::class);
4040
$mockTranslations->expects($this->once())
4141
->method('create')
42-
->with($this->callback(function ($params) {
43-
return isset($params['file']) &&
44-
isset($params['model']) &&
45-
$params['model'] === 'whisper-large-v3';
46-
}))
42+
->with(["file" => __DIR__ . '/test-audio.mp3', "model" => 'whisper-large-v3'])
4743
->willReturn($expectedResponse);
4844

4945
// Mock the Audio class that will return our mock Translations
@@ -103,13 +99,12 @@ public function it_can_create_translation_with_options()
10399
$mockTranslations = $this->createMock(Translations::class);
104100
$mockTranslations->expects($this->once())
105101
->method('create')
106-
->with($this->callback(function ($params) {
107-
return isset($params['file']) &&
108-
isset($params['temperature']) &&
109-
$params['temperature'] === 0.3 &&
110-
isset($params['response_format']) &&
111-
$params['response_format'] === 'verbose_json';
112-
}))
102+
->with([
103+
'file' => __DIR__ . '/test-audio.mp3',
104+
'model' => 'whisper-large-v3',
105+
'temperature' => 0.3,
106+
'response_format' => 'verbose_json'
107+
])
113108
->willReturn($expectedResponse);
114109

115110
// Mock the Audio class that will return our mock Translations
@@ -146,4 +141,4 @@ public function it_can_create_translation_with_options()
146141
$this->assertCount(2, $result['segments']);
147142
$this->assertEquals('This is a test translation', $result['segments'][0]['text']);
148143
}
149-
}
144+
}

0 commit comments

Comments
 (0)