<?php
namespace nur\ldap;

use nur\b\ValueException;
use nur\t\TestCase;

class namesTest extends TestCase {
  function testSplit(): void {
    $valid = names::split_dn("", $rdn, $suffix);
    self::assertFalse($valid);

    $valid = names::split_dn("a=b", $rdn, $suffix);
    self::assertTrue($valid);
    self::assertSame("a=b", $rdn);
    self::assertSame("", $suffix);

    $valid = names::split_dn("a=b\\2ac", $rdn, $suffix);
    self::assertTrue($valid);
    self::assertSame("a=b*c", $rdn);
    self::assertSame("", $suffix);

    $valid = names::split_dn("a=b\\2bc", $rdn, $suffix);
    self::assertTrue($valid);
    self::assertSame("a=b\\2Bc", $rdn);
    self::assertSame("", $suffix);

    $valid = names::split_dn("a=b,x=y,z=t", $rdn, $suffix);
    self::assertTrue($valid);
    self::assertSame("a=b", $rdn);
    self::assertSame("x=y,z=t", $suffix);

    $valid = names::split_dn("a=b+c=d,x=y,z=t", $rdn, $suffix);
    self::assertTrue($valid);
    self::assertSame("a=b+c=d", $rdn);
    self::assertSame("x=y,z=t", $suffix);
  }

  function testSplit_rdn() {
    self::assertException(ValueException::class, function () {
      names::split_rdn("");
    });
    self::assertException(ValueException::class, function () {
      names::split_rdn("xxx");
    });
    self::assertSame([
      "a" => ["b"],
    ], names::split_rdn("a=b"));
    self::assertSame([
      "a" => ["b"],
      "c" => ["d"],
    ], names::split_rdn("a=b+c=d"));
  }

  function testJoin() {
    self::assertSame("", names::join("", ""));
    self::assertSame("a=b", names::join("a=b", ""));
    self::assertSame("a=b,x=y,z=t", names::join("a=b", "x=y,z=t"));
    self::assertSame("a=b,x=y,z=t", names::join([
      "a" => "b",
    ], "x=y,z=t"));
    self::assertSame("a=b+c=d,x=y,z=t", names::join([
      "a" => "b",
      "c" => "d",
    ], "x=y,z=t"));
  }

  function testUnescape() {
    self::assertSame("(", names::ldap_unescape("\\28"));
  }
}
