|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Catalog\Test\Unit\Ui\DataProvider\Product; |
| 7 | + |
| 8 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 9 | +use Magento\Catalog\Ui\DataProvider\Product\ProductCustomOptionsDataProvider; |
| 10 | +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; |
| 11 | +use Magento\Framework\App\RequestInterface; |
| 12 | +use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection; |
| 13 | +use Magento\Framework\DB\Select as DbSelect; |
| 14 | + |
| 15 | +class ProductCustomOptionsDataProviderTest extends \PHPUnit_Framework_TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var ObjectManagerHelper |
| 19 | + */ |
| 20 | + protected $objectManagerHelper; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var ProductCustomOptionsDataProvider |
| 24 | + */ |
| 25 | + protected $dataProvider; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject |
| 29 | + */ |
| 30 | + protected $collectionFactoryMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject |
| 34 | + */ |
| 35 | + protected $requestMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var AbstractCollection|\PHPUnit_Framework_MockObject_MockObject |
| 39 | + */ |
| 40 | + protected $collectionMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var DbSelect|\PHPUnit_Framework_MockObject_MockObject |
| 44 | + */ |
| 45 | + protected $dbSelectMock; |
| 46 | + |
| 47 | + protected function setUp() |
| 48 | + { |
| 49 | + $this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class) |
| 50 | + ->disableOriginalConstructor() |
| 51 | + ->setMethods(['create']) |
| 52 | + ->getMock(); |
| 53 | + $this->requestMock = $this->getMockBuilder(RequestInterface::class) |
| 54 | + ->getMockForAbstractClass(); |
| 55 | + $this->collectionMock = $this->getMockBuilder(AbstractCollection::class) |
| 56 | + ->disableOriginalConstructor() |
| 57 | + ->setMethods(['load', 'getSelect', 'getTable', 'getIterator', 'isLoaded', 'toArray', 'getSize']) |
| 58 | + ->getMockForAbstractClass(); |
| 59 | + $this->dbSelectMock = $this->getMockBuilder(DbSelect::class) |
| 60 | + ->disableOriginalConstructor() |
| 61 | + ->getMock(); |
| 62 | + |
| 63 | + $this->collectionFactoryMock->expects($this->once()) |
| 64 | + ->method('create') |
| 65 | + ->willReturn($this->collectionMock); |
| 66 | + |
| 67 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 68 | + $this->dataProvider = $this->objectManagerHelper->getObject( |
| 69 | + ProductCustomOptionsDataProvider::class, |
| 70 | + [ |
| 71 | + 'collectionFactory' => $this->collectionFactoryMock, |
| 72 | + 'request' => $this->requestMock |
| 73 | + ] |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param int $amount |
| 79 | + * @param array $collectionArray |
| 80 | + * @param array $result |
| 81 | + * @dataProvider getDataDataProvider |
| 82 | + */ |
| 83 | + public function testGetDataCollectionIsLoaded($amount, array $collectionArray, array $result) |
| 84 | + { |
| 85 | + $this->collectionMock->expects($this->never()) |
| 86 | + ->method('load'); |
| 87 | + |
| 88 | + $this->setCommonExpectations(true, $amount, $collectionArray); |
| 89 | + |
| 90 | + $this->assertSame($result, $this->dataProvider->getData()); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param int $amount |
| 95 | + * @param array $collectionArray |
| 96 | + * @param array $result |
| 97 | + * @dataProvider getDataDataProvider |
| 98 | + */ |
| 99 | + public function testGetData($amount, array $collectionArray, array $result) |
| 100 | + { |
| 101 | + $tableName = 'catalog_product_option_table'; |
| 102 | + |
| 103 | + $this->collectionMock->expects($this->once()) |
| 104 | + ->method('isLoaded') |
| 105 | + ->willReturn(false); |
| 106 | + $this->requestMock->expects($this->once()) |
| 107 | + ->method('getParam') |
| 108 | + ->with('current_product_id', null) |
| 109 | + ->willReturn(0); |
| 110 | + $this->collectionMock->expects($this->any()) |
| 111 | + ->method('getSelect') |
| 112 | + ->willReturn($this->dbSelectMock); |
| 113 | + $this->dbSelectMock->expects($this->any()) |
| 114 | + ->method('distinct') |
| 115 | + ->willReturnSelf(); |
| 116 | + $this->collectionMock->expects($this->any()) |
| 117 | + ->method('getTable') |
| 118 | + ->with('catalog_product_option') |
| 119 | + ->willReturn($tableName); |
| 120 | + $this->dbSelectMock->expects($this->once()) |
| 121 | + ->method('join') |
| 122 | + ->with(['opt' => $tableName], 'opt.product_id = e.entity_id', null) |
| 123 | + ->willReturnSelf(); |
| 124 | + $this->collectionMock->expects($this->once()) |
| 125 | + ->method('load') |
| 126 | + ->willReturnSelf(); |
| 127 | + $this->collectionMock->expects($this->any()) |
| 128 | + ->method('getIterator') |
| 129 | + ->willReturn(new \ArrayIterator([])); |
| 130 | + |
| 131 | + $this->setCommonExpectations(false, $amount, $collectionArray); |
| 132 | + |
| 133 | + $this->assertSame($result, $this->dataProvider->getData()); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @return array |
| 138 | + */ |
| 139 | + public function getDataDataProvider() |
| 140 | + { |
| 141 | + return [ |
| 142 | + 0 => [ |
| 143 | + 'amount' => 2, |
| 144 | + 'collectionArray' => [ |
| 145 | + '12' => ['id' => '12', 'value' => 'test1'], |
| 146 | + '25' => ['id' => '25', 'value' => 'test2'] |
| 147 | + ], |
| 148 | + 'result' => [ |
| 149 | + 'totalRecords' => 2, |
| 150 | + 'items' => [ |
| 151 | + ['id' => '12', 'value' => 'test1'], |
| 152 | + ['id' => '25', 'value' => 'test2'] |
| 153 | + ] |
| 154 | + ] |
| 155 | + ] |
| 156 | + ]; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Set common expectations |
| 161 | + * |
| 162 | + * @param bool $isLoaded |
| 163 | + * @param int $amount |
| 164 | + * @param array $collectionArray |
| 165 | + * @return void |
| 166 | + */ |
| 167 | + protected function setCommonExpectations($isLoaded, $amount, array $collectionArray) |
| 168 | + { |
| 169 | + $this->collectionMock->expects($this->once()) |
| 170 | + ->method('isLoaded') |
| 171 | + ->willReturn($isLoaded); |
| 172 | + $this->collectionMock->expects($this->once()) |
| 173 | + ->method('toArray') |
| 174 | + ->willReturn($collectionArray); |
| 175 | + $this->collectionMock->expects($this->once()) |
| 176 | + ->method('getSize') |
| 177 | + ->willReturn($amount); |
| 178 | + } |
| 179 | +} |
0 commit comments