<?php
namespace nur\sery\php\access;

use nulib\tests\TestCase;
use nur\sery\wip\php\access\KeyAccess;
use stdClass;

class KeyAccessTest extends TestCase {
  function testAccess() {
    $default = new stdClass();
    $array = ["null" => null, "false" => false, "empty" => ""];

    #
    $a = new KeyAccess($array, "inexistant");
    self::assertFalse($a->exists());
    self::assertFalse($a->available());
    self::assertSame($default, $a->get($default));

    $a = new KeyAccess($array, "null");
    self::assertTrue($a->exists());
    self::assertTrue($a->available());
    self::assertSame(null, $a->get($default));

    $a = new KeyAccess($array, "false");
    self::assertTrue($a->exists());
    self::assertFalse($a->available());
    self::assertSame($default, $a->get($default));

    $a = new KeyAccess($array, "empty");
    self::assertTrue($a->exists());
    self::assertTrue($a->available());
    self::assertSame("", $a->get($default));

    #
    $a = new KeyAccess($array, "null", ["allow_null" => false]);
    self::assertTrue($a->exists());
    self::assertFalse($a->available());
    self::assertSame($default, $a->get($default));

    $a = new KeyAccess($array, "null", ["allow_null" => true]);
    self::assertTrue($a->exists());
    self::assertTrue($a->available());
    self::assertSame(null, $a->get($default));

    #
    $a = new KeyAccess($array, "false", ["allow_false" => false]);
    self::assertTrue($a->exists());
    self::assertFalse($a->available());
    self::assertSame($default, $a->get($default));

    $a = new KeyAccess($array, "false", ["allow_false" => true]);
    self::assertTrue($a->exists());
    self::assertTrue($a->available());
    self::assertSame(false, $a->get($default));

    #
    $a = new KeyAccess($array, "empty", ["allow_empty" => false]);
    self::assertTrue($a->exists());
    self::assertFalse($a->available());
    self::assertSame($default, $a->get($default));

    $a = new KeyAccess($array, "empty", ["allow_empty" => true]);
    self::assertTrue($a->exists());
    self::assertTrue($a->available());
    self::assertSame("", $a->get($default));
  }
}
