1
+ <?php
2
+
3
+ require_once '../vendor/autoload.php ' ;
4
+
5
+ use Joomla \AI \AIFactory ;
6
+ use Joomla \AI \Exception \ProviderException ;
7
+
8
+ $ configFile = __DIR__ . '/../config.json ' ;
9
+ $ config = json_decode (file_get_contents ($ configFile ), true );
10
+ $ api_key = $ config ['openai_api_key ' ] ?? null ;
11
+ $ anthropic_api_key = $ config ['anthropic_api_key ' ] ?? null ;
12
+
13
+ echo "=== AI Factory Test Suite === \n\n" ;
14
+
15
+ // Test Case 1: Invalid Provider
16
+ echo "1. Testing invalid provider 'abcd': \n" ;
17
+ try {
18
+ $ options = [
19
+ 'api_key ' => $ anthropic_api_key
20
+ ];
21
+
22
+ $ ai = AIFactory::getAI ('abcd ' , $ options );
23
+ $ response = $ ai ->chat ("Hey " );
24
+ echo $ response ->getContent ();
25
+
26
+ } catch (ProviderException $ e ) {
27
+ echo "Caught expected exception: " . $ e ->getMessage () . "\n" ;
28
+ }
29
+ echo "\n" ;
30
+
31
+ // Test Case 2: Valid Provider Creation
32
+ echo "2. Testing valid provider creation (anthropic): \n" ;
33
+ try {
34
+ $ options = [
35
+ 'api_key ' => $ anthropic_api_key
36
+ ];
37
+
38
+ $ ai = AIFactory::getAI ('anthropic ' , $ options );
39
+ echo "Provider name: " . $ ai ->getProvider ()->getName () . "\n" ;
40
+ $ response = $ ai ->chat ("Hey " );
41
+ echo $ response ->getContent ();
42
+ } catch (Exception $ e ) {
43
+ echo "Failed to create Anthropic provider: " . $ e ->getMessage () . "\n" ;
44
+ }
45
+ echo "\n" ;
46
+
47
+ // Test Case 3: Non-existent Method Call
48
+ echo "3. Testing non-existent method call: \n" ;
49
+ try {
50
+ $ options = [
51
+ 'api_key ' => $ anthropic_api_key
52
+ ];
53
+
54
+ $ ai = AIFactory::getAI ('anthropic ' , $ options );
55
+ $ response = $ ai ->nonExistentMethod ("test " );
56
+ echo $ response ->getContent ();
57
+ } catch (ProviderException $ e ) {
58
+ echo "Caught expected Exception for non-existent method: " . $ e ->getMessage () . "\n" ;
59
+ }
60
+ echo "\n" ;
61
+
62
+ // Test Case 4: Available Providers
63
+ echo "4. Testing available providers: \n" ;
64
+ try {
65
+ $ availableProviders = AIFactory::getAvailableProviders ();
66
+ echo "Available providers: " . implode (', ' , $ availableProviders ) . "\n" ;
67
+
68
+ // Test each provider availability
69
+ foreach ($ availableProviders as $ provider ) {
70
+ $ isAvailable = AIFactory::isProviderAvailable ($ provider );
71
+ echo "Provider ' $ provider' is available: " . ($ isAvailable ? 'Yes ' : 'No ' ) . "\n" ;
72
+ }
73
+
74
+ // Test non-existent provider
75
+ $ isAvailable = AIFactory::isProviderAvailable ('non-existent ' );
76
+ echo "Provider 'non-existent' is available: " . ($ isAvailable ? 'Yes ' : 'No ' ) . "\n" ;
77
+ } catch (Exception $ e ) {
78
+ echo "Failed to get available providers: " . $ e ->getMessage () . "\n" ;
79
+ }
80
+ echo "\n" ;
81
+
82
+ // Test Case 5: Valid Method Call
83
+ echo "5. Testing valid method calls: \n" ;
84
+ try {
85
+ $ options = [
86
+ 'api_key ' => $ anthropic_api_key
87
+ ];
88
+
89
+ $ ai = AIFactory::getAI ('anthropic ' , $ options );
90
+ $ response = $ ai ->chat ("Hey " );
91
+ echo $ response ->getContent ();
92
+ } catch (Exception $ e ) {
93
+ echo "Test Failed: " . $ e ->getMessage () . "\n" ;
94
+ }
95
+
96
+ echo "\n=== Test Suite Complete === \n" ;
0 commit comments