|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Backup\Test\Unit\Model; |
| 9 | + |
| 10 | +use Magento\Backup\Model\Db; |
| 11 | +use Magento\Backup\Model\ResourceModel\Db as DbResource; |
| 12 | +use Magento\Framework\App\ResourceConnection; |
| 13 | +use Magento\Framework\Backup\Db\BackupInterface; |
| 14 | +use Magento\Framework\DataObject; |
| 15 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 16 | + |
| 17 | +class DbTest extends \PHPUnit\Framework\TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var ObjectManager |
| 21 | + */ |
| 22 | + private $objectManager; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var Db |
| 26 | + */ |
| 27 | + private $dbModel; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var DbResource|\PHPUnit_Framework_MockObject_MockObject |
| 31 | + */ |
| 32 | + private $dbResourceMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject |
| 36 | + */ |
| 37 | + private $connectionResourceMock; |
| 38 | + |
| 39 | + protected function setUp() |
| 40 | + { |
| 41 | + $this->dbResourceMock = $this->getMockBuilder(DbResource::class) |
| 42 | + ->disableOriginalConstructor() |
| 43 | + ->getMock(); |
| 44 | + $this->connectionResourceMock = $this->getMockBuilder(ResourceConnection::class) |
| 45 | + ->disableOriginalConstructor() |
| 46 | + ->getMock(); |
| 47 | + |
| 48 | + $this->objectManager = new ObjectManager($this); |
| 49 | + $this->dbModel = $this->objectManager->getObject( |
| 50 | + Db::class, |
| 51 | + [ |
| 52 | + 'resourceDb' => $this->dbResourceMock, |
| 53 | + 'resource' => $this->connectionResourceMock |
| 54 | + ] |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + public function testGetResource() |
| 59 | + { |
| 60 | + self::assertEquals($this->dbResourceMock, $this->dbModel->getResource()); |
| 61 | + } |
| 62 | + |
| 63 | + public function testGetTables() |
| 64 | + { |
| 65 | + $tables = []; |
| 66 | + $this->dbResourceMock->expects($this->once()) |
| 67 | + ->method('getTables') |
| 68 | + ->willReturn($tables); |
| 69 | + |
| 70 | + self::assertEquals($tables, $this->dbModel->getTables()); |
| 71 | + } |
| 72 | + |
| 73 | + public function testGetTableCreateScript() |
| 74 | + { |
| 75 | + $tableName = 'some_table'; |
| 76 | + $script = 'script'; |
| 77 | + $this->dbResourceMock->expects($this->once()) |
| 78 | + ->method('getTableCreateScript') |
| 79 | + ->with($tableName, false) |
| 80 | + ->willReturn($script); |
| 81 | + |
| 82 | + self::assertEquals($script, $this->dbModel->getTableCreateScript($tableName, false)); |
| 83 | + } |
| 84 | + |
| 85 | + public function testGetTableDataDump() |
| 86 | + { |
| 87 | + $tableName = 'some_table'; |
| 88 | + $dump = 'dump'; |
| 89 | + $this->dbResourceMock->expects($this->once()) |
| 90 | + ->method('getTableDataDump') |
| 91 | + ->with($tableName) |
| 92 | + ->willReturn($dump); |
| 93 | + |
| 94 | + self::assertEquals($dump, $this->dbModel->getTableDataDump($tableName)); |
| 95 | + } |
| 96 | + |
| 97 | + public function testGetHeader() |
| 98 | + { |
| 99 | + $header = 'header'; |
| 100 | + $this->dbResourceMock->expects($this->once()) |
| 101 | + ->method('getHeader') |
| 102 | + ->willReturn($header); |
| 103 | + |
| 104 | + self::assertEquals($header, $this->dbModel->getHeader()); |
| 105 | + } |
| 106 | + |
| 107 | + public function testGetFooter() |
| 108 | + { |
| 109 | + $footer = 'footer'; |
| 110 | + $this->dbResourceMock->expects($this->once()) |
| 111 | + ->method('getFooter') |
| 112 | + ->willReturn($footer); |
| 113 | + |
| 114 | + self::assertEquals($footer, $this->dbModel->getFooter()); |
| 115 | + } |
| 116 | + |
| 117 | + public function testRenderSql() |
| 118 | + { |
| 119 | + $header = 'header'; |
| 120 | + $script = 'script'; |
| 121 | + $tableName = 'some_table'; |
| 122 | + $tables = [$tableName, $tableName]; |
| 123 | + $dump = 'dump'; |
| 124 | + $footer = 'footer'; |
| 125 | + |
| 126 | + $this->dbResourceMock->expects($this->once()) |
| 127 | + ->method('getTables') |
| 128 | + ->willReturn($tables); |
| 129 | + $this->dbResourceMock->expects($this->once()) |
| 130 | + ->method('getHeader') |
| 131 | + ->willReturn($header); |
| 132 | + $this->dbResourceMock->expects($this->exactly(2)) |
| 133 | + ->method('getTableCreateScript') |
| 134 | + ->with($tableName, true) |
| 135 | + ->willReturn($script); |
| 136 | + $this->dbResourceMock->expects($this->exactly(2)) |
| 137 | + ->method('getTableDataDump') |
| 138 | + ->with($tableName) |
| 139 | + ->willReturn($dump); |
| 140 | + $this->dbResourceMock->expects($this->once()) |
| 141 | + ->method('getFooter') |
| 142 | + ->willReturn($footer); |
| 143 | + |
| 144 | + self::assertEquals( |
| 145 | + $header . $script . $dump . $script . $dump . $footer, |
| 146 | + $this->dbModel->renderSql() |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + public function testCreateBackup() |
| 151 | + { |
| 152 | + /** @var BackupInterface|\PHPUnit_Framework_MockObject_MockObject $backupMock */ |
| 153 | + $backupMock = $this->getMockBuilder(BackupInterface::class)->getMock(); |
| 154 | + /** @var DataObject $tableStatus */ |
| 155 | + $tableStatus = new DataObject(); |
| 156 | + |
| 157 | + $tableName = 'some_table'; |
| 158 | + $tables = [$tableName]; |
| 159 | + $header = 'header'; |
| 160 | + $footer = 'footer'; |
| 161 | + $dropSql = 'drop_sql'; |
| 162 | + $createSql = 'create_sql'; |
| 163 | + $beforeSql = 'before_sql'; |
| 164 | + $afterSql = 'after_sql'; |
| 165 | + $dataSql = 'data_sql'; |
| 166 | + $foreignKeysSql = 'foreign_keys'; |
| 167 | + $triggersSql = 'triggers_sql'; |
| 168 | + $rowsCount = 2; |
| 169 | + $dataLength = 1; |
| 170 | + |
| 171 | + $this->dbResourceMock->expects($this->once()) |
| 172 | + ->method('beginTransaction'); |
| 173 | + $this->dbResourceMock->expects($this->once()) |
| 174 | + ->method('commitTransaction'); |
| 175 | + $this->dbResourceMock->expects($this->once()) |
| 176 | + ->method('getTables') |
| 177 | + ->willReturn($tables); |
| 178 | + $this->dbResourceMock->expects($this->once()) |
| 179 | + ->method('getTableDropSql') |
| 180 | + ->willReturn($dropSql); |
| 181 | + $this->dbResourceMock->expects($this->once()) |
| 182 | + ->method('getTableCreateSql') |
| 183 | + ->with($tableName, false) |
| 184 | + ->willReturn($createSql); |
| 185 | + $this->dbResourceMock->expects($this->once()) |
| 186 | + ->method('getTableDataBeforeSql') |
| 187 | + ->with($tableName) |
| 188 | + ->willReturn($beforeSql); |
| 189 | + $this->dbResourceMock->expects($this->once()) |
| 190 | + ->method('getTableDataAfterSql') |
| 191 | + ->with($tableName) |
| 192 | + ->willReturn($afterSql); |
| 193 | + $this->dbResourceMock->expects($this->once()) |
| 194 | + ->method('getTableDataSql') |
| 195 | + ->with($tableName, $rowsCount, 0) |
| 196 | + ->willReturn($dataSql); |
| 197 | + $this->dbResourceMock->expects($this->once()) |
| 198 | + ->method('getTableStatus') |
| 199 | + ->with($tableName) |
| 200 | + ->willReturn($tableStatus); |
| 201 | + $this->dbResourceMock->expects($this->once()) |
| 202 | + ->method('getTables') |
| 203 | + ->willReturn($createSql); |
| 204 | + $this->dbResourceMock->expects($this->once()) |
| 205 | + ->method('getHeader') |
| 206 | + ->willReturn($header); |
| 207 | + $this->dbResourceMock->expects($this->once()) |
| 208 | + ->method('getTableHeader') |
| 209 | + ->willReturn($header); |
| 210 | + $this->dbResourceMock->expects($this->once()) |
| 211 | + ->method('getFooter') |
| 212 | + ->willReturn($footer); |
| 213 | + $this->dbResourceMock->expects($this->once()) |
| 214 | + ->method('getTableForeignKeysSql') |
| 215 | + ->willReturn($foreignKeysSql); |
| 216 | + $this->dbResourceMock->expects($this->once()) |
| 217 | + ->method('getTableTriggersSql') |
| 218 | + ->willReturn($triggersSql); |
| 219 | + $backupMock->expects($this->once()) |
| 220 | + ->method('open'); |
| 221 | + $backupMock->expects($this->once()) |
| 222 | + ->method('close'); |
| 223 | + |
| 224 | + $tableStatus->setRows($rowsCount); |
| 225 | + $tableStatus->setDataLength($dataLength); |
| 226 | + |
| 227 | + $backupMock->expects($this->any()) |
| 228 | + ->method('write') |
| 229 | + ->withConsecutive( |
| 230 | + [$this->equalTo($header)], |
| 231 | + [$this->equalTo($header . $dropSql . "\n")], |
| 232 | + [$this->equalTo($createSql . "\n")], |
| 233 | + [$this->equalTo($beforeSql)], |
| 234 | + [$this->equalTo($dataSql)], |
| 235 | + [$this->equalTo($afterSql)], |
| 236 | + [$this->equalTo($foreignKeysSql)], |
| 237 | + [$this->equalTo($triggersSql)], |
| 238 | + [$this->equalTo($footer)] |
| 239 | + ); |
| 240 | + |
| 241 | + $this->dbModel->createBackup($backupMock); |
| 242 | + } |
| 243 | +} |
0 commit comments