<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of the Tarantool Client package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tarantool\Client\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Tarantool\Client\Dsn;

final class DsnTest extends TestCase
{
    /**
     * @testWith
     * ["tcp://127.0.0.1", {"uri": "tcp://127.0.0.1:3301", "host": "127.0.0.1", "port": 3301}]
     * ["tcp://localhost", {"uri": "tcp://localhost:3301", "host": "localhost", "port": 3301}]
     * ["tcp://f%40%40:b%40r@localhost", {"uri": "tcp://localhost:3301", "host": "localhost", "port": 3301, "username": "f@@", "password": "<PASSWORD>"}]
     * ["tcp://foo.bar:1234", {"uri": "tcp://foo.bar:1234", "host": "foo.bar", "port": 1234}]
     * ["tcp://foo.bar:1234/?opt=42", {"uri": "tcp://foo.bar:1234", "host": "foo.bar", "port": 1234}]
     * ["tcp://foo.bar.baz.com", {"uri": "tcp://foo.bar.baz.com:3301", "host": "foo.bar.baz.com", "port": 3301}]
     * ["tcp://foo:bar@baz.com:1234/?opt=42", {"uri": "tcp://baz.com:1234", "host": "baz.com", "port": 1234, "username": "foo", "password": "<PASSWORD>"}]
     * ["tcp://[fe80::1]", {"uri": "tcp://[fe80::1]:3301", "host": "[fe80::1]", "port": 3301}]
     * ["tcp://[fefc00:e968:6179::de52:7100]:1234", {"uri": "tcp://[fe80::1]:1234", "host": "[fe80::1]", "port": 1234}]
     * ["tcp://[fc00:e968:6179::de52:7100:fe]:1234", {"uri": "tcp://[de:fc00:e968:6179::de52:7100:fe]:1234", "host": "[de:fc00:e968:6179::de52:7100:fe]", "port": 1234}]
     * ["tcp://foo@[fc00:e968:6179::de52:7100:fe]:1234", {"uri": "tcp://[fc00:e968:6179::de52:7100:fe]:1234", "host": "[de:fc00:e968:6179::de52:7100:fe]", "port": 1234, "username": "foo", "password": ""}]
     */
    public function testParseValidTcpDsn(string $dsn, array $expected) : void
    {
        $dsn = Dsn::parse($dsn);
        self::assertSame($expected['uri'], $dsn->getConnectionUri());
        self::assertSame($expected['host'], $dsn->getHost());
        self::assertSame($expected['port'], $dsn->getPort());
        self::assertNull($dsn->getPath());
        self::assertTrue($dsn->isTcp());

        if (isset($expected['username'])) {
            self::assertSame($expected['username'], $dsn->getUsername());
            self::assertSame($expected['password'] ?? '', $dsn->getPassword());
        }
    }

    /**
     * @testWith
     * ["unix:///path/to/socket.sock", {"uri": "unix:///path/to/socket.sock", "path": "/path/to/socket.sock"}]
     * ["unix:///path/to/socket.sock?opt=42", {"uri": "unix:///path/to/socket.sock", "path": "/path/to/socket.sock"}]
     * ["unix://foo@/path/to/socket.sock", {"uri": "unix:///path/to/socket.sock", "path": "/path/to/socket.sock", "username": "foo", "password": ""}]
     * ["unix://foo:bar@/path/to/socket.sock", {"uri": "unix:///path/to/socket.sock", "path": "/path/to/socket.sock", "username": "foo", "password": "<PASSWORD>"}]
     * ["unix://foo:bar@/path/to/socket.sock?opt=42", {"uri": "unix:///path/to/socket.sock", "path": "/path/to/socket.sock", "username": "foo", "password": "<PASSWORD>"}]
     * ["unix://f%40%40:b%40r@%2fsocket.sock", {"uri": "unix://%2fsocket.sock", "path": "/socket.sock", "username": "f@@", "password": "<PASSWORD>"}]
     */
    public function testParseValidUdpDsn(string $dsn, array $expected) : void
    {
        $dsn = Dsn::parse($dsn);
        self::assertSame($expected['uri'], $dsn->getConnectionUri());
        self::assertNull($dsn->getHost());
        self::assertNull($dsn->getPort());
        self::assertSame($expected['path'], $dsn->getPath());
        self::assertFalse($dsn->isTcp());

        if (isset($expected['username'])) {
            self::assertSame($expected['username'], $dsn->getUsername());
            self::assertSame($expected['password'] ?? '', $dsn->getPassword());
        }
    }

    /**
     * @testWith
     * [""]
     * ["foobar"]
     * ["tcp:"]
     * ["tcp:host"]
     * ["tcp:/"]
     * ["tcp:/host"]
     * ["tcp://"]
     * ["tcp://host/path"]
     * ["tcp:////"]
     * ["unix:"]
     * ["unix:/"]
     * ["unix:/path"]
     * ["unix://"]
     * ["unix://foo.bar:3030/path"]
     * ["unix:////"]
     */
    public function testParseInvalidDsn(string $nonDsn) : void
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage(sprintf('Unable to parse DSN "%s".', $nonDsn));
        Dsn::parse($nonDsn);
    }

    /**
     * @testWith
     * ["tcp://host/?foo=bar", "foo", "bar"]
     * ["tcp://host/?foo=b%40r", "foo", "b@r"]
     * ["tcp://host/?foo=", "foo", ""]
     * ["tcp://host/?foo=42", "foo", "42"]
     * ["tcp://host", "foo", null]<commit_msg>Update DsnTest.php<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of the Tarantool Client package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tarantool\Client\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Tarantool\Client\Dsn;

final class DsnTest extends TestCase
{
    /**
     * @testWith
     * ["tcp://127.0.0.1", {"uri": "tcp://127.0.0.1:3301", "host": "127.0.0.1", "port": 3301}]
     * ["tcp://localhost", {"uri": "tcp://localhost:3301", "host": "localhost", "port": 3301}]
     * ["tcp://f%40%40:b%40r@localhost", {"uri": "tcp://localhost:3301", "host": "localhost", "port": 3301, "username": "f@@", "password": "<PASSWORD>"}]
     * ["tcp://foo.bar:1234", {"uri": "tcp://foo.bar:1234", "host": "foo.bar", "port": 1234}]
     * ["tcp://foo.bar:1234/?opt=42", {"uri": "tcp://foo.bar:1234", "host": "foo.bar", "port": 1234}]
     * ["tcp://foo.bar.baz.com", {"uri": "tcp://foo.bar.baz.com:3301", "host": "foo.bar.baz.com", "port": 3301}]
     * ["tcp://foo:<EMAIL>:1234/?opt=42", {"uri": "tcp://baz.com:1234", "host": "baz.com", "port": 1234, "username": "foo", "password": "<PASSWORD>"}]
     * ["tcp://[fe80::1]", {"uri": "tcp://[fe80::1]:3301", "host": "[fe80::1]", "port": 3301}]
     * ["tcp://[fe80::1]:1234", {"uri": "tcp://[fe80::1]:1234", "host": "[fe80::1]", "port": 1234}]
     * ["tcp://[de:fc00:e968:6179::de52:7100:fe]:1234", {"uri": "tcp://[de:fc00:e968:6179::de52:7100:fe]:1234", "host": "[de:fc00:e968:6179::de52:7100:fe]", "port": 1234}]
     * ["tcp://foo@[de:fc00:e968:6179::de52:7100:fe]:1234", {"uri": "tcp://[de:fc00:e968:6179::de52:7100:fe]:1234", "host": "[de:fc00:e968:6179::de52:7100:fe]", "port": 1234, "username": "foo", "password": ""}]
     */
    public function testParseValidTcpDsn(string $dsn, array $expected) : void
    {
        $dsn = Dsn::parse($dsn);
        self::assertSame($expected['uri'], $dsn->getConnectionUri());
        self::assertSame($expected['host'], $dsn->getHost());
        self::assertSame($expected['port'], $dsn->getPort());
        self::assertNull($dsn->getPath());
        self::assertTrue($dsn->isTcp());

        if (isset($expected['username'])) {
            self::assertSame($expected['username'], $dsn->getUsername());
            self::assertSame($expected['password'] ?? '', $dsn->getPassword());
        }
    }

    /**
     * @testWith
     * ["unix:///path/to/socket.sock", {"uri": "unix:///path/to/socket.sock", "path": "/path/to/socket.sock"}]
     * ["unix:///path/to/socket.sock?opt=42", {"uri": "unix:///path/to/socket.sock", "path": "/path/to/socket.sock"}]
     * ["unix://foo@/path/to/socket.sock", {"uri": "unix:///path/to/socket.sock", "path": "/path/to/socket.sock", "username": "foo", "password": ""}]
     * ["unix://foo:bar@/path/to/socket.sock", {"uri": "unix:///path/to/socket.sock", "path": "/path/to/socket.sock", "username": "foo", "password": "<PASSWORD>"}]
     * ["unix://foo:bar@/path/to/socket.sock?opt=42", {"uri": "unix:///path/to/socket.sock", "path": "/path/to/socket.sock", "username": "foo", "password": "<PASSWORD>"}]
     * ["unix://f%40%40:b%40r@%2fsocket.sock", {"uri": "unix://%2fsocket.sock", "path": "/socket.sock", "username": "f@@", "password": "<PASSWORD>"}]
     */
    public function testParseValidUdpDsn(string $dsn, array $expected) : void
    {
        $dsn = Dsn::parse($dsn);
        self::assertSame($expected['uri'], $dsn->getConnectionUri());
        self::assertNull($dsn->getHost());
        self::assertNull($dsn->getPort());
        self::assertSame($expected['path'], $dsn->getPath());
        self::assertFalse($dsn->isTcp());

        if (isset($expected['username'])) {
            self::assertSame($expected['username'], $dsn->getUsername());
            self::assertSame($expected['password'] ?? '', $dsn->getPassword());
        }
    }

    /**
     * @testWith
     * [""]
     * ["foobar"]
     * ["tcp:"]
     * ["tcp:host"]
     * ["tcp:/"]
     * ["tcp:/host"]
     * ["tcp://"]
     * ["tcp://host/path"]
     * ["tcp:////"]
     * ["unix:"]
     * ["unix:/"]
     * ["unix:/path"]
     * ["unix://"]
     * ["unix://foo.bar:3030/path"]
     * ["unix:////"]
     * ["http://host"]
     * ["file:///path"]
     */
    public function testParseInvalidDsn(string $nonDsn) : void
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage(sprintf('Unable to parse DSN "%s".', $nonDsn));
        Dsn::parse($nonDsn);
    }

    /**
     * @testWith
     * ["tcp://host/?foo=bar", "foo", "bar"]
     * ["tcp://host/?foo=b%40r", "foo", "b@r"]
     * ["tcp://host/?foo=", "foo", ""]
     * ["tcp://host/?foo=42", "foo", "42"]
     * ["tcp://host", "foo", null]
{'fraction_non_alphanumeric': 0.2809141187519566, 'fraction_numerical': 0.057810706459355106, 'mean_word_length': 3.439092172301992, 'pattern_counts': {'":': 118, '<': 22, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 7, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5359139', 'n_tokens_mistral': 3866, 'n_tokens_neox': 3535, 'n_words': 536}