|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Tests\unit\Magento\FunctionalTestingFramework\Console; |
| 7 | + |
| 8 | +use AspectMock\Test as AspectMock; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Magento\FunctionalTestingFramework\Console\BaseGenerateCommand; |
| 11 | +use Magento\FunctionalTestingFramework\Suite\Objects\SuiteObject; |
| 12 | +use Magento\FunctionalTestingFramework\Suite\Handlers\SuiteObjectHandler; |
| 13 | +use Magento\FunctionalTestingFramework\Test\Objects\TestObject; |
| 14 | +use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler; |
| 15 | + |
| 16 | +class BaseGenerateCommandTest extends TestCase |
| 17 | +{ |
| 18 | + public function tearDown() |
| 19 | + { |
| 20 | + AspectMock::clean(); |
| 21 | + } |
| 22 | + |
| 23 | + public function testOneTestOneSuiteConfig() |
| 24 | + { |
| 25 | + $testOne = new TestObject('Test1', [], [], []); |
| 26 | + $suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne], [], []); |
| 27 | + |
| 28 | + $testArray = ['Test1' => $testOne]; |
| 29 | + $suiteArray = ['Suite1' => $suiteOne]; |
| 30 | + |
| 31 | + $this->mockHandlers($testArray, $suiteArray); |
| 32 | + |
| 33 | + $actual = json_decode($this->callTestConfig(['Test1']), true); |
| 34 | + $expected = ['tests' => null, 'suites' => ['Suite1' => ['Test1']]]; |
| 35 | + $this->assertEquals($expected, $actual); |
| 36 | + } |
| 37 | + |
| 38 | + public function testOneTestTwoSuitesConfig() |
| 39 | + { |
| 40 | + $testOne = new TestObject('Test1', [], [], []); |
| 41 | + $suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne], [], []); |
| 42 | + $suiteTwo = new SuiteObject('Suite2', ['Test1' => $testOne], [], []); |
| 43 | + |
| 44 | + $testArray = ['Test1' => $testOne]; |
| 45 | + $suiteArray = ['Suite1' => $suiteOne, 'Suite2' => $suiteTwo]; |
| 46 | + |
| 47 | + $this->mockHandlers($testArray, $suiteArray); |
| 48 | + |
| 49 | + $actual = json_decode($this->callTestConfig(['Test1']), true); |
| 50 | + $expected = ['tests' => null, 'suites' => ['Suite1' => ['Test1'], 'Suite2' => ['Test1']]]; |
| 51 | + $this->assertEquals($expected, $actual); |
| 52 | + } |
| 53 | + |
| 54 | + public function testOneTestOneGroup() |
| 55 | + { |
| 56 | + $testOne = new TestObject('Test1', [], ['group' => ['Group1']], []); |
| 57 | + |
| 58 | + $testArray = ['Test1' => $testOne]; |
| 59 | + $suiteArray = []; |
| 60 | + |
| 61 | + $this->mockHandlers($testArray, $suiteArray); |
| 62 | + |
| 63 | + $actual = json_decode($this->callGroupConfig(['Group1']), true); |
| 64 | + $expected = ['tests' => ['Test1'], 'suites' => null]; |
| 65 | + $this->assertEquals($expected, $actual); |
| 66 | + } |
| 67 | + |
| 68 | + public function testThreeTestsTwoGroup() |
| 69 | + { |
| 70 | + $testOne = new TestObject('Test1', [], ['group' => ['Group1']], []); |
| 71 | + $testTwo = new TestObject('Test2', [], ['group' => ['Group1']], []); |
| 72 | + $testThree = new TestObject('Test3', [], ['group' => ['Group2']], []); |
| 73 | + |
| 74 | + $testArray = ['Test1' => $testOne, 'Test2' => $testTwo, 'Test3' => $testThree]; |
| 75 | + $suiteArray = []; |
| 76 | + |
| 77 | + $this->mockHandlers($testArray, $suiteArray); |
| 78 | + |
| 79 | + $actual = json_decode($this->callGroupConfig(['Group1', 'Group2']), true); |
| 80 | + $expected = ['tests' => ['Test1', 'Test2', 'Test3'], 'suites' => null]; |
| 81 | + $this->assertEquals($expected, $actual); |
| 82 | + } |
| 83 | + |
| 84 | + public function testOneTestOneSuiteOneGroupConfig() |
| 85 | + { |
| 86 | + $testOne = new TestObject('Test1', [], ['group' => ['Group1']], []); |
| 87 | + $suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne], [], []); |
| 88 | + |
| 89 | + $testArray = ['Test1' => $testOne]; |
| 90 | + $suiteArray = ['Suite1' => $suiteOne]; |
| 91 | + |
| 92 | + $this->mockHandlers($testArray, $suiteArray); |
| 93 | + |
| 94 | + $actual = json_decode($this->callGroupConfig(['Group1']), true); |
| 95 | + $expected = ['tests' => null, 'suites' => ['Suite1' => ['Test1']]]; |
| 96 | + $this->assertEquals($expected, $actual); |
| 97 | + } |
| 98 | + |
| 99 | + public function testTwoTestOneSuiteTwoGroupConfig() |
| 100 | + { |
| 101 | + $testOne = new TestObject('Test1', [], ['group' => ['Group1']], []); |
| 102 | + $testTwo = new TestObject('Test2', [], ['group' => ['Group2']], []); |
| 103 | + $suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne, 'Test2' => $testTwo], [], []); |
| 104 | + |
| 105 | + $testArray = ['Test1' => $testOne, 'Test2' => $testTwo]; |
| 106 | + $suiteArray = ['Suite1' => $suiteOne]; |
| 107 | + |
| 108 | + $this->mockHandlers($testArray, $suiteArray); |
| 109 | + |
| 110 | + $actual = json_decode($this->callGroupConfig(['Group1', 'Group2']), true); |
| 111 | + $expected = ['tests' => null, 'suites' => ['Suite1' => ['Test1', 'Test2']]]; |
| 112 | + $this->assertEquals($expected, $actual); |
| 113 | + } |
| 114 | + |
| 115 | + public function testTwoTestTwoSuiteOneGroupConfig() |
| 116 | + { |
| 117 | + $testOne = new TestObject('Test1', [], ['group' => ['Group1']], []); |
| 118 | + $testTwo = new TestObject('Test2', [], ['group' => ['Group1']], []); |
| 119 | + $suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne], [], []); |
| 120 | + $suiteTwo = new SuiteObject('Suite2', ['Test2' => $testTwo], [], []); |
| 121 | + |
| 122 | + $testArray = ['Test1' => $testOne, 'Test2' => $testTwo]; |
| 123 | + $suiteArray = ['Suite1' => $suiteOne, 'Suite2' => $suiteTwo]; |
| 124 | + |
| 125 | + $this->mockHandlers($testArray, $suiteArray); |
| 126 | + |
| 127 | + $actual = json_decode($this->callGroupConfig(['Group1']), true); |
| 128 | + $expected = ['tests' => null, 'suites' => ['Suite1' => ['Test1'], 'Suite2' => ['Test2']]]; |
| 129 | + $this->assertEquals($expected, $actual); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Test specific usecase of a test that is in a group with the group being called along with the suite |
| 134 | + * i.e. run:group Group1 Suite1 |
| 135 | + * @throws \Exception |
| 136 | + */ |
| 137 | + public function testThreeTestOneSuiteOneGroupMix() |
| 138 | + { |
| 139 | + $testOne = new TestObject('Test1', [], [], []); |
| 140 | + $testTwo = new TestObject('Test2', [], [], []); |
| 141 | + $testThree = new TestObject('Test3', [], ['group' => ['Group1']], []); |
| 142 | + $suiteOne = new SuiteObject( |
| 143 | + 'Suite1', |
| 144 | + ['Test1' => $testOne, 'Test2' => $testTwo, 'Test3' => $testThree], |
| 145 | + [], |
| 146 | + [] |
| 147 | + ); |
| 148 | + |
| 149 | + $testArray = ['Test1' => $testOne, 'Test2' => $testTwo, 'Test3' => $testThree]; |
| 150 | + $suiteArray = ['Suite1' => $suiteOne]; |
| 151 | + |
| 152 | + $this->mockHandlers($testArray, $suiteArray); |
| 153 | + |
| 154 | + $actual = json_decode($this->callGroupConfig(['Group1', 'Suite1']), true); |
| 155 | + $expected = ['tests' => null, 'suites' => ['Suite1' => []]]; |
| 156 | + $this->assertEquals($expected, $actual); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Mock handlers to skip parsing |
| 161 | + * @param array $testArray |
| 162 | + * @param array $suiteArray |
| 163 | + * @throws \Exception |
| 164 | + */ |
| 165 | + public function mockHandlers($testArray, $suiteArray) |
| 166 | + { |
| 167 | + AspectMock::double(TestObjectHandler::class, ['initTestData' => ''])->make(); |
| 168 | + $handler = TestObjectHandler::getInstance(); |
| 169 | + $property = new \ReflectionProperty(TestObjectHandler::class, 'tests'); |
| 170 | + $property->setAccessible(true); |
| 171 | + $property->setValue($handler, $testArray); |
| 172 | + |
| 173 | + AspectMock::double(SuiteObjectHandler::class, ['initSuiteData' => ''])->make(); |
| 174 | + $handler = SuiteObjectHandler::getInstance(); |
| 175 | + $property = new \ReflectionProperty(SuiteObjectHandler::class, 'suiteObjects'); |
| 176 | + $property->setAccessible(true); |
| 177 | + $property->setValue($handler, $suiteArray); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Changes visibility and runs getTestAndSuiteConfiguration |
| 182 | + * @param array $testArray |
| 183 | + * @return string |
| 184 | + */ |
| 185 | + public function callTestConfig($testArray) |
| 186 | + { |
| 187 | + $command = new BaseGenerateCommand(); |
| 188 | + $class = new \ReflectionClass($command); |
| 189 | + $method = $class->getMethod('getTestAndSuiteConfiguration'); |
| 190 | + $method->setAccessible(true); |
| 191 | + return $method->invokeArgs($command, [$testArray]); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Changes visibility and runs getGroupAndSuiteConfiguration |
| 196 | + * @param array $groupArray |
| 197 | + * @return string |
| 198 | + */ |
| 199 | + public function callGroupConfig($groupArray) |
| 200 | + { |
| 201 | + $command = new BaseGenerateCommand(); |
| 202 | + $class = new \ReflectionClass($command); |
| 203 | + $method = $class->getMethod('getGroupAndSuiteConfiguration'); |
| 204 | + $method->setAccessible(true); |
| 205 | + return $method->invokeArgs($command, [$groupArray]); |
| 206 | + } |
| 207 | +} |
0 commit comments