Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductCustomOptionsDataProviderTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
14 
15 class ProductCustomOptionsDataProviderTest extends \PHPUnit\Framework\TestCase
16 {
21 
25  protected $dataProvider;
26 
31 
35  protected $requestMock;
36 
40  protected $collectionMock;
41 
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 
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 
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 
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 
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 }
$tableName
Definition: trigger.php:13
$amount
Definition: order.php:14