<?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\ThirdParty;

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

/**
 * Generated by PHPUnit_SkeletonGenerator on 2014-11-04 at 16:47:42.
 */
class JsonIoTest 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
    {
        if (!$this->isJsonIoTestsEnabled()) {
            $this->markTestSkipped(
                'Skipping JSONIO tests, enable option in phpunit.xml'
            );
        }
        \SolubleTestFactories::startJavaBridgeServer();
        $this->servlet_address = \SolubleTestFactories::getJavaBridgeServerAddress();
        $this->adapter = new Adapter([
            'driver' => 'Pjb62',
            'servlet_address' => $this->servlet_address,
        ]);
    }

    /**
     * @see https://github.com/jdereg/json-io
     */
    public function testJavaSimpleJsonSerialization()
    {
        $ba = $this->adapter;
        $jsonWriter = $ba->javaClass('com.cedarsoftware.util.io.JsonWriter');

        $string = $ba->java('java.lang.String', 'Hello world');
        $encoded = $jsonWriter->objectToJson($string);
        self::assertEquals('"Hello world"', (string) $encoded);
    }

    public function testObjectJsonSerialization()
    {
        $ba = $this->adapter;
        $jsonWriter = $ba->javaClass('com.cedarsoftware.util.io.JsonWriter');

        $simpleDateFormat = $ba->java('java.text.SimpleDateFormat', 'yyyy-MM-dd');

        $hashMap = $ba->java('java.util.HashMap', [
            'integer' => 1,
            'phpstring' => 'PHP Héllo',
            'javastring' => $ba->java('java.lang.String', 'Java Héllo'),
            'javadate' => $simpleDateFormat->parse('2017-05-20')
        ]);

        $jsonString = (string) $jsonWriter->objectToJson($hashMap);
        // Will produce
        // {
        //   "@type":"java.util.HashMap",
        //   "javastring":"Java Héllo",
        //   "javadate": {
        //         "@type":"date",
        //         "value":1495231200000
        //   },
        //   "phpstring":"PHP Héllo",
        //   "integer": {
        //          "@type":"int",
        //          "value":1
        //   }
        // }

        self::assertJson($jsonString);
        $decoded = json_decode($jsonString);
        self::assertEquals('date', $decoded->javadate->{'@type'});
        self::assertEquals('Java Héllo', $decoded->javastring);
    }

    protected function isJsonIoTestsEnabled()
    {
        return isset($_SERVER['JAPHA_ENABLE_JSONIO_TESTS']) &&
            $_SERVER['JAPHA_ENABLE_JSONIO_TESTS'] == true;
    }
}
