<?php

use PHPUnit\Framework\TestCase;

class ProducerTest extends TestCase
{
    protected function setUp(): void
    {
        if (!extension_loaded('rdkafka')) {
            $this->markTestSkipped('rdkafka extension not installed.');
        }
    }

    public function testEmptyTopicThrows(): void
    {
        $producer = new Producer(new KafkaConfig('127.0.0.1:9092'));

        $this->expectException(\InvalidArgumentException::class);
        $producer->produce('', 'hello');
    }

    public function testUnencodablePayloadThrows(): void
    {
        $producer = new Producer(new KafkaConfig('127.0.0.1:9092'));

        // A resource cannot be JSON encoded.
        $resource = fopen('php://memory', 'r');

        $this->expectException(\RuntimeException::class);
        try {
            $producer->produce('orders', $resource);
        } finally {
            fclose($resource);
        }
    }

    public function testConstructorBuildsProducer(): void
    {
        // Constructing must not require a live broker (librdkafka is lazy).
        $producer = new Producer(new KafkaConfig('127.0.0.1:9092'));
        $this->assertInstanceOf(Producer::class, $producer);
    }
}
