Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductDataProviderTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
14 
18 class ProductDataProviderTest extends \PHPUnit\Framework\TestCase
19 {
23  protected $objectManager;
24 
29 
33  protected $collectionMock;
34 
38  protected $modifierMockOne;
39 
43  protected $poolMock;
44 
48  protected $model;
49 
50  protected function setUp()
51  {
52  $this->objectManager = new ObjectManager($this);
53  $this->collectionMock = $this->getMockBuilder(Collection::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56  $this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)
57  ->disableOriginalConstructor()
58  ->setMethods(['create'])
59  ->getMock();
60  $this->collectionFactoryMock->expects($this->once())
61  ->method('create')
62  ->willReturn($this->collectionMock);
63  $this->poolMock = $this->getMockBuilder(Pool::class)
64  ->disableOriginalConstructor()
65  ->getMock();
66  $this->modifierMockOne = $this->getMockBuilder(ModifierInterface::class)
67  ->setMethods(['getData', 'getMeta'])
68  ->getMockForAbstractClass();
69 
70  $this->model = $this->objectManager->getObject(ProductDataProvider::class, [
71  'name' => 'testName',
72  'primaryFieldName' => 'testPrimaryFieldName',
73  'requestFieldName' => 'testRequestFieldName',
74  'collectionFactory' => $this->collectionFactoryMock,
75  'pool' => $this->poolMock,
76  ]);
77  }
78 
79  public function testGetMeta()
80  {
81  $expectedMeta = ['meta_key' => 'meta_value'];
82 
83  $this->poolMock->expects($this->once())
84  ->method('getModifiersInstances')
85  ->willReturn([$this->modifierMockOne]);
86  $this->modifierMockOne->expects($this->once())
87  ->method('modifyMeta')
88  ->willReturn($expectedMeta);
89 
90  $this->assertSame($expectedMeta, $this->model->getMeta());
91  }
92 
93  public function testGetData()
94  {
95  $expectedMeta = ['data_key' => 'data_value'];
96 
97  $this->poolMock->expects($this->once())
98  ->method('getModifiersInstances')
99  ->willReturn([$this->modifierMockOne]);
100  $this->modifierMockOne->expects($this->once())
101  ->method('modifyData')
102  ->willReturn($expectedMeta);
103 
104  $this->assertSame($expectedMeta, $this->model->getData());
105  }
106 }