<?php

/**
 * TradeApiTest
 * PHP version 8.1.
 *
 * @category Class
 *
 * @author   OpenAPI Generator team
 *
 * @see     https://openapi-generator.tech
 */

/**
 * Binance Convert REST API.
 *
 * OpenAPI Specification for the Binance Convert REST API
 *
 * The version of the OpenAPI document: 1.0.0
 * Generated by: https://openapi-generator.tech
 * Generator version: 7.12.0
 */

/**
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Please update the test case below to test the endpoint.
 */

namespace Binance\Client\Convert\Test\Api;

use Binance\Client\Convert\Api\TradeApi;
use Binance\Client\Convert\Model\AcceptQuoteRequest;
use Binance\Client\Convert\Model\CancelLimitOrderRequest;
use Binance\Client\Convert\Model\PlaceLimitOrderRequest;
use Binance\Client\Convert\Model\QueryLimitOpenOrdersRequest;
use Binance\Client\Convert\Model\SendQuoteRequestRequest;
use Binance\Common\Configuration\ClientConfiguration;
use Binance\Common\Configuration\SignatureConfiguration;
use Binance\Common\HttpClient;
use GuzzleHttp\Psr7\Utils;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;

/**
 * TradeApiTest Class Doc Comment.
 *
 * @category Class
 *
 * @author   OpenAPI Generator team
 *
 * @see     https://openapi-generator.tech
 *
 * @internal
 *
 * @coversNothing
 */
class TradeApiTest extends TestCase
{
    public function getApiMock(&$request)
    {
        $clientMock = $this->createMock(HttpClient::class);
        $signatureConfig = new SignatureConfiguration();
        $signatureConfig->setApiKey('apiKey');
        $signatureConfig->setSecretKey('secretKey');
        $clientConfig = new ClientConfiguration();
        $clientConfig->setUrl('http://localhost:8080');
        $clientConfig->setSignatureConfiguration($signatureConfig);
        $responseMock = $this->createMock(ResponseInterface::class);

        $responseMock->expects($this->any())
            ->method('getStatusCode')
            ->willReturn(200)
        ;

        $responseMock->expects($this->any())
            ->method('getBody')
            ->willReturn(Utils::streamFor('{}'))
        ;

        $clientMock->expects($this->once())
            ->method('send')
            ->will($this->returnCallback(function ($actualRequest) use (&$request, $responseMock) {
                $request = $actualRequest;

                return $responseMock;
            }))
        ;

        $apiMock = $this->getMockBuilder(TradeApi::class)
            ->setConstructorArgs([$clientConfig, $clientMock])
            ->onlyMethods(['getTimestamp'])
            ->getMock()
        ;

        $apiMock->method('getTimestamp')->willReturn('1736393892000');

        return $apiMock;
    }

    /**
     * Test case for acceptQuote.
     *
     * Accept Quote (TRADE).
     */
    public function testAcceptQuote()
    {
        $acceptQuoteRequest = new AcceptQuoteRequest();
        $acceptQuoteRequest->setQuoteId('1');

        $response = $this->getApiMock($request)->acceptQuote($acceptQuoteRequest);

        parse_str($request->getUri(), $queryMap);

        self::assertEquals(200, $response->getStatusCode());
        self::assertEquals('/sapi/v1/convert/acceptQuote', $request->getUri()->getPath());
        self::assertEquals('0624eea4af8a8321e2c84e368ed873d71a9a5c971e629175b93556bd22fc325d', $queryMap['signature']);
    }

    /**
     * Test case for cancelLimitOrder.
     *
     * Cancel limit order (USER_DATA).
     */
    public function testCancelLimitOrder()
    {
        $cancelLimitOrderRequest = new CancelLimitOrderRequest();
        $cancelLimitOrderRequest->setOrderId('1');

        $response = $this->getApiMock($request)->cancelLimitOrder($cancelLimitOrderRequest);

        parse_str($request->getUri(), $queryMap);

        self::assertEquals(200, $response->getStatusCode());
        self::assertEquals('/sapi/v1/convert/limit/cancelOrder', $request->getUri()->getPath());
        self::assertEquals('ad8bc32c069fb75b8afba1bc7d8f4dc63cd336877366a3b3bc88918201410d88', $queryMap['signature']);
    }

    /**
     * Test case for getConvertTradeHistory.
     *
     * Get Convert Trade History(USER_DATA).
     */
    public function testGetConvertTradeHistory()
    {
        $startTime = 1623319461670;
        $endTime = 1641782889000;
        $limit = 100;
        $recvWindow = 5000;
        $response = $this->getApiMock($request)->getConvertTradeHistory($startTime, $endTime, $limit, $recvWindow);

        parse_str($request->getUri(), $queryMap);

        self::assertEquals(200, $response->getStatusCode());
        self::assertEquals('/sapi/v1/convert/tradeFlow', $request->getUri()->getPath());
        self::assertEquals('23689f3330f331d9db9c46c9dc87d2217486442f1746adfdda02c10e3dbde13e', $queryMap['signature']);
    }

    /**
     * Test case for orderStatus.
     *
     * Order status(USER_DATA).
     */
    public function testOrderStatus()
    {
        $orderId = '1';
        $quoteId = '1';
        $response = $this->getApiMock($request)->orderStatus($orderId, $quoteId);

        parse_str($request->getUri(), $queryMap);

        self::assertEquals(200, $response->getStatusCode());
        self::assertEquals('/sapi/v1/convert/orderStatus', $request->getUri()->getPath());
        self::assertEquals('c8ea02b290636497dfc9f4f59d01ea6ceff503bf1955b020615bd7bb133a81c4', $queryMap['signature']);
    }

    /**
     * Test case for placeLimitOrder.
     *
     * Place limit order (USER_DATA).
     */
    public function testPlaceLimitOrder()
    {
        $placeLimitOrderRequest = new PlaceLimitOrderRequest();
        $placeLimitOrderRequest->setBaseAsset('');
        $placeLimitOrderRequest->setQuoteAsset('');
        $placeLimitOrderRequest->setLimitPrice(1.0);
        $placeLimitOrderRequest->setSide('BUY');
        $placeLimitOrderRequest->setExpiredType('');

        $response = $this->getApiMock($request)->placeLimitOrder($placeLimitOrderRequest);

        parse_str($request->getUri(), $queryMap);

        self::assertEquals(200, $response->getStatusCode());
        self::assertEquals('/sapi/v1/convert/limit/placeOrder', $request->getUri()->getPath());
        self::assertEquals('454bb8d2634e8e0b341da8d3343d9aad8c2c3ad66dd6a060ebcd0be45a48caca', $queryMap['signature']);
    }

    /**
     * Test case for queryLimitOpenOrders.
     *
     * Query limit open orders (USER_DATA).
     */
    public function testQueryLimitOpenOrders()
    {
        $queryLimitOpenOrdersRequest = new QueryLimitOpenOrdersRequest();

        $response = $this->getApiMock($request)->queryLimitOpenOrders($queryLimitOpenOrdersRequest);

        parse_str($request->getUri(), $queryMap);

        self::assertEquals(200, $response->getStatusCode());
        self::assertEquals('/sapi/v1/convert/limit/queryOpenOrders', $request->getUri()->getPath());
        self::assertEquals('dc0def720e795be0f84b02a8fa211ecc3a27dd06ffdbd287a679f1321807f820', $queryMap['signature']);
    }

    /**
     * Test case for sendQuoteRequest.
     *
     * Send Quote Request(USER_DATA).
     */
    public function testSendQuoteRequest()
    {
        $sendQuoteRequestRequest = new SendQuoteRequestRequest();
        $sendQuoteRequestRequest->setFromAsset('');
        $sendQuoteRequestRequest->setToAsset('');

        $response = $this->getApiMock($request)->sendQuoteRequest($sendQuoteRequestRequest);

        parse_str($request->getUri(), $queryMap);

        self::assertEquals(200, $response->getStatusCode());
        self::assertEquals('/sapi/v1/convert/getQuote', $request->getUri()->getPath());
        self::assertEquals('c2a41c5811da81c00258e452c20a96710858f159abce153ff6651b9a78b4490c', $queryMap['signature']);
    }
}
