Skip to content

Commit e717ba8

Browse files
committed
minor #1380 Merge bridge tests from mate/Tests into bridge tests (wachterjohannes)
This PR was merged into the main branch. Discussion ---------- Merge bridge tests from mate/Tests into bridge tests | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | no | Issues | N/A | License | MIT ## Summary - Merged LogSearchToolTest with comprehensive test coverage - Merged ServiceToolTest with enhanced test cases - Added necessary fixtures to support comprehensive testing - Removed duplicate test files and fixtures from mate test suite Commits ------- b3facdc Merge bridge tests from mate/Tests into bridge tests
2 parents 9b45f99 + b3facdc commit e717ba8

File tree

9 files changed

+240
-440
lines changed

9 files changed

+240
-440
lines changed

src/mate/src/Bridge/Monolog/Tests/Capability/LogSearchToolTest.php

Lines changed: 139 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,100 +21,207 @@
2121
*/
2222
final class LogSearchToolTest extends TestCase
2323
{
24+
private string $fixturesDir;
2425
private LogSearchTool $tool;
2526

2627
protected function setUp(): void
2728
{
28-
$fixturesDir = \dirname(__DIR__).'/Fixtures';
29-
$reader = new LogReader(new LogParser(), $fixturesDir);
29+
$this->fixturesDir = \dirname(__DIR__).'/Fixtures';
30+
$parser = new LogParser();
31+
$reader = new LogReader($parser, $this->fixturesDir);
3032
$this->tool = new LogSearchTool($reader);
3133
}
3234

33-
public function testSearch()
35+
public function testSearchByTextTerm()
3436
{
35-
$results = $this->tool->search('database');
37+
$results = $this->tool->search('logged in');
3638

39+
$this->assertNotEmpty($results);
3740
$this->assertCount(1, $results);
38-
$this->assertStringContainsString('Database', $results[0]['message']);
41+
$this->assertStringContainsString('User logged in', $results[0]['message']);
3942
}
4043

41-
public function testSearchWithLevel()
44+
public function testSearchByTextTermReturnsEmptyWhenNotFound()
4245
{
43-
$results = $this->tool->search('', 'ERROR');
46+
$results = $this->tool->search('nonexistent search term xyz');
4447

45-
$this->assertCount(2, $results);
46-
foreach ($results as $result) {
47-
$this->assertSame('ERROR', $result['level']);
48+
$this->assertEmpty($results);
49+
}
50+
51+
public function testSearchByLevel()
52+
{
53+
$results = $this->tool->search('', level: 'ERROR');
54+
55+
$this->assertNotEmpty($results);
56+
57+
foreach ($results as $entry) {
58+
$this->assertSame('ERROR', $entry['level']);
4859
}
4960
}
5061

51-
public function testSearchWithChannel()
62+
public function testSearchByChannel()
5263
{
53-
$results = $this->tool->search('', null, 'security');
64+
$results = $this->tool->search('', channel: 'security');
65+
66+
$this->assertNotEmpty($results);
5467

55-
$this->assertCount(2, $results);
56-
foreach ($results as $result) {
57-
$this->assertSame('security', $result['channel']);
68+
foreach ($results as $entry) {
69+
$this->assertSame('security', $entry['channel']);
5870
}
5971
}
6072

73+
public function testSearchByEnvironment()
74+
{
75+
// Skip this test as the bridge test fixtures don't have environment-specific files
76+
$this->markTestSkipped('Environment-specific search not supported in bridge test fixtures');
77+
}
78+
6179
public function testSearchWithLimit()
6280
{
63-
$results = $this->tool->search('', limit: 3);
81+
$results = $this->tool->search('', limit: 2);
6482

65-
$this->assertCount(3, $results);
83+
$this->assertLessThanOrEqual(2, \count($results));
6684
}
6785

6886
public function testSearchRegex()
6987
{
70-
$results = $this->tool->searchRegex('/connection|timeout/i');
88+
$results = $this->tool->searchRegex('Database.*failed');
89+
90+
$this->assertNotEmpty($results);
91+
$this->assertStringContainsString('Database connection failed', $results[0]['message']);
92+
}
93+
94+
public function testSearchRegexWithDelimiters()
95+
{
96+
$results = $this->tool->searchRegex('/User.*logged/i');
7197

72-
$this->assertGreaterThanOrEqual(1, \count($results));
98+
$this->assertNotEmpty($results);
99+
}
100+
101+
public function testSearchRegexByLevel()
102+
{
103+
$results = $this->tool->searchRegex('.*', level: 'WARNING');
104+
105+
$this->assertNotEmpty($results);
106+
107+
foreach ($results as $entry) {
108+
$this->assertSame('WARNING', $entry['level']);
109+
}
73110
}
74111

75112
public function testSearchContext()
76113
{
77114
$results = $this->tool->searchContext('user_id', '123');
78115

79-
$this->assertCount(1, $results);
116+
$this->assertNotEmpty($results);
117+
$this->assertArrayHasKey('user_id', $results[0]['context']);
80118
$this->assertSame(123, $results[0]['context']['user_id']);
81119
}
82120

121+
public function testSearchContextReturnsEmptyWhenKeyNotFound()
122+
{
123+
$results = $this->tool->searchContext('nonexistent_key', 'value');
124+
125+
$this->assertEmpty($results);
126+
}
127+
128+
public function testSearchContextByLevel()
129+
{
130+
$results = $this->tool->searchContext('error', 'Connection', level: 'ERROR');
131+
132+
$this->assertNotEmpty($results);
133+
}
134+
83135
public function testTail()
84136
{
85-
$results = $this->tool->tail(5);
137+
$results = $this->tool->tail(10);
138+
139+
$this->assertNotEmpty($results);
140+
$this->assertLessThanOrEqual(10, \count($results));
141+
}
86142

87-
$this->assertCount(5, $results);
143+
public function testTailWithLevel()
144+
{
145+
$results = $this->tool->tail(10, level: 'INFO');
146+
147+
foreach ($results as $entry) {
148+
$this->assertSame('INFO', $entry['level']);
149+
}
150+
}
151+
152+
public function testTailWithEnvironment()
153+
{
154+
// Skip this test as the bridge test fixtures don't have environment-specific files
155+
$this->markTestSkipped('Environment-specific tail not supported in bridge test fixtures');
88156
}
89157

90158
public function testListFiles()
91159
{
92-
$files = $this->tool->listFiles();
160+
$results = $this->tool->listFiles();
93161

94-
$this->assertCount(2, $files);
95-
foreach ($files as $file) {
162+
$this->assertNotEmpty($results);
163+
164+
foreach ($results as $file) {
96165
$this->assertArrayHasKey('name', $file);
97166
$this->assertArrayHasKey('path', $file);
98167
$this->assertArrayHasKey('size', $file);
99168
$this->assertArrayHasKey('modified', $file);
100169
}
101170
}
102171

172+
public function testListFilesForEnvironment()
173+
{
174+
// Skip this test as the bridge test fixtures don't have environment-specific files
175+
$this->markTestSkipped('Environment-specific file listing not supported in bridge test fixtures');
176+
}
177+
103178
public function testListChannels()
104179
{
105-
$channels = $this->tool->listChannels();
180+
$results = $this->tool->listChannels();
106181

107-
$this->assertContains('app', $channels);
108-
$this->assertContains('security', $channels);
182+
$this->assertNotEmpty($results);
183+
$this->assertContains('app', $results);
184+
$this->assertContains('security', $results);
109185
}
110186

111187
public function testByLevel()
112188
{
113-
$results = $this->tool->byLevel('WARNING');
189+
$results = $this->tool->byLevel('INFO');
190+
191+
$this->assertNotEmpty($results);
114192

115-
$this->assertGreaterThanOrEqual(1, \count($results));
116-
foreach ($results as $result) {
117-
$this->assertSame('WARNING', $result['level']);
193+
foreach ($results as $entry) {
194+
$this->assertSame('INFO', $entry['level']);
118195
}
119196
}
197+
198+
public function testByLevelWithEnvironment()
199+
{
200+
// Skip this test as the bridge test fixtures don't have environment-specific files
201+
$this->markTestSkipped('Environment-specific level search not supported in bridge test fixtures');
202+
}
203+
204+
public function testByLevelWithLimit()
205+
{
206+
$results = $this->tool->byLevel('INFO', limit: 1);
207+
208+
$this->assertLessThanOrEqual(1, \count($results));
209+
}
210+
211+
public function testSearchReturnsLogEntryArrayStructure()
212+
{
213+
$results = $this->tool->search('logged');
214+
215+
$this->assertNotEmpty($results);
216+
217+
$entry = $results[0];
218+
$this->assertArrayHasKey('datetime', $entry);
219+
$this->assertArrayHasKey('channel', $entry);
220+
$this->assertArrayHasKey('level', $entry);
221+
$this->assertArrayHasKey('message', $entry);
222+
$this->assertArrayHasKey('context', $entry);
223+
$this->assertArrayHasKey('extra', $entry);
224+
$this->assertArrayHasKey('source_file', $entry);
225+
$this->assertArrayHasKey('line_number', $entry);
226+
}
120227
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[2024-01-15 15:00:00] test.INFO: Test started {"test":"UserControllerTest"} []
2-
[2024-01-15 15:00:05] test.INFO: Test completed {"test":"UserControllerTest","status":"passed"} []
2+
[2024-01-15 15:00:05] test.INFO: Test completed {"test":"UserControllerTest","status":"passed"} []
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[2024-01-15 12:00:00] app.ERROR: Critical system error {"exception":"OutOfMemoryError"} []
2-
[2024-01-15 12:01:00] app.INFO: System recovered successfully {} []
2+
[2024-01-15 12:01:00] app.INFO: System recovered successfully {} []

src/mate/src/Bridge/Symfony/Tests/Capability/ServiceToolTest.php

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,96 @@
1616
use Symfony\AI\Mate\Bridge\Symfony\Service\ContainerProvider;
1717

1818
/**
19-
* @author Tobias Nyholm <[email protected]>
19+
* @author Johannes Wachter <[email protected]>
2020
*/
2121
final class ServiceToolTest extends TestCase
2222
{
23-
public function testGetAllServices()
23+
private string $fixturesDir;
24+
25+
protected function setUp(): void
26+
{
27+
$this->fixturesDir = \dirname(__DIR__).'/Fixtures';
28+
}
29+
30+
public function testGetAllServicesReturnsServicesFromContainer()
31+
{
32+
$provider = new ContainerProvider();
33+
$tool = new ServiceTool($this->fixturesDir, $provider);
34+
35+
$services = $tool->getAllServices();
36+
37+
$this->assertArrayHasKey('cache.app', $services);
38+
$this->assertArrayHasKey('logger', $services);
39+
$this->assertArrayHasKey('event_dispatcher', $services);
40+
41+
$this->assertSame('Symfony\Component\Cache\Adapter\FilesystemAdapter', $services['cache.app']);
42+
$this->assertSame('Psr\Log\NullLogger', $services['logger']);
43+
$this->assertSame('Symfony\Component\EventDispatcher\EventDispatcher', $services['event_dispatcher']);
44+
}
45+
46+
public function testGetAllServicesReturnsEmptyArrayWhenContainerNotFound()
47+
{
48+
$provider = new ContainerProvider();
49+
$tool = new ServiceTool('/non/existent/directory', $provider);
50+
51+
$services = $tool->getAllServices();
52+
53+
$this->assertEmpty($services);
54+
}
55+
56+
public function testGetAllServicesIncludesServicesWithMethodCalls()
57+
{
58+
$provider = new ContainerProvider();
59+
$tool = new ServiceTool($this->fixturesDir, $provider);
60+
61+
$services = $tool->getAllServices();
62+
63+
$this->assertArrayHasKey('event_dispatcher', $services);
64+
$this->assertSame('Symfony\Component\EventDispatcher\EventDispatcher', $services['event_dispatcher']);
65+
}
66+
67+
public function testGetAllServicesIncludesServicesWithTags()
68+
{
69+
$provider = new ContainerProvider();
70+
$tool = new ServiceTool($this->fixturesDir, $provider);
71+
72+
$services = $tool->getAllServices();
73+
74+
$this->assertArrayHasKey('cache.app', $services);
75+
$this->assertArrayHasKey('logger', $services);
76+
}
77+
78+
public function testGetAllServicesResolvesAliases()
79+
{
80+
$provider = new ContainerProvider();
81+
$tool = new ServiceTool($this->fixturesDir, $provider);
82+
83+
$services = $tool->getAllServices();
84+
85+
// my_service is an alias to cache.app
86+
$this->assertArrayHasKey('my_service', $services);
87+
$this->assertSame('Symfony\Component\Cache\Adapter\FilesystemAdapter', $services['my_service']);
88+
}
89+
90+
public function testGetAllServicesStripsLeadingDotsFromServiceIds()
2491
{
25-
$tool = new ServiceTool(
26-
\dirname(__DIR__).'/Fixtures',
27-
new ContainerProvider()
28-
);
92+
$provider = new ContainerProvider();
93+
$tool = new ServiceTool($this->fixturesDir, $provider);
94+
95+
$services = $tool->getAllServices();
96+
97+
// .service_locator.abc123 should be accessible without the leading dot
98+
$this->assertArrayHasKey('service_locator.abc123', $services);
99+
}
100+
101+
public function testGetAllServicesIncludesServicesWithFactory()
102+
{
103+
$provider = new ContainerProvider();
104+
$tool = new ServiceTool($this->fixturesDir, $provider);
105+
106+
$services = $tool->getAllServices();
29107

30-
$output = $tool->getAllServices();
31-
$this->assertCount(6, $output);
108+
$this->assertArrayHasKey('router', $services);
109+
$this->assertSame('Symfony\Component\Routing\Router', $services['router']);
32110
}
33111
}
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<container xmlns="http://symfony.com/schema/dic/services"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
2+
<container xmlns="http://symfony.com/schema/dic/services">
53
<services>
6-
<service id="service.logger" class="Psr\Log\NullLogger">
4+
<service id="cache.app" class="Symfony\Component\Cache\Adapter\FilesystemAdapter">
5+
<tag name="cache.pool"/>
6+
</service>
7+
<service id="logger" class="Psr\Log\NullLogger">
78
<tag name="monolog.logger" channel="app"/>
89
</service>
9-
<service id="service.router" class="Symfony\Component\Routing\Router">
10-
<call method="setContext"/>
11-
<tag name="routing.router"/>
10+
<service id="event_dispatcher" class="Symfony\Component\EventDispatcher\EventDispatcher">
11+
<call method="addListener"/>
1212
</service>
13-
<service id="service.event_dispatcher" class="Symfony\Component\EventDispatcher\EventDispatcher">
14-
<tag name="event_dispatcher"/>
13+
<service id=".service_locator.abc123" class="Symfony\Component\DependencyInjection\ServiceLocator">
14+
<tag name="container.service_locator"/>
1515
</service>
16-
<service id="service.cache" class="Symfony\Component\Cache\Adapter\FilesystemAdapter">
17-
<factory class="Symfony\Component\Cache\Adapter\FilesystemAdapter" method="create"/>
16+
<service id="my_service" alias="cache.app"/>
17+
<service id="router" class="Symfony\Component\Routing\Router">
18+
<factory class="RouterFactory" method="create"/>
1819
</service>
19-
<service id="service.http_client" class="Symfony\Component\HttpClient\HttpClient"/>
20-
<service id="logger.alias" alias="service.logger"/>
2120
</services>
22-
</container>
21+
</container>

0 commit comments

Comments
 (0)