Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TestObjectHandlerTest.php
Go to the documentation of this file.
1 <?php
8 
9 use AspectMock\Test as AspectMock;
10 
11 use Go\Aop\Aspect;
23 
25 {
31  public function testGetTestObject()
32  {
33  // set up mock data
34  $testDataArrayBuilder = new TestDataArrayBuilder();
35  $mockData = $testDataArrayBuilder
36  ->withAnnotations()
37  ->withFailedHook()
38  ->withAfterHook()
39  ->withBeforeHook()
40  ->withTestActions()
41  ->build();
42 
43  $this->setMockParserOutput(['tests' => $mockData]);
44 
45  // run object handler method
47  $actualTestObject = $toh->getObject($testDataArrayBuilder->testName);
48 
49  // perform asserts
50  $expectedBeforeActionObject = new ActionObject(
51  $testDataArrayBuilder->testActionBeforeName,
52  $testDataArrayBuilder->testActionType,
53  []
54  );
55  $expectedAfterActionObject = new ActionObject(
56  $testDataArrayBuilder->testActionAfterName,
57  $testDataArrayBuilder->testActionType,
58  []
59  );
60  $expectedFailedActionObject = new ActionObject(
61  'saveScreenshot',
62  'saveScreenshot',
63  []
64  );
65 
66  $expectedBeforeHookObject = new TestHookObject(
68  $testDataArrayBuilder->testName,
69  ["testActionBefore" => $expectedBeforeActionObject]
70  );
71  $expectedAfterHookObject = new TestHookObject(
73  $testDataArrayBuilder->testName,
74  ["testActionAfter" => $expectedAfterActionObject]
75  );
76  $expectedFailedHookObject = new TestHookObject(
78  $testDataArrayBuilder->testName,
79  [$expectedFailedActionObject]
80  );
81 
82  $expectedTestActionObject = new ActionObject(
83  $testDataArrayBuilder->testTestActionName,
84  $testDataArrayBuilder->testActionType,
85  []
86  );
87  $expectedTestObject = new TestObject(
88  $testDataArrayBuilder->testName,
89  ["testActionInTest" => $expectedTestActionObject],
90  [
91  'features' => ['NO MODULE DETECTED'],
92  'group' => ['test']
93  ],
94  [
95  TestObjectExtractor::TEST_BEFORE_HOOK => $expectedBeforeHookObject,
96  TestObjectExtractor::TEST_AFTER_HOOK => $expectedAfterHookObject,
97  TestObjectExtractor::TEST_FAILED_HOOK => $expectedFailedHookObject
98  ],
99  null
100  );
101 
102  $this->assertEquals($expectedTestObject, $actualTestObject);
103  }
104 
108  public function testGetTestWithFileName()
109  {
110  $this->markTestIncomplete();
111  //TODO
112  }
113 
119  public function testGetTestsByGroup()
120  {
121  // set up mock data with Exclude Test
122  $includeTest = (new TestDataArrayBuilder())
123  ->withName('includeTest')
124  ->withAnnotations(['group' => [['value' => 'test']], 'title'=>[['value' => 'includeTest']]])
125  ->withTestActions()
126  ->build();
127  $excludeTest = (new TestDataArrayBuilder())
128  ->withName('excludeTest')
129  ->withAnnotations(['title'=>[['value' => 'excludeTest']]])
130  ->withTestActions()
131  ->build();
132 
133  $this->setMockParserOutput(['tests' => array_merge($includeTest, $excludeTest)]);
134 
135  // execute test method
137  $tests = $toh->getTestsByGroup('test');
138 
139  // perform asserts
140  $this->assertCount(1, $tests);
141  $this->assertArrayHasKey('includeTest', $tests);
142  $this->assertArrayNotHasKey('excludeTest', $tests);
143  }
144 
150  public function testGetTestWithModuleName()
151  {
152  // set up Test Data
153  $moduleExpected = "SomeTestModule";
154  $filepath = DIRECTORY_SEPARATOR .
155  "user" .
156  "magento2ce" . DIRECTORY_SEPARATOR .
157  "dev" . DIRECTORY_SEPARATOR .
158  "tests" . DIRECTORY_SEPARATOR .
159  "acceptance" . DIRECTORY_SEPARATOR .
160  "tests" . DIRECTORY_SEPARATOR .
161  $moduleExpected . DIRECTORY_SEPARATOR .
162  "Tests" . DIRECTORY_SEPARATOR .
163  "text.xml";
164  // set up mock data
165  $testDataArrayBuilder = new TestDataArrayBuilder();
166  $mockData = $testDataArrayBuilder
167  ->withAnnotations()
168  ->withFailedHook()
169  ->withAfterHook()
170  ->withBeforeHook()
171  ->withTestActions()
172  ->withFileName($filepath)
173  ->build();
174  $this->setMockParserOutput(['tests' => $mockData]);
175  // Execute Test Method
177  $actualTestObject = $toh->getObject($testDataArrayBuilder->testName);
178  $moduleName = $actualTestObject->getAnnotations()["features"][0];
179  //performAsserts
180  $this->assertEquals($moduleExpected, $moduleName);
181  }
182 
189  private function setMockParserOutput($data)
190  {
191  // clear test object handler value to inject parsed content
192  $property = new \ReflectionProperty(TestObjectHandler::class, 'testObjectHandler');
193  $property->setAccessible(true);
194  $property->setValue(null);
195 
196  $mockDataParser = AspectMock::double(TestDataParser::class, ['readTestData' => $data])->make();
197  $instance = AspectMock::double(ObjectManager::class, ['create' => $mockDataParser])
198  ->make(); // bypass the private constructor
199  AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]);
200  }
201 }