|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Framework\DB\Test\Unit\DB\Statement; |
| 8 | + |
| 9 | +use Magento\Framework\DB\Statement\Parameter; |
| 10 | +use Magento\Framework\DB\Statement\Pdo\Mysql; |
| 11 | +use PHPUnit\Framework\MockObject\MockObject; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +/** |
| 15 | + * @inheritdoc |
| 16 | + */ |
| 17 | +class MysqlTest extends TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var \Zend_Db_Adapter_Abstract|MockObject |
| 21 | + */ |
| 22 | + private $adapterMock; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var \PDO|MockObject |
| 26 | + */ |
| 27 | + private $pdoMock; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var \Zend_Db_Profiler|MockObject |
| 31 | + */ |
| 32 | + private $zendDbProfilerMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var \PDOStatement|MockObject |
| 36 | + */ |
| 37 | + private $pdoStatementMock; |
| 38 | + |
| 39 | + /** |
| 40 | + * @inheritdoc |
| 41 | + */ |
| 42 | + public function setUp() |
| 43 | + { |
| 44 | + $this->adapterMock = $this->getMockForAbstractClass( |
| 45 | + \Zend_Db_Adapter_Abstract::class, |
| 46 | + [], |
| 47 | + '', |
| 48 | + false, |
| 49 | + true, |
| 50 | + true, |
| 51 | + ['getConnection', 'getProfiler'] |
| 52 | + ); |
| 53 | + $this->pdoMock = $this->createMock(\PDO::class); |
| 54 | + $this->adapterMock->expects($this->once()) |
| 55 | + ->method('getConnection') |
| 56 | + ->willReturn($this->pdoMock); |
| 57 | + $this->zendDbProfilerMock = $this->createMock(\Zend_Db_Profiler::class); |
| 58 | + $this->adapterMock->expects($this->once()) |
| 59 | + ->method('getProfiler') |
| 60 | + ->willReturn($this->zendDbProfilerMock); |
| 61 | + $this->pdoStatementMock = $this->createMock(\PDOStatement::class); |
| 62 | + } |
| 63 | + |
| 64 | + public function testExecuteWithoutParams() |
| 65 | + { |
| 66 | + $query = 'SET @a=1;'; |
| 67 | + $this->pdoMock->expects($this->once()) |
| 68 | + ->method('prepare') |
| 69 | + ->with($query) |
| 70 | + ->willReturn($this->pdoStatementMock); |
| 71 | + $this->pdoStatementMock->expects($this->once()) |
| 72 | + ->method('execute'); |
| 73 | + (new Mysql($this->adapterMock, $query))->_execute(); |
| 74 | + } |
| 75 | + |
| 76 | + public function testExecuteWhenThrowPDOException() |
| 77 | + { |
| 78 | + $this->expectException(\Zend_Db_Statement_Exception::class); |
| 79 | + $this->expectExceptionMessage('test message, query was:'); |
| 80 | + $errorReporting = error_reporting(); |
| 81 | + $query = 'SET @a=1;'; |
| 82 | + $this->pdoMock->expects($this->once()) |
| 83 | + ->method('prepare') |
| 84 | + ->with($query) |
| 85 | + ->willReturn($this->pdoStatementMock); |
| 86 | + $this->pdoStatementMock->expects($this->once()) |
| 87 | + ->method('execute') |
| 88 | + ->willThrowException(new \PDOException('test message')); |
| 89 | + |
| 90 | + $this->assertEquals($errorReporting, error_reporting(), 'Error report level was\'t restored'); |
| 91 | + |
| 92 | + (new Mysql($this->adapterMock, $query))->_execute(); |
| 93 | + } |
| 94 | + |
| 95 | + public function testExecuteWhenParamsAsPrimitives() |
| 96 | + { |
| 97 | + $params = [':param1' => 'value1', ':param2' => 'value2']; |
| 98 | + $query = 'UPDATE `some_table1` SET `col1`=\'val1\' WHERE `param1`=\':param1\' AND `param2`=\':param2\';'; |
| 99 | + $this->pdoMock->expects($this->once()) |
| 100 | + ->method('prepare') |
| 101 | + ->with($query) |
| 102 | + ->willReturn($this->pdoStatementMock); |
| 103 | + $this->pdoStatementMock->expects($this->never()) |
| 104 | + ->method('bindParam'); |
| 105 | + $this->pdoStatementMock->expects($this->once()) |
| 106 | + ->method('execute') |
| 107 | + ->with($params); |
| 108 | + |
| 109 | + (new Mysql($this->adapterMock, $query))->_execute($params); |
| 110 | + } |
| 111 | + |
| 112 | + public function testExecuteWhenParamsAsParameterObject() |
| 113 | + { |
| 114 | + $param1 = $this->createMock(Parameter::class); |
| 115 | + $param1Value = 'SomeValue'; |
| 116 | + $param1DataType = 'dataType'; |
| 117 | + $param1Length = '9'; |
| 118 | + $param1DriverOptions = 'some driver options'; |
| 119 | + $param1->expects($this->once()) |
| 120 | + ->method('getIsBlob') |
| 121 | + ->willReturn(false); |
| 122 | + $param1->expects($this->once()) |
| 123 | + ->method('getDataType') |
| 124 | + ->willReturn($param1DataType); |
| 125 | + $param1->expects($this->once()) |
| 126 | + ->method('getLength') |
| 127 | + ->willReturn($param1Length); |
| 128 | + $param1->expects($this->once()) |
| 129 | + ->method('getDriverOptions') |
| 130 | + ->willReturn($param1DriverOptions); |
| 131 | + $param1->expects($this->once()) |
| 132 | + ->method('getValue') |
| 133 | + ->willReturn($param1Value); |
| 134 | + $params = [ |
| 135 | + ':param1' => $param1, |
| 136 | + ':param2' => 'value2', |
| 137 | + ]; |
| 138 | + $query = 'UPDATE `some_table1` SET `col1`=\'val1\' WHERE `param1`=\':param1\' AND `param2`=\':param2\';'; |
| 139 | + $this->pdoMock->expects($this->once()) |
| 140 | + ->method('prepare') |
| 141 | + ->with($query) |
| 142 | + ->willReturn($this->pdoStatementMock); |
| 143 | + $this->pdoStatementMock->expects($this->exactly(2)) |
| 144 | + ->method('bindParam') |
| 145 | + ->withConsecutive( |
| 146 | + [':param1', $param1Value, $param1DataType, $param1Length, $param1DriverOptions], |
| 147 | + [':param2', 'value2', \PDO::PARAM_STR, null, null] |
| 148 | + ); |
| 149 | + $this->pdoStatementMock->expects($this->once()) |
| 150 | + ->method('execute'); |
| 151 | + |
| 152 | + (new Mysql($this->adapterMock, $query))->_execute($params); |
| 153 | + } |
| 154 | +} |
0 commit comments