7
7
8
8
use Magento \Catalog \Api \Data \ProductInterface ;
9
9
use Magento \Catalog \Api \ProductRepositoryInterface ;
10
+ use Magento \Eav \Model \Entity \Attribute as EavAttribute ;
10
11
use Magento \Framework \EntityManager \MetadataPool ;
11
12
12
13
/**
@@ -26,6 +27,11 @@ class AttributeTest extends \PHPUnit\Framework\TestCase
26
27
*/
27
28
protected $ productResource ;
28
29
30
+ /**
31
+ * @var ProductRepositoryInterface
32
+ */
33
+ private $ productRepository ;
34
+
29
35
/**
30
36
* @var MetadataPool
31
37
*/
@@ -45,6 +51,7 @@ protected function setUp()
45
51
$ this ->productResource = $ this ->objectManager ->get (
46
52
\Magento \Catalog \Model \ResourceModel \Product::class
47
53
);
54
+ $ this ->productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
48
55
$ this ->metadataPool = $ this ->objectManager ->get (MetadataPool::class);
49
56
}
50
57
@@ -55,9 +62,10 @@ protected function setUp()
55
62
*/
56
63
public function testDeleteEntity ()
57
64
{
58
- /* @var \Magento\Eav\Model\Entity\Attribute $attribute */
59
- $ attribute = $ this ->objectManager ->get (\ Magento \ Eav \ Model \ Entity \Attribute ::class);
65
+ /* @var EavAttribute $attribute */
66
+ $ attribute = $ this ->objectManager ->get (EavAttribute ::class);
60
67
$ attribute ->loadByCode (\Magento \Catalog \Model \Product::ENTITY , 'text_attribute ' );
68
+ $ product = $ this ->productRepository ->get ('simple ' );
61
69
62
70
$ entityEavAttributeRow = $ this ->getEavEntityAttributeRow (
63
71
$ attribute ->getEntityTypeId (),
@@ -70,9 +78,8 @@ public function testDeleteEntity()
70
78
);
71
79
72
80
$ entityAttributeValues = $ this ->getProductAttributeValues (
73
- $ attribute ->getId (),
74
- 1 ,
75
- 'catalog_product_entity_text '
81
+ $ attribute ,
82
+ $ product
76
83
);
77
84
$ this ->assertNotEmpty (
78
85
$ entityAttributeValues ,
@@ -93,9 +100,8 @@ public function testDeleteEntity()
93
100
);
94
101
95
102
$ entityAttributeValues = $ this ->getProductAttributeValues (
96
- $ attribute ->getId (),
97
- 1 ,
98
- 'catalog_product_entity_text '
103
+ $ attribute ,
104
+ $ product
99
105
);
100
106
$ this ->assertEmpty (
101
107
$ entityAttributeValues ,
@@ -126,19 +132,21 @@ private function getEavEntityAttributeRow($entityTypeId, $attributeSetId, $attri
126
132
/**
127
133
* Retrieve product attribute values.
128
134
*
129
- * @param int $attributeId
130
- * @param int $productId
131
- * @param string $table
135
+ * @param EavAttribute $attribute
136
+ * @param ProductInterface $product
132
137
* @return array
133
138
*/
134
- private function getProductAttributeValues ($ attributeId , $ productId , $ table )
139
+ private function getProductAttributeValues ($ attribute , $ product )
135
140
{
141
+ $ backendTable = $ attribute ->getBackend ()->getTable ();
136
142
$ linkField = $ this ->metadataPool ->getMetadata (ProductInterface::class)->getLinkField ();
143
+ $ linkFieldValue = $ product ->getData ($ linkField );
144
+
137
145
$ connection = $ this ->productResource ->getConnection ();
138
146
$ select = $ connection ->select ()
139
- ->from ($ this ->productResource ->getTable ($ table ))
140
- ->where ('attribute_id=? ' , $ attributeId )
141
- ->where ($ linkField . '=? ' , $ productId );
147
+ ->from ($ this ->productResource ->getTable ($ backendTable ))
148
+ ->where ('attribute_id=? ' , $ attribute -> getId () )
149
+ ->where ($ linkField . '=? ' , $ linkFieldValue );
142
150
143
151
return $ connection ->fetchAll ($ select );
144
152
}
0 commit comments