|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace Magento\Store\Test\Unit\Model\ResourceModel; |
| 5 | + |
| 6 | +use Magento\Framework\App\ResourceConnection; |
| 7 | +use Magento\Framework\DB\Select; |
| 8 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 9 | +use Magento\Store\Model\ResourceModel\Website; |
| 10 | + |
| 11 | +class WebsiteTest extends \PHPUnit\Framework\TestCase |
| 12 | +{ |
| 13 | + /** @var Website */ |
| 14 | + private $model; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject |
| 18 | + */ |
| 19 | + protected $resourceMock; |
| 20 | + |
| 21 | + /** @var Select | \PHPUnit_Framework_MockObject_MockObject */ |
| 22 | + private $select; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject |
| 26 | + */ |
| 27 | + protected $connectionMock; |
| 28 | + |
| 29 | + public function setUp() |
| 30 | + { |
| 31 | + $objectManagerHelper = new ObjectManager($this); |
| 32 | + $this->select = $this->createMock(\Magento\Framework\DB\Select::class); |
| 33 | + $this->resourceMock = $this->createPartialMock(ResourceConnection::class, [ |
| 34 | + 'getConnection', |
| 35 | + 'getMainTable', |
| 36 | + 'getTable', |
| 37 | + 'getTableName' |
| 38 | + ]); |
| 39 | + $this->connectionMock = $this->createPartialMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class, [ |
| 40 | + 'isTableExists', |
| 41 | + 'select', |
| 42 | + 'fetchAll', |
| 43 | + 'fetchOne', |
| 44 | + 'from', |
| 45 | + 'getCheckSql', |
| 46 | + 'joinLeft', |
| 47 | + 'where' |
| 48 | + ]); |
| 49 | + $contextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class); |
| 50 | + $contextMock->expects($this->once())->method('getResources')->willReturn($this->resourceMock); |
| 51 | + $this->model = $objectManagerHelper->getObject(Website::class, [ |
| 52 | + 'context' => $contextMock |
| 53 | + ]); |
| 54 | + } |
| 55 | + |
| 56 | + public function testReadAllWebsites() |
| 57 | + { |
| 58 | + $data = [ |
| 59 | + "admin" => ["website_id" => "0", "code" => "admin", "name" => "Admin"], |
| 60 | + "base" => ["website_id" => "1", "code" => "base", "name" => "Main Website"] |
| 61 | + ]; |
| 62 | + $mainTable = 'store_website'; |
| 63 | + |
| 64 | + $this->resourceMock->expects($this->once()) |
| 65 | + ->method('getTableName') |
| 66 | + ->willReturn($mainTable); |
| 67 | + |
| 68 | + $this->resourceMock->expects($this->atLeastOnce()) |
| 69 | + ->method('getConnection') |
| 70 | + ->willReturn($this->connectionMock); |
| 71 | + |
| 72 | + $this->connectionMock->expects($this->once()) |
| 73 | + ->method('isTableExists') |
| 74 | + ->with($mainTable) |
| 75 | + ->willReturn(true); |
| 76 | + |
| 77 | + $this->connectionMock->expects($this->once()) |
| 78 | + ->method('select') |
| 79 | + ->willReturn($this->select); |
| 80 | + |
| 81 | + $this->select->expects($this->once()) |
| 82 | + ->method('from') |
| 83 | + ->with($mainTable) |
| 84 | + ->willReturnSelf(); |
| 85 | + |
| 86 | + $this->connectionMock->expects($this->once()) |
| 87 | + ->method('fetchAll') |
| 88 | + ->with($this->select) |
| 89 | + ->willReturn($data); |
| 90 | + |
| 91 | + $this->assertEquals($data, $this->model->readAllWebsites()); |
| 92 | + } |
| 93 | + |
| 94 | + public function testReadAllWebsitesNoDbTable() |
| 95 | + { |
| 96 | + $data = []; |
| 97 | + $mainTable = 'no_store_website_table'; |
| 98 | + |
| 99 | + $this->resourceMock->expects($this->once()) |
| 100 | + ->method('getTableName') |
| 101 | + ->willReturn($mainTable); |
| 102 | + |
| 103 | + $this->resourceMock->expects($this->atLeastOnce()) |
| 104 | + ->method('getConnection') |
| 105 | + ->willReturn($this->connectionMock); |
| 106 | + |
| 107 | + $this->connectionMock->expects($this->once()) |
| 108 | + ->method('isTableExists') |
| 109 | + ->with($mainTable) |
| 110 | + ->willReturn(false); |
| 111 | + |
| 112 | + $this->connectionMock->expects($this->never()) |
| 113 | + ->method('select') |
| 114 | + ->willReturn($this->select); |
| 115 | + |
| 116 | + $this->select->expects($this->never()) |
| 117 | + ->method('from') |
| 118 | + ->with($mainTable) |
| 119 | + ->willReturnSelf(); |
| 120 | + |
| 121 | + $this->connectionMock->expects($this->never()) |
| 122 | + ->method('fetchAll') |
| 123 | + ->with($this->select) |
| 124 | + ->willReturn($data); |
| 125 | + |
| 126 | + $this->assertEquals($data, $this->model->readAllWebsites()); |
| 127 | + } |
| 128 | + |
| 129 | + public function testGetDefaultStoresSelect($includeDefault = false) |
| 130 | + { |
| 131 | + $storeId = 1; |
| 132 | + $storeWebsiteTable = 'store_website'; |
| 133 | + $storeGroupTable = 'store_group'; |
| 134 | + |
| 135 | + $this->resourceMock->expects($this->atLeastOnce()) |
| 136 | + ->method('getConnection') |
| 137 | + ->willReturn($this->connectionMock); |
| 138 | + |
| 139 | + $this->connectionMock->expects($this->once()) |
| 140 | + ->method('getCheckSql') |
| 141 | + ->with( |
| 142 | + 'store_group_table.default_store_id IS NULL', |
| 143 | + '0', |
| 144 | + 'store_group_table.default_store_id' |
| 145 | + ) |
| 146 | + ->willReturn($storeId); |
| 147 | + |
| 148 | + $this->connectionMock->expects($this->once()) |
| 149 | + ->method('select') |
| 150 | + ->willReturn($this->select); |
| 151 | + |
| 152 | + $this->resourceMock->expects($this->atLeastOnce()) |
| 153 | + ->method('getTableName') |
| 154 | + ->withConsecutive([$storeWebsiteTable], [$storeGroupTable]) |
| 155 | + ->willReturnOnConsecutiveCalls($storeWebsiteTable, $storeGroupTable); |
| 156 | + |
| 157 | + $this->select->expects($this->once()) |
| 158 | + ->method('from') |
| 159 | + ->with( |
| 160 | + ['website_table' => $storeWebsiteTable], |
| 161 | + ['website_id'] |
| 162 | + ) |
| 163 | + ->willReturnSelf(); |
| 164 | + |
| 165 | + $this->select->expects($this->once()) |
| 166 | + ->method('joinLeft') |
| 167 | + ->with( |
| 168 | + ['store_group_table' => $storeGroupTable], |
| 169 | + 'website_table.website_id=store_group_table.website_id' . |
| 170 | + ' AND website_table.default_group_id = store_group_table.group_id', |
| 171 | + ['store_id' => $storeId] |
| 172 | + ) |
| 173 | + ->willReturnSelf(); |
| 174 | + |
| 175 | + $this->assertInstanceOf('\Magento\Framework\DB\Select', $this->model->getDefaultStoresSelect($includeDefault)); |
| 176 | + } |
| 177 | + |
| 178 | + public function testCountAll($includeDefault = false) |
| 179 | + { |
| 180 | + $count = 2; |
| 181 | + $mainTable = 'store_website'; |
| 182 | + |
| 183 | + $this->resourceMock->expects($this->once()) |
| 184 | + ->method('getConnection') |
| 185 | + ->willReturn($this->connectionMock); |
| 186 | + |
| 187 | + $this->connectionMock->expects($this->once()) |
| 188 | + ->method('select') |
| 189 | + ->willReturn($this->select); |
| 190 | + |
| 191 | + $this->resourceMock->expects($this->once()) |
| 192 | + ->method('getTableName') |
| 193 | + ->willReturn($mainTable); |
| 194 | + |
| 195 | + $this->select->expects($this->once()) |
| 196 | + ->method('from') |
| 197 | + ->with($mainTable, 'COUNT(*)') |
| 198 | + ->willReturnSelf(); |
| 199 | + |
| 200 | + $this->connectionMock->expects($this->once()) |
| 201 | + ->method('fetchOne') |
| 202 | + ->with($this->select) |
| 203 | + ->willReturn($count); |
| 204 | + |
| 205 | + $this->assertEquals($count, $this->model->countAll($includeDefault)); |
| 206 | + } |
| 207 | +} |
0 commit comments