Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StatusBaseSelectProcessorTest.php
Go to the documentation of this file.
1 <?php
7 
21 
25 class StatusBaseSelectProcessorTest extends \PHPUnit\Framework\TestCase
26 {
30  private $eavConfig;
31 
35  private $metadataPool;
36 
40  private $storeManager;
41 
45  private $select;
46 
50  private $statusBaseSelectProcessor;
51 
52  protected function setUp()
53  {
54  $this->eavConfig = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
55  $this->metadataPool = $this->getMockBuilder(MetadataPool::class)->disableOriginalConstructor()->getMock();
56  $this->storeManager = $this->getMockBuilder(StoreManagerInterface::class)->getMock();
57  $this->select = $this->getMockBuilder(Select::class)->disableOriginalConstructor()->getMock();
58 
59  $this->statusBaseSelectProcessor = (new ObjectManager($this))->getObject(StatusBaseSelectProcessor::class, [
60  'eavConfig' => $this->eavConfig,
61  'metadataPool' => $this->metadataPool,
62  'storeManager' => $this->storeManager,
63  ]);
64  }
65 
66  public function testProcess()
67  {
68  $linkField = 'link_field';
69  $backendTable = 'backend_table';
70  $attributeId = 2;
71  $currentStoreId = 1;
72 
73  $metadata = $this->createMock(EntityMetadataInterface::class);
74  $metadata->expects($this->once())
75  ->method('getLinkField')
76  ->willReturn($linkField);
77  $this->metadataPool->expects($this->once())
78  ->method('getMetadata')
79  ->with(ProductInterface::class)
80  ->willReturn($metadata);
81 
83  $statusAttribute = $this->getMockBuilder(AttributeInterface::class)
84  ->setMethods(['getBackendTable', 'getAttributeId'])
85  ->getMock();
86  $statusAttribute->expects($this->atLeastOnce())
87  ->method('getBackendTable')
88  ->willReturn($backendTable);
89  $statusAttribute->expects($this->atLeastOnce())
90  ->method('getAttributeId')
91  ->willReturn($attributeId);
92  $this->eavConfig->expects($this->once())
93  ->method('getAttribute')
95  ->willReturn($statusAttribute);
96 
97  $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)->getMock();
98 
99  $this->storeManager->expects($this->once())
100  ->method('getStore')
101  ->willReturn($storeMock);
102 
103  $storeMock->expects($this->once())
104  ->method('getId')
105  ->willReturn($currentStoreId);
106 
107  $this->select->expects($this->at(0))
108  ->method('joinLeft')
109  ->with(
110  ['status_global_attr' => $backendTable],
111  "status_global_attr.{$linkField} = "
113  . " AND status_global_attr.attribute_id = {$attributeId}"
114  . ' AND status_global_attr.store_id = ' . Store::DEFAULT_STORE_ID,
115  []
116  )
117  ->willReturnSelf();
118  $this->select->expects($this->at(1))
119  ->method('joinLeft')
120  ->with(
121  ['status_attr' => $backendTable],
122  "status_attr.{$linkField} = " . BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS . ".{$linkField}"
123  . " AND status_attr.attribute_id = {$attributeId}"
124  . " AND status_attr.store_id = {$currentStoreId}",
125  []
126  )
127  ->willReturnSelf();
128  $this->select->expects($this->at(2))
129  ->method('where')
130  ->with('IFNULL(status_attr.value, status_global_attr.value) = ?', Status::STATUS_ENABLED)
131  ->willReturnSelf();
132 
133  $this->assertEquals($this->select, $this->statusBaseSelectProcessor->process($this->select));
134  }
135 }