Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BundleDataProviderTest.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
14 
15 class BundleDataProviderTest extends \PHPUnit\Framework\TestCase
16 {
17  const ALLOWED_TYPE = 'simple';
18 
22  protected $objectManager;
23 
27  protected $requestMock;
28 
33 
37  protected $collectionMock;
38 
42  protected $dataHelperMock;
43 
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->dataHelperMock = $this->getMockBuilder(Data::class)
74  ->disableOriginalConstructor()
75  ->setMethods(['getAllowedSelectionTypes'])
76  ->getMock();
77  }
78 
82  protected function getModel()
83  {
84  return $this->objectManager->getObject(BundleDataProvider::class, [
85  'name' => 'testName',
86  'primaryFieldName' => 'testPrimaryFieldName',
87  'requestFieldName' => 'testRequestFieldName',
88  'collectionFactory' => $this->collectionFactoryMock,
89  'request' => $this->requestMock,
90  'dataHelper' => $this->dataHelperMock,
91  'addFieldStrategies' => [],
92  'addFilterStrategies' => [],
93  'meta' => [],
94  'data' => [],
95  ]);
96  }
97 
98  public function testGetData()
99  {
100  $items = ['testProduct1', 'testProduct2'];
101  $expectedData = [
102  'totalRecords' => count($items),
103  'items' => $items,
104  ];
105 
106  $this->dataHelperMock->expects($this->once())
107  ->method('getAllowedSelectionTypes')
108  ->willReturn([self::ALLOWED_TYPE]);
109  $this->collectionMock->expects($this->once())
110  ->method('isLoaded')
111  ->willReturn(false);
112  $this->collectionMock->expects($this->once())
113  ->method('addAttributeToFilter')
114  ->with('type_id', [self::ALLOWED_TYPE]);
115  $this->collectionMock->expects($this->once())
116  ->method('addFilterByRequiredOptions');
117  $this->collectionMock->expects($this->once())
118  ->method('addStoreFilter')
119  ->with(\Magento\Store\Model\Store::DEFAULT_STORE_ID);
120  $this->collectionMock->expects($this->once())
121  ->method('toArray')
122  ->willReturn($items);
123  $this->collectionMock->expects($this->once())
124  ->method('getSize')
125  ->willReturn(count($items));
126 
127  $this->assertEquals($expectedData, $this->getModel()->getData());
128  }
129 
130  public function testGetCollection()
131  {
132  $this->assertInstanceOf(Collection::class, $this->getModel()->getCollection());
133  }
134 }
$items