<?php
namespace ccxt;

// ----------------------------------------------------------------------------

// PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
// https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code

// -----------------------------------------------------------------------------
include_once PATH_TO_CCXT . '/test/exchange/base/test_shared_methods.php';

function helper_test_init_throttler() {
    $exchange = new \ccxt\Exchange(array(
        'id' => 'sampleexchange',
        'rateLimit' => 10.8,
    ));
    // todo: assert (exchange.MAX_VALUE !== undefined);
    $token_bucket = $exchange->get_property($exchange, 'tokenBucket'); // trick for uncamelcase transpilation
    if ($token_bucket === null) {
        $token_bucket = $exchange->get_property($exchange, 'TokenBucket');
    }
    assert($token_bucket !== null);
    assert('GO_SKIP_START');
    $rate_limit = $exchange->get_property($exchange, 'rateLimit');
    assert($rate_limit === 10.8);
    assert($token_bucket['delay'] === 0.001);
    assert($token_bucket['refillRate'] === 1 / $rate_limit);
    assert('GO_SKIP_END');
    // fix decimal/integer issues across langs
    assert($exchange->in_array($token_bucket['capacity'], [1, 1]));
    $cost = $exchange->parse_to_numeric($exchange->safe_string_2($token_bucket, 'cost', 'defaultCost')); // python sync, todo fix
    assert($exchange->in_array($cost, [1, 1]));
    assert(!(is_array($token_bucket) && array_key_exists('maxCapacity', $token_bucket)) || $exchange->in_array($token_bucket['maxCapacity'], [1000, 1000]));
}


function helper_test_sandbox_state($exchange, $should_be_enabled = true) {
    assert($exchange->urls !== null);
    assert(is_array($exchange->urls) && array_key_exists('test', $exchange->urls));
    assert('GO_SKIP_START');
    $is_sandbox_mode_enabled = $exchange->get_property($exchange, 'isSandboxModeEnabled');
    if ($should_be_enabled) {
        assert($is_sandbox_mode_enabled);
        assert($exchange->urls['api']['public'] === 'https://example.org');
        assert($exchange->urls['apiBackup']['public'] === 'https://example.com');
    } else {
        assert(!$is_sandbox_mode_enabled);
        assert($exchange->urls['api']['public'] === 'https://example.com');
        assert($exchange->urls['test']['public'] === 'https://example.org');
    }
    assert('GO_SKIP_END');
}


function helper_test_init_sandbox() {
    // todo: sandbox for real exchanges
    $opts = array(
        'id' => 'sampleexchange',
        'options' => array(
            'sandbox' => false,
        ),
        'urls' => array(
            'api' => array(
                'public' => 'https://example.com',
            ),
            'test' => array(
                'public' => 'https://example.org',
            ),
        ),
    );
    //
    // CASE A: when sandbox is not enabled
    //
    $exchange3 = new \ccxt\Exchange($opts);
    helper_test_sandbox_state($exchange3, false);
    $exchange3->set_sandbox_mode(true);
    helper_test_sandbox_state($exchange3, true);
    //
    // CASE B: when sandbox is enabled
    //
    $opts['options']['sandbox'] = true;
    $exchange4 = new \ccxt\Exchange($opts);
    helper_test_sandbox_state($exchange4, true);
    $exchange4->set_sandbox_mode(false);
    helper_test_sandbox_state($exchange4, false);
}


function helper_test_init_market() {
    // ############# markets ############# //
    $sample_market = array(
        'id' => 'BtcUsd',
        'symbol' => 'BTC/USD',
        'base' => 'BTC',
        'quote' => 'USD',
        'baseId' => 'Btc',
        'quoteId' => 'Usd',
        'type' => 'spot',
        'spot' => true,
    );
    $exchange2 = new \ccxt\Exchange(array(
        'id' => 'sampleexchange',
        'markets' => array(
            'BTC/USD' => $sample_market,
        ),
    ));
    assert($exchange2->markets['BTC/USD'] !== null);
}


function test_after_constructor() {
    helper_test_init_throttler();
    helper_test_init_sandbox();
    helper_test_init_market();
}
