<?php
namespace nur\ldap;

use PHPUnit\Framework\TestCase;

class filtersTest extends TestCase {
  function testParse(): void {
    self::assertSame("(objectClass=*)", filters::parse(null));
    self::assertSame("(objectClass=*)", filters::parse(""));
    self::assertSame("(abc)", filters::parse("abc"));
    self::assertSame("(a=b)", filters::parse("a=b"));
    self::assertSame("(a=b)", filters::parse("(a=b)"));
    self::assertSame("(a=*)", filters::parse("a=*"));
    self::assertSame("(a=*)", filters::parse("(a=*)"));
    self::assertSame("(a=b)", filters::parse([
      "a=b",
    ]));
    self::assertSame("(a=b)", filters::parse([
      "(a=b)",
    ]));
    self::assertSame("(a=b)", filters::parse([
      "a" => "b",
    ]));
    self::assertSame("(a=\\2a)", filters::parse([
      "a" => "*",
    ]));
    self::assertSame("(&(a=b)(c=d))", filters::parse([
      "a=b",
      "c=d",
    ]));
    self::assertSame("(&(a=b)(c=d))", filters::parse([
      "a" => "b",
      "c" => "d",
    ]));
    self::assertSame("(|(a=b)(c=d)(&(x=y)(z=t)))",
      filters::parse([
        "|",
        "a" => "b",
        "c" => "d",
        ["&",
          "x" => "y",
          "z" => "t",
        ],
      ]));
    self::assertSame("(a=b)", filters::parse([
      "&",
      "a" => "b",
    ]));
    self::assertSame("(a=b)", filters::parse([
      "|",
      "a" => "b",
    ]));
    self::assertSame("(!(a=b))", filters::parse([
      "!",
      "a" => "b",
    ]));
  }

  function testParseArrayValues(): void {
    self::assertSame("(&(a=x)(a=y))", filters::parse([
      "a" => ["x", "y"],
    ]));
    self::assertSame("(&(a=x)(a=y))", filters::parse([
      "&",
      "a" => ["x", "y"],
    ]));
    self::assertSame("(|(a=x)(a=y))", filters::parse([
      "|",
      "a" => ["x", "y"],
    ]));
  }

  function testOps(): void {
    self::assertSame("(a=b)", filters::eq("a", "b"));
    self::assertSame("(a=b*)", filters::eq("a", "b*"));
    self::assertSame("(a=*b*)", filters::eq("a", "*b*"));
    self::assertSame("(a=*)", filters::eq("a", "*"));
  }
}
