Skip to content

Commit

Permalink
use a clock to create datetime instances
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed May 24, 2024
1 parent 1f90bc3 commit e8ccc55
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Notifier\Bridge\Bluesky;

use Psr\Log\LoggerInterface;
use Symfony\Component\Clock\Clock;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Mime\Part\File;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
Expand All @@ -32,15 +34,19 @@
final class BlueskyTransport extends AbstractTransport
{
private array $authSession = [];
private ClockInterface $clock;

public function __construct(
#[\SensitiveParameter] private string $user,
#[\SensitiveParameter] private string $password,
private LoggerInterface $logger,
?HttpClientInterface $client = null,
?EventDispatcherInterface $dispatcher = null,
?ClockInterface $clock = null,
) {
parent::__construct($client, $dispatcher);

$this->clock = $clock ?? Clock::get();
}

public function __toString(): string
Expand All @@ -66,7 +72,7 @@ protected function doSend(MessageInterface $message): SentMessage
$post = [
'$type' => 'app.bsky.feed.post',
'text' => $message->getSubject(),
'createdAt' => \DateTimeImmutable::createFromFormat('U', time())->format('Y-m-d\\TH:i:s.u\\Z'),
'createdAt' => $this->clock->now()->format('Y-m-d\\TH:i:s.u\\Z'),
];
if ([] !== $facets = $this->parseFacets($post['text'])) {
$post['facets'] = $facets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Psr\Log\NullLogger;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\JsonMockResponse;
use Symfony\Component\Mime\Part\File;
Expand All @@ -28,15 +29,16 @@

final class BlueskyTransportTest extends TransportTestCase
{
private static $clock;

protected function setUp(): void
{
ClockMock::register(self::class);
ClockMock::withClockMock(1714293617);
self::$clock = new MockClock(new \DateTimeImmutable('@1714293617'));
}

public static function createTransport(?HttpClientInterface $client = null): BlueskyTransport
{
$blueskyTransport = new BlueskyTransport('username', 'password', new NullLogger(), $client ?? new MockHttpClient());
$blueskyTransport = new BlueskyTransport('username', 'password', new NullLogger(), $client ?? new MockHttpClient(), null, self::$clock);
$blueskyTransport->setHost('bsky.social');

return $blueskyTransport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"require": {
"php": ">=8.2",
"psr/log": "^1|^2|^3",
"symfony/clock": "^6.4|^7.0",
"symfony/http-client": "^6.4|^7.0",
"symfony/notifier": "^7.2",
"symfony/string": "^6.4|^7.0"
Expand Down

0 comments on commit e8ccc55

Please sign in to comment.