Skip to content

Commit 167aa61

Browse files
committed
test(models): reorganize tests namespace and add PromptsTest
- Move ModelRegistryTest, ModelRegistryYamlTest and ModelsYamlTest into tests/Models directory - Update their namespaces and file paths to `Bblslug\Tests\Models` - Add PromptsTest to cover Prompts::load() exception handling and render() placeholder replacement
1 parent dd8d107 commit 167aa61

File tree

4 files changed

+99
-4
lines changed

4 files changed

+99
-4
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Bblslug\Tests;
3+
namespace Bblslug\Tests\Models;
44

55
use Bblslug\Models\Drivers\AnthropicDriver;
66
use Bblslug\Models\Drivers\DeepLDriver;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Bblslug\Tests;
3+
namespace Bblslug\Tests\Models;
44

55
use Bblslug\Models\ModelRegistry;
66
use PHPUnit\Framework\TestCase;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Bblslug\Tests;
5+
namespace Bblslug\Tests\Models;
66

77
use PHPUnit\Framework\TestCase;
88
use Symfony\Component\Yaml\Yaml;
@@ -13,7 +13,7 @@ class ModelsYamlTest extends TestCase
1313
/** @test */
1414
public function modelsYamlIsValidAndNonEmpty(): void
1515
{
16-
$path = __DIR__ . '/../resources/models.yaml';
16+
$path = __DIR__ . '/../../resources/models.yaml';
1717

1818
$this->assertFileExists($path, 'models.yaml must exist');
1919
$this->assertIsReadable($path, 'models.yaml must be readable');

tests/Models/PromptsTest.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bblslug\Tests\Models;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Bblslug\Models\Prompts;
9+
use ReflectionClass;
10+
11+
class PromptsTest extends TestCase
12+
{
13+
private string $yamlPath;
14+
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
// Reset internal cache
19+
$ref = new ReflectionClass(Prompts::class);
20+
$prop = $ref->getProperty('templates');
21+
$prop->setAccessible(true);
22+
$prop->setValue(null, null);
23+
24+
// Prepare a temporary YAML file for testing
25+
$this->yamlPath = sys_get_temp_dir() . '/prompts_test.yaml';
26+
if (file_exists($this->yamlPath)) {
27+
unlink($this->yamlPath);
28+
}
29+
}
30+
31+
protected function tearDown(): void
32+
{
33+
// Clean up temp file
34+
if (file_exists($this->yamlPath)) {
35+
unlink($this->yamlPath);
36+
}
37+
parent::tearDown();
38+
}
39+
40+
/** @test */
41+
public function testLoadThrowsExceptionForMissingFile(): void
42+
{
43+
// Should throw when file is not readable
44+
$this->expectException(\RuntimeException::class);
45+
Prompts::load('/path/does/not/exist.yaml');
46+
}
47+
48+
/** @test */
49+
public function testRenderReplacesPlaceholders(): void
50+
{
51+
// Create a simple YAML with one template
52+
$yaml = <<<YAML
53+
translator:
54+
text: 'Translate from {source} to {target}: {text}'
55+
YAML;
56+
file_put_contents($this->yamlPath, $yaml);
57+
58+
// Load our test YAML
59+
Prompts::load($this->yamlPath);
60+
61+
// Render the template with variables
62+
$output = Prompts::render(
63+
kind: 'translator',
64+
format: 'text',
65+
vars: ['source' => 'en', 'target' => 'fr', 'text' => 'Hello']
66+
);
67+
68+
$this->assertSame(
69+
'Translate from en to fr: Hello',
70+
$output
71+
);
72+
}
73+
74+
/** @test */
75+
public function testRenderThrowsExceptionForUndefinedTemplate(): void
76+
{
77+
// Create YAML with a single known template
78+
$yaml = <<<YAML
79+
bot:
80+
html: '<p>{message}</p>'
81+
YAML;
82+
file_put_contents($this->yamlPath, $yaml);
83+
84+
Prompts::load($this->yamlPath);
85+
86+
// Trying to render missing kind.format should fail
87+
$this->expectException(\InvalidArgumentException::class);
88+
$this->expectExceptionMessage("Prompt 'unknown.json' not found");
89+
Prompts::render(
90+
kind: 'unknown',
91+
format: 'json',
92+
vars: []
93+
);
94+
}
95+
}

0 commit comments

Comments
 (0)