StatementTest.php 5.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?php

namespace Doctrine\Tests\DBAL\Portability;

use Doctrine\DBAL\Portability\Connection;
use Doctrine\DBAL\Portability\Statement;

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}
     */
28
    protected function setUp()
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    {
        $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';
        $type     = \PDO::PARAM_STR;
        $length   = 666;

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

50
        self::assertTrue($this->stmt->bindParam($column, $variable, $type, $length));
51 52 53 54 55 56 57 58 59 60 61 62 63
    }

    public function testBindValue()
    {
        $param = 'myparam';
        $value = 'myvalue';
        $type  = \PDO::PARAM_STR;

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

64
        self::assertTrue($this->stmt->bindValue($param, $value, $type));
65 66 67 68 69 70 71 72
    }

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

73
        self::assertTrue($this->stmt->closeCursor());
74 75 76 77 78 79 80 81 82 83
    }

    public function testColumnCount()
    {
        $columnCount = 666;

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

84
        self::assertSame($columnCount, $this->stmt->columnCount());
85 86 87 88 89 90 91 92 93 94
    }

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

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

95
        self::assertSame($errorCode, $this->stmt->errorCode());
96 97 98 99 100 101 102 103 104 105
    }

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

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

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

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

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

121
        self::assertTrue($this->stmt->execute($params));
122 123 124 125 126 127 128 129 130 131 132 133 134
    }

    public function testSetFetchMode()
    {
        $fetchMode = \PDO::FETCH_CLASS;
        $arg1      = 'MyClass';
        $arg2      = array(1, 2);

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

135 136 137
        self::assertAttributeSame(\PDO::FETCH_BOTH, 'defaultFetchMode', $this->stmt);
        self::assertTrue($this->stmt->setFetchMode($fetchMode, $arg1, $arg2));
        self::assertAttributeSame($fetchMode, 'defaultFetchMode', $this->stmt);
138 139 140 141 142 143 144 145 146 147 148 149 150
    }

    public function testGetIterator()
    {
        $data = array(
            'foo' => 'bar',
            'bar' => 'foo'
        );

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

151
        self::assertEquals(new \ArrayIterator($data), $this->stmt->getIterator());
152 153 154 155 156 157 158 159 160 161
    }

    public function testRowCount()
    {
        $rowCount = 666;

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

162
        self::assertSame($rowCount, $this->stmt->rowCount());
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 189 190
    }

    /**
     * @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()
    {
191
        return $this->createMock('Doctrine\Tests\Mocks\DriverStatementMock');
192 193
    }
}