StatementTest.php 5.23 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\Tests\DBAL\Portability;

5 6
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
7 8
use Doctrine\DBAL\Portability\Connection;
use Doctrine\DBAL\Portability\Statement;
9
use function iterator_to_array;
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

class StatementTest extends \Doctrine\Tests\DbalTestCase
{
    /**
     * @var \Doctrine\DBAL\Portability\Connection|\PHPUnit_Framework_MockObject_MockObject
     */
    protected $conn;

    /**
     * @var \Doctrine\DBAL\Portability\Statement
     */
    protected $stmt;

    /**
     * @var \Doctrine\DBAL\Driver\Statement|\PHPUnit_Framework_MockObject_MockObject
     */
    protected $wrappedStmt;

    /**
     * {@inheritdoc}
     */
31
    protected function setUp()
32 33 34 35 36 37 38 39 40 41 42 43 44
    {
        $this->wrappedStmt = $this->createWrappedStatement();
        $this->conn        = $this->createConnection();
        $this->stmt        = $this->createStatement($this->wrappedStmt, $this->conn);
    }

    /**
     * @group DBAL-726
     */
    public function testBindParam()
    {
        $column   = 'mycolumn';
        $variable = 'myvalue';
45
        $type     = ParameterType::STRING;
46 47 48 49 50 51 52
        $length   = 666;

        $this->wrappedStmt->expects($this->once())
            ->method('bindParam')
            ->with($column, $variable, $type, $length)
            ->will($this->returnValue(true));

53
        self::assertTrue($this->stmt->bindParam($column, $variable, $type, $length));
54 55 56 57 58 59
    }

    public function testBindValue()
    {
        $param = 'myparam';
        $value = 'myvalue';
60
        $type  = ParameterType::STRING;
61 62 63 64 65 66

        $this->wrappedStmt->expects($this->once())
            ->method('bindValue')
            ->with($param, $value, $type)
            ->will($this->returnValue(true));

67
        self::assertTrue($this->stmt->bindValue($param, $value, $type));
68 69 70 71 72 73 74 75
    }

    public function testCloseCursor()
    {
        $this->wrappedStmt->expects($this->once())
            ->method('closeCursor')
            ->will($this->returnValue(true));

76
        self::assertTrue($this->stmt->closeCursor());
77 78 79 80 81 82 83 84 85 86
    }

    public function testColumnCount()
    {
        $columnCount = 666;

        $this->wrappedStmt->expects($this->once())
            ->method('columnCount')
            ->will($this->returnValue($columnCount));

87
        self::assertSame($columnCount, $this->stmt->columnCount());
88 89 90 91 92 93 94 95 96 97
    }

    public function testErrorCode()
    {
        $errorCode = '666';

        $this->wrappedStmt->expects($this->once())
            ->method('errorCode')
            ->will($this->returnValue($errorCode));

98
        self::assertSame($errorCode, $this->stmt->errorCode());
99 100 101 102 103 104 105 106 107 108
    }

    public function testErrorInfo()
    {
        $errorInfo = array('666', 'Evil error.');

        $this->wrappedStmt->expects($this->once())
            ->method('errorInfo')
            ->will($this->returnValue($errorInfo));

109
        self::assertSame($errorInfo, $this->stmt->errorInfo());
110 111 112 113 114 115 116 117 118 119 120 121 122 123
    }

    public function testExecute()
    {
        $params = array(
            'foo',
            'bar'
        );

        $this->wrappedStmt->expects($this->once())
            ->method('execute')
            ->with($params)
            ->will($this->returnValue(true));

124
        self::assertTrue($this->stmt->execute($params));
125 126 127 128
    }

    public function testSetFetchMode()
    {
129
        $fetchMode = FetchMode::CUSTOM_OBJECT;
130 131 132 133 134 135 136 137
        $arg1      = 'MyClass';
        $arg2      = array(1, 2);

        $this->wrappedStmt->expects($this->once())
            ->method('setFetchMode')
            ->with($fetchMode, $arg1, $arg2)
            ->will($this->returnValue(true));

138
        self::assertAttributeSame(FetchMode::MIXED, 'defaultFetchMode', $this->stmt);
139 140
        self::assertTrue($this->stmt->setFetchMode($fetchMode, $arg1, $arg2));
        self::assertAttributeSame($fetchMode, 'defaultFetchMode', $this->stmt);
141 142 143 144
    }

    public function testGetIterator()
    {
145 146 147
        $this->wrappedStmt->expects($this->exactly(3))
            ->method('fetch')
            ->willReturnOnConsecutiveCalls('foo', 'bar', false);
148

149
        self::assertSame(['foo', 'bar'], iterator_to_array($this->stmt->getIterator()));
150 151 152 153 154 155 156 157 158 159
    }

    public function testRowCount()
    {
        $rowCount = 666;

        $this->wrappedStmt->expects($this->once())
            ->method('rowCount')
            ->will($this->returnValue($rowCount));

160
        self::assertSame($rowCount, $this->stmt->rowCount());
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    }

    /**
     * @return \Doctrine\DBAL\Portability\Connection|\PHPUnit_Framework_MockObject_MockObject
     */
    protected function createConnection()
    {
        return $this->getMockBuilder('Doctrine\DBAL\Portability\Connection')
            ->disableOriginalConstructor()
            ->getMock();
    }

    /**
     * @param \Doctrine\DBAL\Driver\Statement       $wrappedStatement
     * @param \Doctrine\DBAL\Portability\Connection $connection
     *
     * @return \Doctrine\DBAL\Portability\Statement
     */
    protected function createStatement(\Doctrine\DBAL\Driver\Statement $wrappedStatement, Connection $connection)
    {
        return new Statement($wrappedStatement, $connection);
    }

    /**
     * @return \Doctrine\DBAL\Driver\Statement|\PHPUnit_Framework_MockObject_MockObject
     */
    protected function createWrappedStatement()
    {
189
        return $this->createMock('Doctrine\Tests\Mocks\DriverStatementMock');
190 191
    }
}