<commit_before><?php

use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
use Yajra\Oci8\Connectors\OracleConnector;
use Yajra\Oci8\Oci8Connection;

class ProceduresAndFunctionsTest extends PHPUnit_Framework_TestCase
{
    public function testConnection()
    {
        $this->assertInstanceOf(Oci8Connection::class, $this->createConnection());
    }

    public function testProcedureWithNumbers()
    {
        $connection = $this->createConnection();

        $procedureName = 'demo';

        $command = "
            CREATE OR REPLACE PROCEDURE demo(p1 IN NUMBER, p2 OUT NUMBER) AS
            BEGIN
                p2 := p1 * 2;
            END;
        ";

        $connection->getPdo()->exec($command);

        $input  = 2;
        $output = 0;

        $bindings = [
            'p1' => $input,
            'p2' => &$output,
        ];

        $connection->executeProcedure($procedureName, $bindings);

        //unfortunately we need to cast here.. any better ideas?
        $output = (int)$output;

        $this->assertSame($input * 2, $output);
    }

    public function testProcedureWithStrings()
    {
        $connection = $this->createConnection();

        $procedureName = 'demo';

        $command = "
            CREATE OR REPLACE PROCEDURE demo(p1 IN VARCHAR2, p2 IN VARCHAR2, p3 OUT VARCHAR2) AS
            BEGIN
                p3 := p1 || p2;
            END;
        ";

        $connection->getPdo()->exec($command);

        $first = 'hello';
        $last  = 'world';

        //this needs to be large enough to hold the plsql return value
        $output = str_repeat(' ', 1000);

        $bindings = [
            'p1' => $first,
            'p2' => $last,
            'p3' => &$output,
        ];

        $connection->executeProcedure($procedureName, $bindings);

        $this->assertSame($first . $last, $output);
    }

    public function testRefCursorFromTable()
    {
        $connection = $this->createConnection();

        $procedureName = 'demo';

        $connection->getSchemaBuilder()->dropIfExists('demotable');
        $connection->getSchemaBuilder()->create('demotable', function (Blueprint $table) {
            $table->string('name');
        });

        $rows = [
            [
                'name' => 'Max'
            ],
            [
                'name' => 'John'
            ]
        ];
        $connection->table('demotable')->insert($rows);

        $command = "
            CREATE OR REPLACE PROCEDURE demo(p1 OUT SYS_REFCURSOR) AS
            BEGIN
                OPEN p1 
                FOR 
                SELECT name
                FROM demotable; 
            END;
        ";

        $connection->getPdo()->exec($command);

        $result = $connection->executeProcedureWithCursor($procedureName);

        $this->assertSame($rows, $result);
    }


    public function testFunctionWithNumbers()
    {
        $connection = $this->createConnection();

        $procedureName = 'add_two';<commit_msg>fix test for object return<commit_after><?php

use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
use Yajra\Oci8\Connectors\OracleConnector;
use Yajra\Oci8\Oci8Connection;

class ProceduresAndFunctionsTest extends PHPUnit_Framework_TestCase
{
    public function testConnection()
    {
        $this->assertInstanceOf(Oci8Connection::class, $this->createConnection());
    }

    public function testProcedureWithNumbers()
    {
        $connection = $this->createConnection();

        $procedureName = 'demo';

        $command = "
            CREATE OR REPLACE PROCEDURE demo(p1 IN NUMBER, p2 OUT NUMBER) AS
            BEGIN
                p2 := p1 * 2;
            END;
        ";

        $connection->getPdo()->exec($command);

        $input  = 2;
        $output = 0;

        $bindings = [
            'p1' => $input,
            'p2' => &$output,
        ];

        $connection->executeProcedure($procedureName, $bindings);

        //unfortunately we need to cast here.. any better ideas?
        $output = (int)$output;

        $this->assertSame($input * 2, $output);
    }

    public function testProcedureWithStrings()
    {
        $connection = $this->createConnection();

        $procedureName = 'demo';

        $command = "
            CREATE OR REPLACE PROCEDURE demo(p1 IN VARCHAR2, p2 IN VARCHAR2, p3 OUT VARCHAR2) AS
            BEGIN
                p3 := p1 || p2;
            END;
        ";

        $connection->getPdo()->exec($command);

        $first = 'hello';
        $last  = 'world';

        //this needs to be large enough to hold the plsql return value
        $output = str_repeat(' ', 1000);

        $bindings = [
            'p1' => $first,
            'p2' => $last,
            'p3' => &$output,
        ];

        $connection->executeProcedure($procedureName, $bindings);

        $this->assertSame($first . $last, $output);
    }

    public function testRefCursorFromTable()
    {
        $connection = $this->createConnection();

        $procedureName = 'demo';

        $connection->getSchemaBuilder()->dropIfExists('demotable');
        $connection->getSchemaBuilder()->create('demotable', function (Blueprint $table) {
            $table->string('name');
        });

        $rows = [
            [
                'name' => 'Max'
            ],
            [
                'name' => 'John'
            ]
        ];
        $connection->table('demotable')->insert($rows);

        $command = "
            CREATE OR REPLACE PROCEDURE demo(p1 OUT SYS_REFCURSOR) AS
            BEGIN
                OPEN p1 
                FOR 
                SELECT name
                FROM demotable; 
            END;
        ";

        $connection->getPdo()->exec($command);

        $result = $connection->executeProcedureWithCursor($procedureName);

        $this->assertSame($rows[0]['name'], $result[0]->name);
        $this->assertSame($rows[1]['name'], $result[1]->name);
    }


    public function testFunctionWithNumbers()
    {
        $connection = $this->createConnection();

        $procedureName = 'add_two';
{'fraction_non_alphanumeric': 0.1301302931596091, 'fraction_numerical': 0.011074918566775244, 'mean_word_length': 1.853624535315985, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 70, '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': '3863259', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1693, 'n_words': 361}