|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\GroupedProduct\Test\Unit\Ui\DataProvider\Product; |
| 7 | + |
| 8 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 9 | +use Magento\GroupedProduct\Ui\DataProvider\Product\GroupedProductDataProvider; |
| 10 | +use Magento\Framework\App\RequestInterface; |
| 11 | +use Magento\Catalog\Model\ResourceModel\Product\Collection; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; |
| 13 | +use Magento\Catalog\Model\ProductTypes\ConfigInterface; |
| 14 | + |
| 15 | +class GroupedProductDataProviderTest extends \PHPUnit_Framework_TestCase |
| 16 | +{ |
| 17 | + const ALLOWED_TYPE = 'simple'; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var ObjectManager |
| 21 | + */ |
| 22 | + protected $objectManager; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject |
| 26 | + */ |
| 27 | + protected $requestMock; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject |
| 31 | + */ |
| 32 | + protected $collectionFactoryMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var Collection|\PHPUnit_Framework_MockObject_MockObject |
| 36 | + */ |
| 37 | + protected $collectionMock; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var ConfigInterface|\PHPUnit_Framework_MockObject_MockObject |
| 41 | + */ |
| 42 | + protected $configMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @return void |
| 46 | + */ |
| 47 | + protected function setUp() |
| 48 | + { |
| 49 | + $this->objectManager = new ObjectManager($this); |
| 50 | + |
| 51 | + $this->requestMock = $this->getMockBuilder(RequestInterface::class) |
| 52 | + ->getMockForAbstractClass(); |
| 53 | + $this->collectionMock = $this->getMockBuilder(Collection::class) |
| 54 | + ->disableOriginalConstructor() |
| 55 | + ->setMethods( |
| 56 | + [ |
| 57 | + 'toArray', |
| 58 | + 'isLoaded', |
| 59 | + 'addAttributeToFilter', |
| 60 | + 'load', |
| 61 | + 'getSize', |
| 62 | + 'addFilterByRequiredOptions', |
| 63 | + 'addStoreFilter' |
| 64 | + ] |
| 65 | + )->getMock(); |
| 66 | + $this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class) |
| 67 | + ->disableOriginalConstructor() |
| 68 | + ->setMethods(['create']) |
| 69 | + ->getMock(); |
| 70 | + $this->collectionFactoryMock->expects($this->any()) |
| 71 | + ->method('create') |
| 72 | + ->willReturn($this->collectionMock); |
| 73 | + $this->configMock = $this->getMockBuilder(ConfigInterface::class) |
| 74 | + ->setMethods(['getComposableTypes']) |
| 75 | + ->getMockForAbstractClass(); |
| 76 | + } |
| 77 | + |
| 78 | + protected function getModel() |
| 79 | + { |
| 80 | + return $this->objectManager->getObject(GroupedProductDataProvider::class, [ |
| 81 | + 'name' => 'testName', |
| 82 | + 'primaryFieldName' => 'testPrimaryFieldName', |
| 83 | + 'requestFieldName' => 'testRequestFieldName', |
| 84 | + 'collectionFactory' => $this->collectionFactoryMock, |
| 85 | + 'request' => $this->requestMock, |
| 86 | + 'config' => $this->configMock, |
| 87 | + 'addFieldStrategies' => [], |
| 88 | + 'addFilterStrategies' => [], |
| 89 | + 'meta' => [], |
| 90 | + 'data' => [], |
| 91 | + ]); |
| 92 | + } |
| 93 | + |
| 94 | + public function testGetData() |
| 95 | + { |
| 96 | + $items = ['testProduct1', 'testProduct2']; |
| 97 | + $expectedData = [ |
| 98 | + 'totalRecords' => count($items), |
| 99 | + 'items' => $items, |
| 100 | + ]; |
| 101 | + |
| 102 | + $this->configMock->expects($this->once()) |
| 103 | + ->method('getComposableTypes') |
| 104 | + ->willReturn([self::ALLOWED_TYPE]); |
| 105 | + $this->collectionMock->expects($this->once()) |
| 106 | + ->method('isLoaded') |
| 107 | + ->willReturn(false); |
| 108 | + $this->collectionMock->expects($this->once()) |
| 109 | + ->method('addAttributeToFilter') |
| 110 | + ->with('type_id', [self::ALLOWED_TYPE]); |
| 111 | + $this->collectionMock->expects($this->once()) |
| 112 | + ->method('toArray') |
| 113 | + ->willReturn($items); |
| 114 | + $this->collectionMock->expects($this->once()) |
| 115 | + ->method('getSize') |
| 116 | + ->willReturn(count($items)); |
| 117 | + |
| 118 | + $this->assertEquals($expectedData, $this->getModel()->getData()); |
| 119 | + } |
| 120 | + |
| 121 | + public function testGetCollection() |
| 122 | + { |
| 123 | + $this->assertInstanceOf(Collection::class, $this->getModel()->getCollection()); |
| 124 | + } |
| 125 | +} |
0 commit comments