<?php

declare(strict_types=1);

namespace Drupal\Tests\flowdrop_interrupt\Unit\Event;

use Drupal\Tests\UnitTestCase;
use Drupal\flowdrop_interrupt\Event\InterruptCreatedEvent;
use Drupal\flowdrop_interrupt\Event\InterruptResolvedEvent;
use Drupal\flowdrop_interrupt\Event\InterruptExpiredEvent;
use Drupal\flowdrop_interrupt\Event\InterruptCancelledEvent;
use Symfony\Contracts\EventDispatcher\Event;

/**
 * Tests the interrupt event classes.
 *
 * @group flowdrop_interrupt
 */
class InterruptEventTest extends UnitTestCase {

  /**
   * Tests InterruptCreatedEvent structure.
   *
   * @covers \Drupal\flowdrop_interrupt\Event\InterruptCreatedEvent
   */
  public function testCreatedEvent(): void {
    $ref = new \ReflectionClass(InterruptCreatedEvent::class);
    $this->assertTrue($ref->isSubclassOf(Event::class));
    $this->assertEquals('flowdrop_interrupt.created', InterruptCreatedEvent::NAME);

    $params = array_map(fn(\ReflectionParameter $p) => $p->getName(), $ref->getConstructor()->getParameters());
    $this->assertEquals(['interrupt', 'type', 'workflowId', 'nodeId', 'sessionId', 'pipelineId'], $params);
  }

  /**
   * Tests InterruptResolvedEvent structure.
   *
   * @covers \Drupal\flowdrop_interrupt\Event\InterruptResolvedEvent
   */
  public function testResolvedEvent(): void {
    $ref = new \ReflectionClass(InterruptResolvedEvent::class);
    $this->assertTrue($ref->isSubclassOf(Event::class));
    $this->assertEquals('flowdrop_interrupt.resolved', InterruptResolvedEvent::NAME);

    $params = array_map(fn(\ReflectionParameter $p) => $p->getName(), $ref->getConstructor()->getParameters());
    $this->assertEquals(['interrupt', 'responseData', 'resolvedBy'], $params);
  }

  /**
   * Tests InterruptExpiredEvent structure.
   *
   * @covers \Drupal\flowdrop_interrupt\Event\InterruptExpiredEvent
   */
  public function testExpiredEvent(): void {
    $ref = new \ReflectionClass(InterruptExpiredEvent::class);
    $this->assertTrue($ref->isSubclassOf(Event::class));
    $this->assertEquals('flowdrop_interrupt.expired', InterruptExpiredEvent::NAME);

    $params = array_map(fn(\ReflectionParameter $p) => $p->getName(), $ref->getConstructor()->getParameters());
    $this->assertEquals(['interrupt'], $params);
  }

  /**
   * Tests InterruptCancelledEvent structure.
   *
   * @covers \Drupal\flowdrop_interrupt\Event\InterruptCancelledEvent
   */
  public function testCancelledEvent(): void {
    $ref = new \ReflectionClass(InterruptCancelledEvent::class);
    $this->assertTrue($ref->isSubclassOf(Event::class));
    $this->assertEquals('flowdrop_interrupt.cancelled', InterruptCancelledEvent::NAME);

    $params = array_map(fn(\ReflectionParameter $p) => $p->getName(), $ref->getConstructor()->getParameters());
    $this->assertEquals(['interrupt'], $params);
  }

}
