1
+ <?php
2
+
3
+ namespace Magento \AcceptanceTestFramework \DataGenerator \Api ;
4
+
5
+ use Magento \AcceptanceTestFramework \DataGenerator \Managers \DataManager ;
6
+ use Magento \AcceptanceTestFramework \DataGenerator \Managers \JsonDefinitionManager ;
7
+ use Magento \AcceptanceTestFramework \DataGenerator \Objects \EntityDataObject ;
8
+ use Magento \AcceptanceTestFramework \Util \ApiClientUtil ;
9
+
10
+ class ApiExecutor
11
+ {
12
+ private $ operation ;
13
+ private $ entityObject ;
14
+ private $ jsonDefinition ;
15
+
16
+ private $ primitives = ['string ' , 'boolean ' , 'integer ' , 'double ' , 'array ' ];
17
+
18
+ /**
19
+ * ApiSubObject constructor.
20
+ * @param string $operation
21
+ * @param EntityDataObject $entityObject
22
+ */
23
+ public function __construct ($ operation , $ entityObject )
24
+ {
25
+ $ this ->operation = $ operation ;
26
+ $ this ->entityObject = $ entityObject ;
27
+
28
+ $ this ->jsonDefinition = JsonDefinitionManager::getInstance ()->getJsonDefinition (
29
+ $ this ->operation ,
30
+ $ this ->entityObject ->getType ()
31
+ );
32
+ }
33
+
34
+ public function executeRequest ()
35
+ {
36
+ $ apiClientUrl = $ this ->jsonDefinition ->getApiUrl ();
37
+
38
+ $ matchedParams = [];
39
+ preg_match_all ("/[{](.+?)[}]/ " , $ apiClientUrl , $ matchedParams );
40
+
41
+ if (!empty ($ matchedParams )) {
42
+ foreach ($ matchedParams [0 ] as $ paramKey => $ paramValue ) {
43
+ $ param = $ this ->entityObject ->getDataByName ($ matchedParams [1 ][$ paramKey ]);
44
+ $ apiClientUrl = str_replace ($ paramValue , $ param , $ apiClientUrl );
45
+ }
46
+ }
47
+
48
+ $ authorization = $ this ->jsonDefinition ->getAuth ();
49
+ $ headers = $ this ->jsonDefinition ->getHeaders ();
50
+
51
+ if ($ authorization ) {
52
+ $ headers [] = $ this ->getAuthorizationHeader ($ authorization );
53
+ }
54
+
55
+ $ jsonBody = $ this ->getEncodedJsonString ();
56
+
57
+ $ apiClientUtil = new ApiClientUtil (
58
+ $ apiClientUrl ,
59
+ $ headers ,
60
+ $ this ->jsonDefinition ->getApiMethod (),
61
+ empty ($ jsonBody ) ? null : $ jsonBody
62
+ );
63
+
64
+ return $ apiClientUtil ->submit ();
65
+ }
66
+
67
+ /**
68
+ * Returns the authorization token needed for some requests via REST call.
69
+ *
70
+ * @param string $authUrl
71
+ * @return string
72
+ */
73
+ private function getAuthorizationHeader ($ authUrl )
74
+ {
75
+ $ headers = ['Content-Type: application/json ' ];
76
+ $ authCreds = [
77
+ 'username ' => getenv ('MAGENTO_ADMIN_USERNAME ' ),
78
+ 'password ' => getenv ('MAGENTO_ADMIN_PASSWORD ' )
79
+ ];
80
+
81
+ $ apiClientUtil = new ApiClientUtil ($ authUrl , $ headers , 'POST ' , json_encode ($ authCreds ));
82
+ $ token = $ apiClientUtil ->submit ();
83
+ $ authHeader = 'Authorization: Bearer ' . str_replace ('" ' , "" , $ token );
84
+
85
+ return $ authHeader ;
86
+ }
87
+
88
+ /**
89
+ * This function returns an array which is structurally equal to the json which is needed by the web api for
90
+ * entity creation. The function retrieves an array describing the json metadata and traverses any dependencies
91
+ * recursively forming an array which represents the json structure for the api of the desired type.
92
+ *
93
+ * @param EntityDataObject $entityObject
94
+ * @param Array $jsonDefMetadata
95
+ *
96
+ * @return array
97
+ */
98
+ private function getJsonDataArray ($ entityObject , $ jsonDefMetadata = null )
99
+ {
100
+ $ jsonArrayMetadata = !$ jsonDefMetadata ? JsonDefinitionManager::getInstance ()->getJsonDefinition (
101
+ $ this ->operation ,
102
+ $ entityObject ->getType ()
103
+ )->getJsonMetadata () : $ jsonDefMetadata ;
104
+
105
+ $ jsonArray = array ();
106
+
107
+ foreach ($ jsonArrayMetadata as $ jsonElement ) {
108
+ $ jsonElementType = $ jsonElement ->getValue ();
109
+
110
+ if (in_array ($ jsonElementType , $ this ->primitives )) {
111
+ $ jsonArray [$ jsonElement ->getKey ()] = $ this ->castValue (
112
+ $ jsonElementType ,
113
+ $ entityObject ->getDataByName ($ jsonElement ->getKey ())
114
+ );
115
+ } else {
116
+ $ entityNamesOfType = $ entityObject ->getLinkedEntitiesOfType ($ jsonElementType );
117
+
118
+ foreach ($ entityNamesOfType as $ entityName ) {
119
+ $ linkedEntityObj = DataManager::getInstance ()->getEntity ($ entityName );
120
+ $ jsonDataSubArray = self ::getJsonDataArray ($ linkedEntityObj );
121
+
122
+ if ($ jsonElement ->getType () == 'array ' ) {
123
+ $ jsonArray [$ jsonElement ->getKey ()][] = $ jsonDataSubArray ;
124
+ } else {
125
+ $ jsonArray [$ jsonElement ->getKey ()] = $ jsonDataSubArray ;
126
+ }
127
+ }
128
+ }
129
+ }
130
+
131
+ return $ jsonArray ;
132
+ }
133
+
134
+ private function getEncodedJsonString ()
135
+ {
136
+ $ jsonArray = $ this ->getJsonDataArray ($ this ->entityObject , $ this ->jsonDefinition ->getJsonMetadata ());
137
+
138
+ return json_encode ([$ this ->entityObject ->getType () => $ jsonArray ], JSON_PRETTY_PRINT );
139
+ }
140
+
141
+ private function castValue ($ type , $ value )
142
+ {
143
+ $ newVal = $ value ;
144
+
145
+ switch ($ type ) {
146
+ case 'string ' :
147
+ break ;
148
+ case 'integer ' :
149
+ $ newVal = (integer )$ value ;
150
+ break ;
151
+ case 'boolean ' :
152
+ $ newVal = (boolean )$ value ;
153
+ break ;
154
+ case 'double ' :
155
+ $ newVal = (double )$ value ;
156
+ break ;
157
+ }
158
+
159
+ return $ newVal ;
160
+ }
161
+ }
0 commit comments