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

namespace Doctrine\Tests\DBAL;

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

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

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

    /**
     * This scenario shows that when the first parameter is not null
     * it properly sets $hasZeroIndex to 1 and calls bindValue starting at 1.
24
     *
25 26 27
     * This also verifies that the statement will check with the connection to
     * see what the current execution mode is.
     *
28 29 30 31 32 33 34
     * 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)
    {
35 36 37 38
        $statement = $this->getMockBuilder('\Doctrine\DBAL\Driver\OCI8\OCI8Statement')
            ->setMethods(array('bindValue', 'errorInfo'))
            ->disableOriginalConstructor()
            ->getMock();
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

        $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])
          );

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

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

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        $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')
            )
        );
    }

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

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