<?php

declare(strict_types=1);

/*
 * This file is a part of the DiscordPHP project.
 *
 * Copyright (c) 2015-2022 David Cole <david.cole1340@gmail.com>
 * Copyright (c) 2020-present Valithor Obsidion <valithor@discordphp.org>
 *
 * This file is subject to the MIT license that is bundled
 * with this source code in the LICENSE.md file.
 */

use Discord\Discord;
use PHPUnit\Framework\TestCase;

final class DiscordTest extends TestCase
{
    public function testCheckEnvVariablesPresent()
    {
        if (file_exists(__DIR__.'/../.env')) {
            $envFile = file_get_contents(__DIR__.'/../.env');
            $lines = explode("\n", $envFile);
            foreach ($lines as $line) {
                $line = trim($line);
                if (empty($line) || strpos($line, '#') === 0) {
                    continue;
                }
                if (strpos($line, '=') === false) {
                    continue;
                }
                [$key, $value] = explode('=', $line, 2);
                $key = trim($key);
                $value = trim($value);
                if (! empty($key)) {
                    putenv("$key=$value");
                }
            }
        }

        $this->assertNotFalse(getenv('DISCORD_TOKEN'), 'Discord token is missing');
        $this->assertNotFalse(getenv('TEST_CHANNEL'), 'Test channel ID is missing');
        $this->assertNotFalse(getenv('TEST_CHANNEL_NAME'), 'Test channel name is missing');
    }

    /*
    public function testSetGetCacheAsync()
    {
        wait(function (Discord $discord, $resolve) {
            $cache = $discord->getCacheConfig()->interface;
            $this->assertIsObject($cache, 'No CacheInterface set');
            $data = 'DiscordPHP 123';

            $cache->set('DPHP.Test', $data)->then(function ($success) use ($cache, $data) {
                $this->assertTrue($success, 'Failed to set a cache');
                if ($success) {
                    return $cache->get('DPHP.Test')->then(function ($value) use ($data) {
                        $this->assertEquals($data, $value, 'The stored cache mismatched');

                        return $value ? true : false;
                    });
                }

                return $success;
            })->then($resolve, $resolve);
        }, 10);
    }
    */
}
