|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Tests\unit\Magento\FunctionalTestFramework\Suite\Handlers; |
| 7 | + |
| 8 | +use AspectMock\Test as AspectMock; |
| 9 | +use Magento\FunctionalTestingFramework\ObjectManager\ObjectManager; |
| 10 | +use Magento\FunctionalTestingFramework\ObjectManagerFactory; |
| 11 | +use Magento\FunctionalTestingFramework\Suite\Handlers\SuiteObjectHandler; |
| 12 | +use Magento\FunctionalTestingFramework\Suite\Parsers\SuiteDataParser; |
| 13 | +use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler; |
| 14 | +use Magento\FunctionalTestingFramework\Test\Parsers\TestDataParser; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use tests\unit\Util\SuiteDataArrayBuilder; |
| 17 | +use tests\unit\Util\TestDataArrayBuilder; |
| 18 | + |
| 19 | +class SuiteObjectHandlerTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Tests basic parsing and accesors of suite object and suite object supporting classes |
| 23 | + */ |
| 24 | + public function testGetSuiteObject() |
| 25 | + { |
| 26 | + $suiteDataArrayBuilder = new SuiteDataArrayBuilder(); |
| 27 | + $mockData = $suiteDataArrayBuilder |
| 28 | + ->withName('basicTestSuite') |
| 29 | + ->withAfterHook() |
| 30 | + ->withBeforeHook() |
| 31 | + ->includeTests(['simpleTest']) |
| 32 | + ->includeGroups(['group1']) |
| 33 | + ->excludeTests(['group1Test2']) |
| 34 | + ->excludeGroups(['group2']) |
| 35 | + ->build(); |
| 36 | + |
| 37 | + $testDataArrayBuilder = new TestDataArrayBuilder(); |
| 38 | + $mockSimpleTest = $testDataArrayBuilder |
| 39 | + ->withName('simpleTest') |
| 40 | + ->withTestActions() |
| 41 | + ->build(); |
| 42 | + |
| 43 | + $mockGroup1Test1 = $testDataArrayBuilder |
| 44 | + ->withName('group1Test1') |
| 45 | + ->withAnnotations(['group' => [['value' => 'group1']]]) |
| 46 | + ->withTestActions() |
| 47 | + ->build(); |
| 48 | + |
| 49 | + $mockGroup1Test2 = $testDataArrayBuilder |
| 50 | + ->withName('group1Test2') |
| 51 | + ->withAnnotations(['group' => [['value' => 'group1']]]) |
| 52 | + ->withTestActions() |
| 53 | + ->build(); |
| 54 | + |
| 55 | + $mockGroup2Test1 = $testDataArrayBuilder |
| 56 | + ->withName('group2Test1') |
| 57 | + ->withAnnotations(['group' => [['value' => 'group2']]]) |
| 58 | + ->withTestActions() |
| 59 | + ->build(); |
| 60 | + |
| 61 | + $mockTestData = ['tests' => array_merge($mockSimpleTest, $mockGroup1Test1, $mockGroup1Test2, $mockGroup2Test1)]; |
| 62 | + $this->setMockTestAndSuiteParserOutput($mockTestData, $mockData); |
| 63 | + |
| 64 | + // parse and retrieve suite object with mocked data |
| 65 | + $basicTestSuiteObj = SuiteObjectHandler::getInstance()->getObject('basicTestSuite'); |
| 66 | + |
| 67 | + // assert on created suite object |
| 68 | + $this->assertEquals($basicTestSuiteObj->getName(), 'basicTestSuite'); |
| 69 | + $this->assertCount(2, $basicTestSuiteObj->getTests()); |
| 70 | + $this->assertNotEmpty($basicTestSuiteObj->getBeforeHook()); |
| 71 | + $this->assertNotEmpty($basicTestSuiteObj->getAfterHook()); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Function used to set mock for parser return and force init method to run between tests. |
| 76 | + * |
| 77 | + * @param array $testData |
| 78 | + * @throws \Exception |
| 79 | + */ |
| 80 | + private function setMockTestAndSuiteParserOutput($testData, $suiteData) |
| 81 | + { |
| 82 | + // clear test object handler value to inject parsed content |
| 83 | + $property = new \ReflectionProperty(TestObjectHandler::class, 'testObjectHandler'); |
| 84 | + $property->setAccessible(true); |
| 85 | + $property->setValue(null); |
| 86 | + |
| 87 | + // clear suite object handler value to inject parsed content |
| 88 | + $property = new \ReflectionProperty(SuiteObjectHandler::class, 'SUITE_OBJECT_HANLDER_INSTANCE'); |
| 89 | + $property->setAccessible(true); |
| 90 | + $property->setValue(null); |
| 91 | + |
| 92 | + $mockDataParser = AspectMock::double(TestDataParser::class, ['readTestData' => $testData])->make(); |
| 93 | + $mockSuiteDataParser = AspectMock::double(SuiteDataParser::class, ['readSuiteData' => $suiteData])->make(); |
| 94 | + $instance = AspectMock::double( |
| 95 | + ObjectManager::class, |
| 96 | + ['create' => function ($clazz) use ($mockDataParser, $mockSuiteDataParser) { |
| 97 | + if ($clazz == TestDataParser::class) { |
| 98 | + return $mockDataParser; |
| 99 | + } |
| 100 | + |
| 101 | + if ($clazz == SuiteDataParser::class) { |
| 102 | + return $mockSuiteDataParser; |
| 103 | + } |
| 104 | + }] |
| 105 | + )->make(); |
| 106 | + // bypass the private constructor |
| 107 | + AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]); |
| 108 | + } |
| 109 | +} |
0 commit comments