<?php
namespace nur\sery {
  use nulib\tests\TestCase;
  use nur\sery\impl\config;
  use nur\sery\impl\myapp;
  use nur\sery\impl\MyApplication1;
  use nur\sery\impl\MyApplication2;

  class appTest extends TestCase {
    function testWith() {
      $projdir = config::get_projdir();
      $cwd = getcwd();

      myapp::reset();
      $app1 = myapp::with(MyApplication1::class);
      self::assertSame([
        "projdir" => $projdir,
        "vendor" => [
          "bindir" => "$projdir/vendor/bin",
          "autoload" => "$projdir/vendor/autoload.php",
        ],
        "appcode" => "nur-sery",
        "cwd" => $cwd,
        "datadir" => "$projdir/devel",
        "etcdir" => "$projdir/devel/etc",
        "vardir" => "$projdir/devel/var",
        "logdir" => "$projdir/devel/log",
        "profile" => "devel",
        "appgroup" => null,
        "name" => "my-application1",
        "title" => null,
      ], $app1->getParams());

      $app2 = myapp::with(MyApplication2::class, $app1);
      self::assertSame([
        "projdir" => $projdir,
        "vendor" => [
          "bindir" => "$projdir/vendor/bin",
          "autoload" => "$projdir/vendor/autoload.php",
        ],
        "appcode" => "nur-sery",
        "cwd" => $cwd,
        "datadir" => "$projdir/devel",
        "etcdir" => "$projdir/devel/etc",
        "vardir" => "$projdir/devel/var",
        "logdir" => "$projdir/devel/log",
        "profile" => "devel",
        "appgroup" => null,
        "name" => "my-application2",
        "title" => null,
      ], $app2->getParams());
    }

    function testInit() {
      $projdir = config::get_projdir();
      $cwd = getcwd();

      myapp::reset();
      myapp::init(MyApplication1::class);
      self::assertSame([
        "projdir" => $projdir,
        "vendor" => [
          "bindir" => "$projdir/vendor/bin",
          "autoload" => "$projdir/vendor/autoload.php",
        ],
        "appcode" => "nur-sery",
        "cwd" => $cwd,
        "datadir" => "$projdir/devel",
        "etcdir" => "$projdir/devel/etc",
        "vardir" => "$projdir/devel/var",
        "logdir" => "$projdir/devel/log",
        "profile" => "devel",
        "appgroup" => null,
        "name" => "my-application1",
        "title" => null,
      ], myapp::get()->getParams());

      myapp::init(MyApplication2::class);
      self::assertSame([
        "projdir" => $projdir,
        "vendor" => [
          "bindir" => "$projdir/vendor/bin",
          "autoload" => "$projdir/vendor/autoload.php",
        ],
        "appcode" => "nur-sery",
        "cwd" => $cwd,
        "datadir" => "$projdir/devel",
        "etcdir" => "$projdir/devel/etc",
        "vardir" => "$projdir/devel/var",
        "logdir" => "$projdir/devel/log",
        "profile" => "devel",
        "appgroup" => null,
        "name" => "my-application2",
        "title" => null,
      ], myapp::get()->getParams());
    }
  }
}

namespace nur\sery\impl {

  use nur\sery\app\cli\Application;
  use nur\sery\os\path;
  use nur\sery\app;

  class config {
    const PROJDIR = __DIR__.'/..';

    static function get_projdir(): string {
      return path::abspath(self::PROJDIR);
    }
  }

  class myapp extends app {
    static function reset(): void {
      self::$app = null;
    }
  }

  class MyApplication1 extends Application {
    const PROJDIR = config::PROJDIR;

    function main() {
    }
  }
  class MyApplication2 extends Application {
    const PROJDIR = null;

    function main() {
    }
  }
}
