File tree Expand file tree Collapse file tree 2 files changed +48
-2
lines changed Expand file tree Collapse file tree 2 files changed +48
-2
lines changed Original file line number Diff line number Diff line change 23
23
{
24
24
public function __construct (
25
25
private ClockInterface $ clock = new SymfonyClock (),
26
+ private ?string $ timezone = null ,
26
27
) {
27
28
}
28
29
29
30
public function __invoke (): string
30
31
{
32
+ $ now = $ this ->clock ->now ();
33
+
34
+ if (null !== $ this ->timezone ) {
35
+ $ now = $ now ->setTimezone (new \DateTimeZone ($ this ->timezone ));
36
+ }
37
+
31
38
return \sprintf (
32
39
'Current date is %s (YYYY-MM-DD) and the time is %s (HH:MM:SS). ' ,
33
- $ this -> clock -> now () ->format ('Y-m-d ' ),
34
- $ this -> clock -> now () ->format ('H:i:s ' ),
40
+ $ now ->format ('Y-m-d ' ),
41
+ $ now ->format ('H:i:s ' ),
35
42
);
36
43
}
37
44
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the Symfony package.
5
+ *
6
+ * (c) Fabien Potencier <[email protected] >
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+ namespace Symfony \AI \Agent \Tests \Toolbox \Tool ;
13
+
14
+ use PHPUnit \Framework \Attributes \CoversClass ;
15
+ use PHPUnit \Framework \TestCase ;
16
+ use Symfony \AI \Agent \Toolbox \Tool \Clock ;
17
+ use Symfony \Component \Clock \MockClock ;
18
+
19
+ #[CoversClass(Clock::class)]
20
+ class ClockTest extends TestCase
21
+ {
22
+ public function testInvokeReturnsCurrentDateTime ()
23
+ {
24
+ $ frozen = new MockClock ('2024-06-01 12:00:00 ' , new \DateTimeZone ('UTC ' ));
25
+ $ clock = new Clock ($ frozen );
26
+ $ output = $ clock ();
27
+
28
+ $ this ->assertSame ('Current date is 2024-06-01 (YYYY-MM-DD) and the time is 12:00:00 (HH:MM:SS). ' , $ output );
29
+ }
30
+
31
+ public function testInvokeWithCustomTimezone ()
32
+ {
33
+ $ frozen = new MockClock ('2024-06-01 12:00:00 ' , new \DateTimeZone ('UTC ' ));
34
+ $ clock = new Clock ($ frozen , 'America/New_York ' );
35
+ $ output = $ clock ();
36
+
37
+ $ this ->assertSame ('Current date is 2024-06-01 (YYYY-MM-DD) and the time is 08:00:00 (HH:MM:SS). ' , $ output );
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments