<?php

declare(strict_types=1);

namespace Drupal\Tests\simple_interactive_maps\Unit;

use Drupal\simple_interactive_maps\SvgHelper;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;

/**
 * Tests SvgHelper's path bounding-box and viewBox calculations.
 */
#[Group('simple_interactive_maps')]
#[CoversClass(SvgHelper::class)]
final class SvgHelperTest extends UnitTestCase {

  /**
   * Tests getBoundingBox() for line commands.
   */
  public function testGetBoundingBoxForLineCommands(): void {
    $box = SvgHelper::getBoundingBox('M0,0L10,0L10,10L0,10Z');
    $this->assertSame([0.0, 0.0, 10.0, 10.0], array_map('floatval', $box));
  }

  /**
   * Tests getBoundingBox() for horizontal and vertical commands.
   */
  public function testGetBoundingBoxForHorizontalAndVerticalCommands(): void {
    $box = SvgHelper::getBoundingBox('M5,5H15V25');
    $this->assertSame([5.0, 5.0, 10.0, 20.0], array_map('floatval', $box));
  }

  /**
   * A cubic bezier whose control points lie on the line is a straight line.
   */
  public function testGetBoundingBoxForDegenerateCubicBezier(): void {
    $box = SvgHelper::getBoundingBox('M0,0C0,0,10,10,10,10');
    $this->assertSame([0.0, 0.0, 10.0, 10.0], array_map('floatval', $box));
  }

  /**
   * Tests that calculateViewBox() combines multiple paths.
   */
  public function testCalculateViewBoxCombinesMultiplePaths(): void {
    $viewBox = SvgHelper::calculateViewBox([
      'M0,0L10,0L10,10L0,10Z',
      'M20,20L30,20L30,30L20,30Z',
    ]);
    $this->assertSame('0 0 30 30', $viewBox);
  }

  /**
   * Tests calculateViewBox() with a single path.
   */
  public function testCalculateViewBoxWithSinglePath(): void {
    $viewBox = SvgHelper::calculateViewBox(['M5,5L15,5L15,15L5,15Z']);
    $this->assertSame('5 5 10 10', $viewBox);
  }

}
