Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LockValidatorTest.php
Go to the documentation of this file.
1 <?php
7 
14 
15 class LockValidatorTest extends \PHPUnit\Framework\TestCase
16 {
20  private $model;
21 
25  private $resource;
26 
30  private $connectionMock;
31 
35  private $select;
36 
40  private $metadataPoolMock;
41 
42  protected function setUp()
43  {
44  $helper = new ObjectManager($this);
45 
46  $this->resource = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
47  ->disableOriginalConstructor()
48  ->getMock();
49 
50  $this->connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
51  ->setMethods(['select', 'fetchOne'])
52  ->disableOriginalConstructor()
53  ->getMockForAbstractClass();
54 
55  $this->select = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
56  ->setMethods(['reset', 'from', 'join', 'where', 'group', 'limit'])
57  ->disableOriginalConstructor()
58  ->getMock();
59 
60  $this->metadataPoolMock = $this->getMockBuilder(MetadataPool::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63 
64  $this->metadataPoolMock->expects(self::once())
65  ->method('getMetadata')
66  ->with(ProductInterface::class)
67  ->willReturn($this->getMetaDataMock());
68 
69  $this->model = $helper->getObject(
70  LockValidator::class,
71  [
72  'resource' => $this->resource
73  ]
74  );
75  $refClass = new \ReflectionClass(LockValidator::class);
76  $refProperty = $refClass->getProperty('metadataPool');
77  $refProperty->setAccessible(true);
78  $refProperty->setValue($this->model, $this->metadataPoolMock);
79  }
80 
81  public function testValidate()
82  {
83  $this->validate(false);
84  }
85 
89  private function getMetaDataMock()
90  {
91  $metadata = $this->getMockBuilder(EntityMetadata::class)
92  ->disableOriginalConstructor()
93  ->getMock();
94 
95  $metadata->expects(self::once())
96  ->method('getLinkField')
97  ->willReturn('entity_id');
98 
99  return $metadata;
100  }
101 
106  public function testValidateException()
107  {
108  $this->validate(true);
109  }
110 
115  public function validate($exception)
116  {
117  $attrTable = 'someAttributeTable';
118  $productTable = 'someProductTable';
119  $attributeId = 333;
120  $attributeSet = 'attrSet';
121 
122  $bind = ['attribute_id' => $attributeId, 'attribute_set_id' => $attributeSet];
123 
125  $object = $this->getMockBuilder(\Magento\Framework\Model\AbstractModel::class)
126  ->setMethods(['getAttributeId', '__wakeup'])
127  ->disableOriginalConstructor()
128  ->getMock();
129  $object->expects($this->once())->method('getAttributeId')->will($this->returnValue($attributeId));
130 
131  $this->resource->expects($this->once())->method('getConnection')
132  ->will($this->returnValue($this->connectionMock));
133  $this->resource->expects($this->at(1))->method('getTableName')
134  ->with($this->equalTo('catalog_product_super_attribute'))
135  ->will($this->returnValue($attrTable));
136  $this->resource->expects($this->at(2))->method('getTableName')
137  ->with($this->equalTo('catalog_product_entity'))
138  ->will($this->returnValue($productTable));
139 
140  $this->connectionMock->expects($this->once())->method('select')
141  ->will($this->returnValue($this->select));
142  $this->connectionMock->expects($this->once())->method('fetchOne')
143  ->with($this->equalTo($this->select), $this->equalTo($bind))
144  ->will($this->returnValue($exception));
145 
146  $this->select->expects($this->once())->method('reset')
147  ->will($this->returnValue($this->select));
148  $this->select->expects($this->once())->method('from')
149  ->with(
150  $this->equalTo(['main_table' => $attrTable]),
151  $this->equalTo(['psa_count' => 'COUNT(product_super_attribute_id)'])
152  )
153  ->will($this->returnValue($this->select));
154  $this->select->expects($this->once())->method('join')
155  ->with(
156  $this->equalTo(['entity' => $productTable]),
157  $this->equalTo('main_table.product_id = entity.entity_id')
158  )
159  ->will($this->returnValue($this->select));
160  $this->select->expects($this->any())->method('where')
161  ->will($this->returnValue($this->select));
162  $this->select->expects($this->once())->method('group')
163  ->with($this->equalTo('main_table.attribute_id'))
164  ->will($this->returnValue($this->select));
165  $this->select->expects($this->once())->method('limit')
166  ->with($this->equalTo(1))
167  ->will($this->returnValue($this->select));
168 
169  $this->model->validate($object, $attributeSet);
170  }
171 }
$helper
Definition: iframe.phtml:13