Skip to content

Commit 47d74fe

Browse files
committed
MQE-545: [Unit Test] OperationDefinitionObjectHandler.php
- Major rework, split 1 test into 4 for different data scenarios
1 parent 84fada8 commit 47d74fe

File tree

1 file changed

+222
-90
lines changed

1 file changed

+222
-90
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/OperationDefinitionObjectHandlerTest.php

Lines changed: 222 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,73 @@
2020
*/
2121
class OperationDefinitionObjectHandlerTest extends TestCase
2222
{
23-
public function testGetAllObjects()
23+
public function testGetMultipleObjects()
2424
{
25-
// Define data variables
26-
$testDataTypeName1 = "operationDataTypeName";
27-
$testDataTypeName2 = "operationDataTypeName2";
28-
$testAuth = "adminOAuth";
29-
$testUrl = "V1/object";
25+
// Data Variables for Assertions
26+
$dataType1 = "type1";
27+
$operationType1 = "create";
28+
$operationType2 = "update";
29+
30+
/**
31+
* Parser Output. Just two simple pieces of metadata with 1 field each
32+
* operationName
33+
* createType1
34+
* has field
35+
* key=id, value=integer
36+
* updateType1
37+
* has field
38+
* key=id, value=integer
39+
*/
40+
$mockData = [OperationDefinitionObjectHandler::ENTITY_OPERATION_ROOT_TAG => [
41+
"testOperationName" => [
42+
OperationDefinitionObjectHandler::ENTITY_OPERATION_DATA_TYPE => $dataType1,
43+
OperationDefinitionObjectHandler::ENTITY_OPERATION_TYPE => $operationType1,
44+
OperationDefinitionObjectHandler::ENTITY_OPERATION_AUTH => "auth",
45+
OperationDefinitionObjectHandler::ENTITY_OPERATION_URL => "V1/Type1",
46+
OperationDefinitionObjectHandler::ENTITY_OPERATION_METHOD => "POST",
47+
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY => [
48+
0 => [
49+
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_KEY => "id",
50+
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_VALUE => "integer"
51+
],
52+
]
53+
],[
54+
OperationDefinitionObjectHandler::ENTITY_OPERATION_DATA_TYPE => $dataType1,
55+
OperationDefinitionObjectHandler::ENTITY_OPERATION_TYPE => $operationType2,
56+
OperationDefinitionObjectHandler::ENTITY_OPERATION_AUTH => "auth",
57+
OperationDefinitionObjectHandler::ENTITY_OPERATION_URL => "V1/Type1/{id}",
58+
OperationDefinitionObjectHandler::ENTITY_OPERATION_METHOD => "PUT",
59+
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY => [
60+
0 => [
61+
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_KEY => "id",
62+
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_VALUE => "integer"
63+
],
64+
]
65+
]]];
66+
$this->setMockParserOutput($mockData);
67+
68+
//Perform Assertions
69+
$operationDefinitionManager = OperationDefinitionObjectHandler::getInstance();
70+
$operations = $operationDefinitionManager->getAllObjects();
71+
$this->assertArrayHasKey($operationType1 . $dataType1, $operations);
72+
$this->assertArrayHasKey($operationType2 . $dataType1, $operations);
73+
74+
}
75+
76+
public function testObjectCreation()
77+
{
78+
// Data Variables for Assertions
79+
$testDataTypeName1 = "type1";
80+
$testAuth = "auth";
81+
$testUrl = "V1/dataType";
3082
$testOperationType = "create";
3183
$testMethod = "POST";
3284
$testSuccessRegex = "/messages-message-success/";
3385
$testContentType = "application/json";
3486
$testHeaderParam = "testParameter";
3587
$testHeaderValue = "testHeader";
36-
// Nested data variables
37-
$nestedObjectKey = "object";
88+
// Nested Object variables
89+
$nestedObjectKey = "objectKey";
3890
$nestedObjectType = "objectType";
3991
$nestedEntryKey1 = "id";
4092
$nestedEntryValue1 = "integer";
@@ -43,15 +95,23 @@ public function testGetAllObjects()
4395
$nestedEntryRequired2 = "true";
4496
$nestedEntryKey3 = "active";
4597
$nestedEntryValue3 = "boolean";
46-
// Two Level nested data variables
47-
$objectArrayKey = "ObjectArray";
48-
$twiceNestedObjectKey = "nestedObjectKey";
49-
$twiceNestedObjectType = "nestedObjectType";
50-
$twiceNestedEntryKey = "nestedFieldKey";
51-
$twiceNestedEntryValue = "string";
52-
53-
// Operation Metadata. BE CAREFUL WHEN EDITING, Objects defined below rely on values in this array.
5498

99+
/**
100+
* Complex Object
101+
* testOperation
102+
* createType1
103+
* has contentType
104+
* has headers
105+
* has URL
106+
* has successRegex
107+
* has nested object
108+
* key nestedKey type nestedType
109+
* has 3 fields
110+
* key id, value integer
111+
* key name, value string, required TRUE
112+
* key active, value boolean
113+
*
114+
*/
55115
$mockData = [OperationDefinitionObjectHandler::ENTITY_OPERATION_ROOT_TAG => [
56116
"testOperationName" => [
57117
OperationDefinitionObjectHandler::ENTITY_OPERATION_DATA_TYPE => $testDataTypeName1,
@@ -98,6 +158,79 @@ public function testGetAllObjects()
98158
]
99159
]
100160
],
161+
]]];
162+
// Prepare objects to compare against
163+
$field = OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY;
164+
$expectedNestedField = new OperationElement($nestedEntryKey1, $nestedEntryValue1, $field, false, [], null);
165+
$expectedNestedField2 = new OperationElement(
166+
$nestedEntryKey2,
167+
$nestedEntryValue2,
168+
$field,
169+
$nestedEntryRequired2,
170+
[],
171+
null
172+
);
173+
$expectedNestedField3 = new OperationElement($nestedEntryKey3, $nestedEntryValue3, $field, false, [], null);
174+
$expectedOperation = new OperationElement(
175+
$nestedObjectKey,
176+
$nestedObjectType,
177+
OperationDefinitionObjectHandler::ENTITY_OPERATION_OBJECT,
178+
false,
179+
[],
180+
[0 => $expectedNestedField, 1 => $expectedNestedField2, 2 =>$expectedNestedField3]
181+
);
182+
183+
// Set up mocked data output
184+
$this->setMockParserOutput($mockData);
185+
186+
// Get Operation
187+
$operationDefinitionManager = OperationDefinitionObjectHandler::getInstance();
188+
$operation = $operationDefinitionManager->getOperationDefinition($testOperationType, $testDataTypeName1);
189+
190+
// Perform Asserts
191+
$this->assertEquals(
192+
[0 => "{$testHeaderParam}: {$testHeaderValue}",
193+
1 => OperationDefinitionObject::HTTP_CONTENT_TYPE_HEADER . ": {$testContentType}"],
194+
$operation->getHeaders()
195+
);
196+
$this->assertEquals($testOperationType, $operation->getOperation());
197+
$this->assertEquals($testMethod, $operation->getApiMethod());
198+
$this->assertEquals($testUrl, $operation->getApiUrl());
199+
$this->assertEquals($testDataTypeName1, $operation->getDataType());
200+
$this->assertEquals($testContentType, $operation->getContentType());
201+
$this->assertEquals($testAuth, $operation->getAuth());
202+
$this->assertEquals($testSuccessRegex, $operation->getSuccessRegex());
203+
204+
// perform asserts on the instantiated metadata in the $createOperationByName
205+
$this->assertEquals($expectedOperation, $operation->getOperationMetadata()[0]);
206+
}
207+
208+
public function testObjectArrayCreation()
209+
{
210+
// Data Variables for Assertions
211+
$dataType1 = "type1";
212+
$operationType1 = "create";
213+
$objectArrayKey = "ObjectArray";
214+
$twiceNestedObjectKey = "nestedObjectKey";
215+
$twiceNestedObjectType = "nestedObjectType";
216+
$twiceNestedEntryKey = "nestedFieldKey";
217+
$twiceNestedEntryValue = "string";
218+
// Parser Output
219+
/**
220+
* Metadata with nested array of objects, with a single field
221+
* OperationName
222+
* createType1
223+
* has array with key ObjectArray
224+
* objects with key = nestedObjectKey, type = nestedObjectType
225+
* has field with key = nestedFieldKey, value = string
226+
*/
227+
$mockData = [OperationDefinitionObjectHandler::ENTITY_OPERATION_ROOT_TAG => [
228+
"testOperationName" => [
229+
OperationDefinitionObjectHandler::ENTITY_OPERATION_DATA_TYPE => $dataType1,
230+
OperationDefinitionObjectHandler::ENTITY_OPERATION_TYPE => $operationType1,
231+
OperationDefinitionObjectHandler::ENTITY_OPERATION_AUTH => "auth",
232+
OperationDefinitionObjectHandler::ENTITY_OPERATION_URL => "V1/Type1",
233+
OperationDefinitionObjectHandler::ENTITY_OPERATION_METHOD => "POST",
101234
OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY => [
102235
0 => [
103236
OperationDefinitionObjectHandler::ENTITY_OPERATION_OBJECT_KEY => $objectArrayKey,
@@ -116,56 +249,8 @@ public function testGetAllObjects()
116249
]
117250
]
118251
]
119-
]
120-
],
121-
"testOperationName2" => [
122-
OperationDefinitionObjectHandler::ENTITY_OPERATION_DATA_TYPE => $testDataTypeName2,
123-
OperationDefinitionObjectHandler::ENTITY_OPERATION_TYPE => $testOperationType,
124-
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY => [
125-
0 => [
126-
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_KEY => "id",
127-
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_VALUE => "integer"
128-
],
129-
1 => [
130-
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_KEY => "name",
131-
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_VALUE => "string"
132-
]
133-
],
134-
OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY => [
135-
0 => [
136-
OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_KEY => "arrayKey",
137-
OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE => [
138-
0 => [
139-
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_VALUE => "string"
140-
]
141-
]
142-
]
143-
]
144-
]
145-
]];
146-
147-
//prepare OperationElements to compare against.
148-
$field = OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY;
149-
150-
$expectedNestedMetadata1 = new OperationElement($nestedEntryKey1, $nestedEntryValue1, $field, false, [], null);
151-
$expectedNestedMetadata2 = new OperationElement(
152-
$nestedEntryKey2,
153-
$nestedEntryValue2,
154-
$field,
155-
$nestedEntryRequired2,
156-
[],
157-
null
158-
);
159-
$expectedNestedMetadata3 = new OperationElement($nestedEntryKey3, $nestedEntryValue3, $field, false, [], null);
160-
$expectedOperation1 = new OperationElement(
161-
$nestedObjectKey,
162-
$nestedObjectType,
163-
OperationDefinitionObjectHandler::ENTITY_OPERATION_OBJECT,
164-
false,
165-
[],
166-
[0 => $expectedNestedMetadata1, 1 => $expectedNestedMetadata2, 2 =>$expectedNestedMetadata3]
167-
);
168-
252+
]]]];
253+
// Prepare Objects to compare against
169254
$twoLevelNestedMetadata = new OperationElement(
170255
$twiceNestedEntryKey,
171256
$twiceNestedEntryValue,
@@ -184,7 +269,7 @@ public function testGetAllObjects()
184269
[0 => $twoLevelNestedMetadata]
185270
);
186271

187-
$expectedOperation2 = new OperationElement(
272+
$expectedOperation = new OperationElement(
188273
$objectArrayKey,
189274
$twiceNestedObjectType,
190275
$twiceNestedObjectKey,
@@ -196,34 +281,81 @@ public function testGetAllObjects()
196281
// Set up mocked data output
197282
$this->setMockParserOutput($mockData);
198283

199-
// get Operations
284+
// Get Operation
200285
$operationDefinitionManager = OperationDefinitionObjectHandler::getInstance();
201-
$operations = $operationDefinitionManager->getAllObjects();
202-
$operationByName = $operationDefinitionManager->getOperationDefinition($testOperationType, $testDataTypeName1);
286+
$operation = $operationDefinitionManager->getOperationDefinition($operationType1, $dataType1);
287+
// Make Assertions
203288

204-
// perform asserts on $operations
205-
$this->assertCount(2, $operations);
206-
$this->assertArrayHasKey($testOperationType . $testDataTypeName1, $operations);
207-
$this->assertArrayHasKey($testOperationType . $testDataTypeName2, $operations);
289+
$this->assertEquals($expectedOperation, $operation->getOperationMetadata()[0]);
290+
}
208291

209-
// perform asserts on $createOperationByName
210-
$this->assertEquals(
211-
[0 => "{$testHeaderParam}: {$testHeaderValue}",
212-
1 => OperationDefinitionObject::HTTP_CONTENT_TYPE_HEADER . ": {$testContentType}"],
213-
$operationByName->getHeaders()
292+
public function testLooseJsonCreation()
293+
{
294+
// Data Variables for Assertions
295+
$dataType = "dataType";
296+
$operationType = "create";
297+
$entryKey = "id";
298+
$entryValue = "integer";
299+
$arrayKey = "arrayKey";
300+
$arrayValue = "string";
301+
/**
302+
* Operation with no objects, just an entry and an array of strings
303+
* testOperationName
304+
* createDataType
305+
* has entry key = id, value = integer
306+
* has array key = arrayKey
307+
* fields of value = string
308+
*/
309+
$mockData = [OperationDefinitionObjectHandler::ENTITY_OPERATION_ROOT_TAG => [
310+
"testOperationName" => [
311+
OperationDefinitionObjectHandler::ENTITY_OPERATION_DATA_TYPE => $dataType,
312+
OperationDefinitionObjectHandler::ENTITY_OPERATION_TYPE => $operationType,
313+
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY => [
314+
0 => [
315+
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_KEY => $entryKey,
316+
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_VALUE => $entryValue
317+
]
318+
],
319+
OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY => [
320+
0 => [
321+
OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_KEY => $arrayKey,
322+
OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY_VALUE => [
323+
0 => [
324+
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_VALUE => $arrayValue
325+
]
326+
]
327+
]
328+
]
329+
]
330+
]];
331+
// Prepare Objects to assert against
332+
$entry = new OperationElement(
333+
$entryKey,
334+
$entryValue,
335+
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY,
336+
false,
337+
[],
338+
null
339+
);
340+
$array = new OperationElement(
341+
$arrayKey,
342+
$arrayValue,
343+
OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY,
344+
false,
345+
[],
346+
null
214347
);
215-
$this->assertEquals($testOperationType, $operationByName->getOperation());
216-
$this->assertEquals($testMethod, $operationByName->getApiMethod());
217-
$this->assertEquals($testUrl, $operationByName->getApiUrl());
218-
$this->assertEquals($testDataTypeName1, $operationByName->getDataType());
219-
$this->assertEquals($testContentType, $operationByName->getContentType());
220-
$this->assertEquals($testAuth, $operationByName->getAuth());
221-
$this->assertEquals($testSuccessRegex, $operationByName->getSuccessRegex());
222348

223-
// perform asserts on the instantiated metadata in the $createOperationByName
224-
$this->assertEquals($expectedOperation1, $operationByName->getOperationMetadata()[0]);
225-
$this->assertEquals($expectedOperation2, $operationByName->getOperationMetadata()[1]);
349+
// Set up mocked data output
350+
$this->setMockParserOutput($mockData);
351+
352+
// get Operations
353+
$operationDefinitionManager = OperationDefinitionObjectHandler::getInstance();
354+
$operation = $operationDefinitionManager->getOperationDefinition($operationType, $dataType);
226355

356+
// Perform Assertions
357+
$this->assertEquals($entry, $operation->getOperationMetadata()[0]);
358+
$this->assertEquals($array, $operation->getOperationMetadata()[1]);
227359
}
228360

229361
/**
@@ -233,7 +365,7 @@ public function testGetAllObjects()
233365
*/
234366
private function setMockParserOutput($data)
235367
{
236-
// clear section object handler value to inject parsed content
368+
// clear Operation object handler value to inject parsed content
237369
$property = new \ReflectionProperty(
238370
OperationDefinitionObjectHandler::class,
239371
'DATA_DEFINITION_OBJECT_HANDLER'

0 commit comments

Comments
 (0)