<commit_before><?php

namespace React\Tests\MySQL;

class ResultQueryTest extends BaseTestCase
{
    public function testSelectStaticText()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select \'foo\'', function ($command, $conn) {
            $this->assertEquals(false, $command->hasError());
            $this->assertCount(1, $command->resultRows);
            $this->assertCount(1, $command->resultRows[0]);
            $this->assertEquals('foo', reset($command->resultRows[0]));
            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        });

        $connection->close();
        $loop->run();
    }

    public function testSelectStaticTextWithParamBinding()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select ?', function ($command, $conn) {
            $this->assertEquals(false, $command->hasError());
            $this->assertCount(1, $command->resultRows);
            $this->assertCount(1, $command->resultRows[0]);
            $this->assertEquals('foo', reset($command->resultRows[0]));
            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        }, 'foo');

        $connection->close();
        $loop->run();
    }

    public function testSelectStaticTextWithQuestionMark()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select \'hello?\'', function ($command, $conn) {
            $this->assertEquals(false, $command->hasError());
            $this->assertCount(1, $command->resultRows);
            $this->assertCount(1, $command->resultRows[0]);
            $this->assertEquals('hello?', reset($command->resultRows[0]));
            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        });

        $connection->close();
        $loop->run();
    }

    public function testSelectLongStaticTextWithProperType()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $length = 40000;

        $connection->query('SELECT ?', function ($command, $conn) use ($length) {
            $this->assertEquals(false, $command->hasError());
            $this->assertCount(1, $command->resultFields);
            $this->assertEquals($length * 3, $command->resultFields[0]['length']);
            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        }, str_repeat('.', $length));

        $connection->close();
        $loop->run();
    }

    public function testSimpleSelect()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        // re-create test "book" table
        $connection->query('DROP TABLE IF EXISTS book');<commit_msg>Add tests for different result sets and data types and empty values<commit_after><?php

namespace React\Tests\MySQL;

use React\MySQL\Protocal\Constants;

class ResultQueryTest extends BaseTestCase
{
    public function testSelectStaticText()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select \'foo\'', function ($command, $conn) {
            $this->assertEquals(false, $command->hasError());

            $this->assertCount(1, $command->resultRows);
            $this->assertCount(1, $command->resultRows[0]);
            $this->assertEquals('foo', reset($command->resultRows[0]));

            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        });

        $connection->close();
        $loop->run();
    }

    public function provideValuesThatWillBeReturnedAsIs()
    {
        return array_map(function ($e) { return array($e); }, array(
            'foo',
            'hello?',
            'FööBär',
            'pile of 💩',
            '<>&--\\\'";',
            "\0\1\2\3\4\5\6\7\8\xff",
            '',
            null
        ));
    }

    public function provideValuesThatWillBeConvertedToString()
    {
        return array(
            array(1, '1'),
            array(1.5, '1.5'),
            array(true, '1'),
            array(false, '0')
        );
    }

    /**
     * @dataProvider provideValuesThatWillBeReturnedAsIs
     */
    public function testSelectStaticValueWillBeReturnedAsIs($value)
    {
        if ($value === '') {
            $this->markTestIncomplete();
        }

        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $expected = $value;

        $connection->query('select ?', function ($command, $conn) use ($expected) {
            $this->assertEquals(false, $command->hasError());

            $this->assertCount(1, $command->resultRows);
            $this->assertCount(1, $command->resultRows[0]);
            $this->assertSame($expected, reset($command->resultRows[0]));

            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        }, $value);

        $connection->close();
        $loop->run();
    }

    /**
     * @dataProvider provideValuesThatWillBeConvertedToString
     */
    public function testSelectStaticValueWillBeConvertedToString($value, $expected)
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select ?', function ($command, $conn) use ($expected) {
            $this->assertEquals(false, $command->hasError());

            $this->assertCount(1, $command->resultRows);
            $this->assertCount(1, $command->resultRows[0]);
            $this->assertSame($expected, reset($command->resultRows[0]));

            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        }, $value);

        $connection->close();
        $loop->run();
    }

    public function testSelectStaticTextWithQuestionMark()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select \'hello?\'', function ($command, $conn) {
            $this->assertEquals(false, $command->hasError());

            $this->assertCount(1, $command->resultRows);
            $this->assertCount(1, $command->resultRows[0]);
            $this->assertEquals('hello?', reset($command->resultRows[0]));

            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        });

        $connection->close();
        $loop->run();
    }

    public function testSelectLongStaticTextHasTypeStringWithValidLength()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $length = 40000;

        $connection->query('SELECT ?', function ($command, $conn) use ($length) {
            $this->assertEquals(false, $command->hasError());

            $this->assertCount(1, $command->resultFields);
            $this->assertEquals($length * 3, $command->resultFields[0]['length']);
            $this->assertSame(Constants::FIELD_TYPE_VAR_STRING, $command->resultFields[0]['type']);

            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        }, str_repeat('.', $length));

        $connection->close();
        $loop->run();
    }

    public function testSelectStaticTextWithEmptyLabel()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select \'foo\' as ``', function ($command, $conn) {
            $this->assertEquals(false, $command->hasError());

            $this->assertCount(1, $command->resultRows);
            $this->assertCount(1, $command->resultRows[0]);
            $this->assertSame('foo', reset($command->resultRows[0]));
            $this->assertSame('', key($command->resultRows[0]));

            $this->assertCount(1, $command->resultFields);
            $this->assertSame('', $command->resultFields[0]['name']);

            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        });

        $connection->close();
        $loop->run();
    }

    public function testSelectStaticNullHasTypeNull()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select null', function ($command, $conn) {
            $this->assertEquals(false, $command->hasError());

            $this->assertCount(1, $command->resultRows);
            $this->assertCount(1, $command->resultRows[0]);
            $this->assertNull(reset($command->resultRows[0]));

            $this->assertCount(1, $command->resultFields);
            $this->assertSame(Constants::FIELD_TYPE_NULL, $command->resultFields[0]['type']);

            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        });

        $connection->close();
        $loop->run();
    }

    public function testSelectStaticTextTwoRows()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select "foo" UNION select "bar"', function ($command, $conn) {
            $this->assertEquals(false, $command->hasError());

            $this->assertCount(2, $command->resultRows);
            $this->assertCount(1, $command->resultRows[0]);

            $this->assertSame('foo', reset($command->resultRows[0]));
            $this->assertSame('bar', reset($command->resultRows[1]));

            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        });

        $connection->close();
        $loop->run();
    }

    public function testSelectStaticTextTwoRowsWithNullHasTypeString()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select "foo" UNION select null', function ($command, $conn) {
            $this->assertEquals(false, $command->hasError());

            $this->assertCount(2, $command->resultRows);
            $this->assertCount(1, $command->resultRows[0]);

            $this->assertSame('foo', reset($command->resultRows[0]));
            $this->assertNull(reset($command->resultRows[1]));

            $this->assertCount(1, $command->resultFields);
            $this->assertSame(Constants::FIELD_TYPE_VAR_STRING, $command->resultFields[0]['type']);

            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        });

        $connection->close();
        $loop->run();
    }

    public function testSelectStaticIntegerTwoRowsWithNullHasTypeLongButReturnsIntAsString()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select 0 UNION select null', function ($command, $conn) {
            $this->assertEquals(false, $command->hasError());

            $this->assertCount(2, $command->resultRows);
            $this->assertCount(1, $command->resultRows[0]);

            $this->assertSame('0', reset($command->resultRows[0]));
            $this->assertNull(reset($command->resultRows[1]));

            $this->assertCount(1, $command->resultFields);
            $this->assertSame(Constants::FIELD_TYPE_LONGLONG, $command->resultFields[0]['type']);

            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        });

        $connection->close();
        $loop->run();
    }

    public function testSelectStaticTextTwoRowsWithIntegerHasTypeString()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select "foo" UNION select 1', function ($command, $conn) {
            $this->assertEquals(false, $command->hasError());

            $this->assertCount(2, $command->resultRows);
            $this->assertCount(1, $command->resultRows[0]);

            $this->assertSame('foo', reset($command->resultRows[0]));
            $this->assertSame('1', reset($command->resultRows[1]));

            $this->assertCount(1, $command->resultFields);
            $this->assertSame(Constants::FIELD_TYPE_VAR_STRING, $command->resultFields[0]['type']);

            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        });

        $connection->close();
        $loop->run();
    }

    public function testSelectStaticTextTwoRowsWithEmptyRow()
    {
        $this->markTestIncomplete();

        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select "foo" UNION select ""', function ($command, $conn) {
            $this->assertEquals(false, $command->hasError());

            $this->assertCount(2, $command->resultRows);
            $this->assertCount(1, $command->resultRows[0]);

            $this->assertSame('foo', reset($command->resultRows[0]));
            $this->assertSame('', reset($command->resultRows[1]));

            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        });

        $connection->close();
        $loop->run();
    }

    public function testSelectStaticTextNoRows()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select "foo" LIMIT 0', function ($command, $conn) {
            $this->assertEquals(false, $command->hasError());

            $this->assertCount(0, $command->resultRows);

            $this->assertCount(1, $command->resultFields);
            $this->assertSame('foo', $command->resultFields[0]['name']);

            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        });

        $connection->close();
        $loop->run();
    }

    public function testSelectStaticTextTwoColumns()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select "foo","bar"', function ($command, $conn) {
            $this->assertEquals(false, $command->hasError());

            $this->assertCount(1, $command->resultRows);
            $this->assertCount(2, $command->resultRows[0]);

            $this->assertSame('foo', reset($command->resultRows[0]));
            $this->assertSame('bar', next($command->resultRows[0]));

            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        });

        $connection->close();
        $loop->run();
    }

    public function testSelectStaticTextTwoColumnsWithEmptyColumn()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select "foo",""', function ($command, $conn) {
            $this->assertEquals(false, $command->hasError());

            $this->assertCount(1, $command->resultRows);
            $this->assertCount(2, $command->resultRows[0]);

            $this->assertSame('foo', reset($command->resultRows[0]));
            $this->assertSame('', next($command->resultRows[0]));

            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        });

        $connection->close();
        $loop->run();
    }

    public function testSelectStaticTextTwoColumnsWithSameNameOverwritesValue()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        $connection->query('select "foo" as `col`,"bar" as `col`', function ($command, $conn) {
            $this->assertEquals(false, $command->hasError());

            $this->assertCount(1, $command->resultRows);
            $this->assertCount(1, $command->resultRows[0]);

            $this->assertSame('bar', reset($command->resultRows[0]));

            $this->assertCount(2, $command->resultFields);
            $this->assertSame('col', $command->resultFields[0]['name']);
            $this->assertSame('col', $command->resultFields[1]['name']);

            $this->assertInstanceOf('React\MySQL\Connection', $conn);
        });

        $connection->close();
        $loop->run();
    }

    public function testSimpleSelect()
    {
        $loop = \React\EventLoop\Factory::create();

        $connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
        $connection->connect(function () {});

        // re-create test "book" table
        $connection->query('DROP TABLE IF EXISTS book');
{'fraction_non_alphanumeric': 0.1791958524074789, 'fraction_numerical': 0.007225194418399426, 'mean_word_length': 2.7790746144226763, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 332, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '4496851', 'n_tokens_mistral': 5018, 'n_tokens_neox': 4920, 'n_words': 719}