<?php

declare(strict_types=1);

namespace Apexl\Tests\NotifierTwilio\UnitTests;

use Apexl\Io\NotifierTwilio\Dtos\TwilioConfigDto;
use Apexl\Io\NotifierTwilio\Factories\TwilioClientFactory;
use apexl\Io\TestInfrastructure\UnitTest;
use PHPUnit\Framework\Attributes\Test;
use ReflectionClass;
use Twilio\Base\BaseClient;
use Twilio\Rest\Client;
use function apexl\Io\container;

final class TwilioClientFactoryTest extends UnitTest
{
    #[Test]
    public function makeProducesAClientWithConfiguredCredentials(): void
    {
        $sid = 'AC' . $this->faker->lexify(str_repeat('?', 32));
        $token = $this->faker->lexify(str_repeat('?', 32));
        $from = '+' . $this->faker->numerify('############');

        $factory = new TwilioClientFactory(
            new TwilioConfigDto($sid, $token, $from),
        );

        $client = $factory->make();

        $this->assertInstanceOf(Client::class, $client);
        $this->assertSame($sid, $this->readProtected($client, 'username'));
        $this->assertSame($token, $this->readProtected($client, 'password'));
    }

    private function readProtected(object $object, string $property): mixed
    {
        $ref = new ReflectionClass(BaseClient::class)->getProperty($property);

        return $ref->getValue($object);
    }
}
