OCI8StatementTest.php 3.54 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\Tests\DBAL;

5 6 7 8 9
use Doctrine\DBAL\Driver\OCI8\OCI8Exception;
use Doctrine\DBAL\Driver\OCI8\OCI8Statement;
use Doctrine\Tests\DbalTestCase;

class OCI8StatementTest extends DbalTestCase
10
{
11
    protected function setUp()
12 13 14 15
    {
        if (!extension_loaded('oci8')) {
            $this->markTestSkipped('oci8 is not installed.');
        }
16

17 18
        parent::setUp();
    }
19 20 21 22

    /**
     * This scenario shows that when the first parameter is not null
     * it properly sets $hasZeroIndex to 1 and calls bindValue starting at 1.
23
     *
24 25 26
     * This also verifies that the statement will check with the connection to
     * see what the current execution mode is.
     *
27 28 29 30 31 32 33
     * The expected exception is due to oci_execute failing due to no valid connection.
     *
     * @dataProvider executeDataProvider
     * @expectedException \Doctrine\DBAL\Driver\OCI8\OCI8Exception
     */
    public function testExecute(array $params)
    {
34 35 36 37
        $statement = $this->getMockBuilder('\Doctrine\DBAL\Driver\OCI8\OCI8Statement')
            ->setMethods(array('bindValue', 'errorInfo'))
            ->disableOriginalConstructor()
            ->getMock();
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

        $statement->expects($this->at(0))
            ->method('bindValue')
            ->with(
                $this->equalTo(1),
                $this->equalTo($params[0])
            );
        $statement->expects($this->at(1))
            ->method('bindValue')
            ->with(
                $this->equalTo(2),
                $this->equalTo($params[1])
            );
        $statement->expects($this->at(2))
            ->method('bindValue')
            ->with(
                $this->equalTo(3),
                $this->equalTo($params[2])
          );

58 59
        // can't pass to constructor since we don't have a real database handle,
        // but execute must check the connection for the executeMode
60 61 62 63
        $conn = $this->getMockBuilder('\Doctrine\DBAL\Driver\OCI8\OCI8Connection')
            ->setMethods(array('getExecuteMode'))
            ->disableOriginalConstructor()
            ->getMock();
64 65 66 67 68 69 70
        $conn->expects($this->once())
            ->method('getExecuteMode');

        $reflProperty = new \ReflectionProperty($statement, '_conn');
        $reflProperty->setAccessible(true);
        $reflProperty->setValue($statement, $conn);

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        $statement->execute($params);
    }

    public static function executeDataProvider()
    {
        return array(
            // $hasZeroIndex = isset($params[0]); == true
            array(
                array(0 => 'test', 1 => null, 2 => 'value')
            ),
            // $hasZeroIndex = isset($params[0]); == false
            array(
                array(0 => null, 1 => 'test', 2 => 'value')
            )
        );
    }

88
    /**
89
     * @dataProvider nonTerminatedLiteralProvider
90
     */
91
    public function testConvertNonTerminatedLiteral($sql, $message)
92
    {
93 94 95
        $this->expectException(OCI8Exception::class);
        $this->expectExceptionMessageRegExp($message);
        OCI8Statement::convertPositionalToNamedPlaceholders($sql);
96 97
    }

98
    public static function nonTerminatedLiteralProvider()
99 100
    {
        return array(
101 102 103
            'no-matching-quote' => array(
                "SELECT 'literal FROM DUAL",
                '/offset 7/',
104
            ),
105 106 107
            'no-matching-double-quote' => array(
                'SELECT 1 "COL1 FROM DUAL',
                '/offset 9/',
108
            ),
109 110 111
            'incorrect-escaping-syntax' => array(
                "SELECT 'quoted \\'string' FROM DUAL",
                '/offset 23/',
Sergei Morozov's avatar
Sergei Morozov committed
112
            ),
113 114
        );
    }
115
}