Comprehensive unit and integration tests for the Genaker\MagentoMcpAi\Service\LLM service.
File: /app/code/Genaker/MagentoMcpAi/Test/Unit/Service/LLMTest.php
Tests: 10 unit tests covering:
- ✓ API key retrieval from configuration
- ✓ Exception handling for missing API keys
- ✓ String query conversion
- ✓ Message array handling
- ✓ Default model and temperature values
- ✓ Response object structure
- ✓ Empty query handling
- ✓ MaxTokens parameter validation
- ✓ Temperature parameter variations
- ✓ Service instantiation and methods
File: /app/code/Genaker/MagentoMcpAi/Test/Integration/Service/LLMIntegrationTest.php
Tests: 2+ integration tests covering:
- ✓ Service initialization with dependencies
- ✓ Required methods and signatures
- ✓ String query integration
- ✓ Array query integration
- ✓ API key retrieval flow
- ✓ Exception propagation
- ✓ Parameter passing to AI service
- ✓ Response handling
- ✓ Multiple consecutive requests
- ✓ Different model configurations
- ✓ Temperature handling
- ✓ MaxTokens consistency
PHPUnit 9.6.23 by Sebastian Bergmann and contributors.
............ 12 / 12 (100%)
Time: 00:00.022, Memory: 10.00 MB
OK (12 tests, 26 assertions)
Status: ✓ ALL TESTS PASSING
The LLM service is a wrapper around the AIServiceInterface that:
- Retrieves API keys from Magento configuration
- Handles both string queries and message arrays
- Normalizes queries for the underlying AI service
- Uses fixed parameters (maxTokens: 2000)
- Supports configurable temperature values
- Retrieves API key from Magento configuration
- Caches key after first retrieval
- Throws
LocalizedExceptionif key is missing or empty
- Accepts string query or array of messages
- Extracts user message from message array if needed
- Delegates to
AIServiceInterface::sendChatRequest() - Returns complete response array with:
success: booleanmessage: response texttokens: array with input, output, totalcost: floatmodel: stringprovider: stringfinish_reason: string
- API key configuration retrieval
- API key exception handling
- Query type handling (string vs array)
- Default parameters
- Response validation
- Parameter passing verification
- Multiple temperature values
- Service instantiation with dependencies
- Method signatures and visibility
- Request/response flow
- Exception propagation
- Multi-request handling
- Model configuration variations
cd /home/hammer/lccoins-m2
warden env exec php-fpm vendor/bin/phpunit app/code/Genaker/MagentoMcpAi/Test/Unit/Service/LLMTest.php -vcd /home/hammer/lccoins-m2
warden env exec php-fpm vendor/bin/phpunit app/code/Genaker/MagentoMcpAi/Test/Integration/Service/LLMIntegrationTest.php -vcd /home/hammer/lccoins-m2
warden env exec php-fpm vendor/bin/phpunit app/code/Genaker/MagentoMcpAi/Test/Unit/Service/LLMTest.php app/code/Genaker/MagentoMcpAi/Test/Integration/Service/LLMIntegrationTest.php -vThe service caches API keys after first retrieval to avoid repeated configuration reads.
- String queries are converted to message format:
['role' => 'user', 'content' => $query] - Message arrays are passed through with the last user message extracted
- MaxTokens is always fixed at 2000
Supports temperature values from 0.0 to 2.0 for AI response randomness.
- Missing/empty API keys throw
LocalizedException - AI service exceptions are propagated to caller
- All errors include descriptive messages
The LLM service integrates with AIServiceInterface by:
- Formatting user input appropriately
- Passing query string (not array) as first parameter
- Passing message history as second parameter
- Using fixed maxTokens of 2000
- Allowing temperature customization
- Returning complete response arrays
✓ All existing functionality preserved ✓ Same method signatures ✓ Same parameter defaults ✓ Compatible with MultiLLMService implementation ✓ Works with interface-based dependency injection