<?php
namespace nur\b\coll;

use nur\b\coll\impl\ItemView;
use nur\t\TestCase;

class ArrayViewTest extends TestCase {
  function testBasic() {
    $array = [];
    $v = new ArrayView($array);
    self::assertFalse($v->has("x"));
    $v["x"] = 2;
    self::assertTrue($v->has("x"));
    self::assertSame(2, $v["x"]);
    self::assertSame(["x" => 2], $array);
    $v->del("x");
    self::assertFalse($v->has("x"));
    self::assertSame([], $array);

    $array = [];
    $v = new ArrayView($array, "sub");
    self::assertFalse($v->has("x"));
    $v["x"] = 2;
    self::assertTrue($v->has("x"));
    self::assertSame(2, $v["x"]);
    self::assertSame(["sub" => ["x" => 2]], $array);
    $v->del("x");
    self::assertFalse($v->has("x"));
    self::assertSame(["sub" => []], $array);

    $array = new class extends FancyArray {
      use TGenericArray;
    };
    $array["x"] = 0;
    $array["sub"] = ["y" => 3];
    $v = new ArrayView($array, "sub");
    self::assertFalse($v->has("x"));
    self::assertTrue($v->has("y"));
    $v["x"] = 2;
    self::assertTrue($v->has("x"));
    self::assertSame(2, $v["x"]);
    self::assertSame(["x" => 0, "sub" => ["y" => 3, "x" => 2]], $array->array());
    $v->del("x");
    self::assertFalse($v->has("x"));
    self::assertSame(["x" => 0, "sub" => ["y" => 3]], $array->array());
  }

  function testItemView() {
    $array = ["alice" => null, "bob" => null];
    $alice = new ItemView($array, "alice");
    $bob = new ItemView($array, "bob");
    $alice->setName("Alice");
    $alice->setAge(32);
    self::assertSame([
      "alice" => ["name" => "Alice", "age" => 32],
      "bob" => ["name" => "bob", "age" => 0],
    ], $array);
  }

  #XXX tester vue d'une vue
}
