<?php

/*
 * Soluble Japha
 *
 * @link      https://github.com/belgattitude/soluble-japha
 * @copyright Copyright (c) 2013-2020 Vanvelthem Sébastien
 * @license   MIT License https://github.com/belgattitude/soluble-japha/blob/master/LICENSE.md
 */

namespace SolubleTest\Japha\Bridge;

use Soluble\Japha\Bridge\Adapter;
use Soluble\Japha\Bridge\Exception\JavaException;
use PHPUnit\Framework\TestCase;

/**
 * Generated by PHPUnit_SkeletonGenerator on 2014-11-04 at 16:47:42.
 */
class AdapterJavaDestructTest extends TestCase
{
    /**
     * @var string
     */
    protected $servlet_address;

    /**
     * @var Adapter
     */
    protected $adapter;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp(): void
    {
        \SolubleTestFactories::startJavaBridgeServer();

        $this->servlet_address = \SolubleTestFactories::getJavaBridgeServerAddress();

        $this->adapter = new Adapter([
            'driver' => 'Pjb62',
            'servlet_address' => $this->servlet_address,
        ]);
    }

    public function testDestruct()
    {
        $ba = $this->adapter;

        $hm = $ba->java('java.util.HashMap', [
            'my_key' => 'my_value'
        ]);
        $hm2 = clone $hm;

        // Internal JVM remote object ID
        $hmId = $hm->get__java();
        $hm2Id = $hm2->get__java();

        self::assertIsInt($hmId);
        self::assertEquals($hmId, $hm2Id);
        self::assertGreaterThan(0, $hmId);
        self::assertEquals(8, $hm->get('my_key')->length());

        unset($hm); // or basically $hm->__destruct();

        // todo: needs mockup but
        // Will call __destruct() magic method on
        // $ba->getDriver()->getClient()->getClient()->unref($hmId);
        /*
        try {
            $my_key = $hm2->get('my_key');
            self::assertEquals(8, $my_key->length());
        } catch (JavaException $e) {
            var_dump($e);
            die();
        }*/
    }
}
