Skip to content

MQE-978: Create Test Coverage Around SuiteGenerator Class #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Tests\unit\Magento\FunctionalTestFramework\Suite;

use AspectMock\Test as AspectMock;
use Magento\FunctionalTestingFramework\ObjectManager\ObjectManager;
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
use Magento\FunctionalTestingFramework\Suite\SuiteGenerator;
use Magento\FunctionalTestingFramework\Suite\Generators\GroupClassGenerator;
use Magento\FunctionalTestingFramework\Suite\Handlers\SuiteObjectHandler;
use Magento\FunctionalTestingFramework\Suite\Parsers\SuiteDataParser;
use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler;
use Magento\FunctionalTestingFramework\Test\Parsers\TestDataParser;
use Magento\FunctionalTestingFramework\Util\Manifest\DefaultTestManifest;
use PHPUnit\Framework\TestCase;
use tests\unit\Util\SuiteDataArrayBuilder;
use tests\unit\Util\TestDataArrayBuilder;

class SuiteGeneratorTest extends TestCase
{

/**
* Tests generating a single suite given a set of parsed test data
* @throws \Exception
*/
public function testGenerateSuite()
{
$suiteDataArrayBuilder = new SuiteDataArrayBuilder();
$mockData = $suiteDataArrayBuilder
->withName('basicTestSuite')
->withAfterHook()
->withBeforeHook()
->includeTests(['simpleTest'])
->includeGroups(['group1'])
->build();

$testDataArrayBuilder = new TestDataArrayBuilder();
$mockSimpleTest = $testDataArrayBuilder
->withName('simpleTest')
->withTestActions()
->build();

$mockTestData = ['tests' => array_merge($mockSimpleTest)];
$this->setMockTestAndSuiteParserOutput($mockTestData, $mockData);

// parse and generate suite object with mocked data
$mockSuiteGenerator = SuiteGenerator::getInstance();
$mockSuiteGenerator->generateSuite("basicTestSuite");

// assert that expected suite is generated
$this->expectOutputString("Suite basicTestSuite generated to _generated/basicTestSuite." . PHP_EOL);
}

/**
* Tests generating all suites given a set of parsed test data
* @throws \Exception
*/
public function testGenerateAllSuites()
{
$suiteDataArrayBuilder = new SuiteDataArrayBuilder();
$mockData = $suiteDataArrayBuilder
->withName('basicTestSuite')
->withAfterHook()
->withBeforeHook()
->includeTests(['simpleTest'])
->includeGroups(['group1'])
->build();

$testDataArrayBuilder = new TestDataArrayBuilder();
$mockSimpleTest = $testDataArrayBuilder
->withName('simpleTest')
->withTestActions()
->build();

$mockTestData = ['tests' => array_merge($mockSimpleTest)];
$this->setMockTestAndSuiteParserOutput($mockTestData, $mockData);

// parse and retrieve suite object with mocked data
$exampleTestManifest = new DefaultTestManifest([], "sample/path");
$mockSuiteGenerator = SuiteGenerator::getInstance();
$mockSuiteGenerator->generateAllSuites($exampleTestManifest);

// assert that expected suites are generated
$this->expectOutputString("Suite basicTestSuite generated to _generated/basicTestSuite." . PHP_EOL);
}

/**
* Function used to set mock for parser return and force init method to run between tests.
*
* @param array $testData
* @throws \Exception
*/
private function setMockTestAndSuiteParserOutput($testData, $suiteData)
{
$property = new \ReflectionProperty(SuiteGenerator::class, 'SUITE_GENERATOR_INSTANCE');
$property->setAccessible(true);
$property->setValue(null);

// clear test object handler value to inject parsed content
$property = new \ReflectionProperty(TestObjectHandler::class, 'testObjectHandler');
$property->setAccessible(true);
$property->setValue(null);

// clear suite object handler value to inject parsed content
$property = new \ReflectionProperty(SuiteObjectHandler::class, 'SUITE_OBJECT_HANLDER_INSTANCE');
$property->setAccessible(true);
$property->setValue(null);

$mockDataParser = AspectMock::double(TestDataParser::class, ['readTestData' => $testData])->make();
$mockSuiteDataParser = AspectMock::double(SuiteDataParser::class, ['readSuiteData' => $suiteData])->make();
$mockGroupClass = AspectMock::double(
GroupClassGenerator::class,
['generateGroupClass' => 'namespace']
)->make();
$mockSuiteClass = AspectMock::double(SuiteGenerator::class, ['generateRelevantGroupTests' => null])->make();
$instance = AspectMock::double(
ObjectManager::class,
['create' => function ($clazz) use (
$mockDataParser,
$mockSuiteDataParser,
$mockGroupClass,
$mockSuiteClass
) {
if ($clazz == TestDataParser::class) {
return $mockDataParser;
}
if ($clazz == SuiteDataParser::class) {
return $mockSuiteDataParser;
}
if ($clazz == GroupClassGenerator::class) {
return $mockGroupClass;
}
if ($clazz == SuiteGenerator::class) {
return $mockSuiteClass;
}
}]
)->make();
// bypass the private constructor
AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]);

$property = new \ReflectionProperty(SuiteGenerator::class, 'groupClassGenerator');
$property->setAccessible(true);
$property->setValue($instance, $instance);

}
}
36 changes: 1 addition & 35 deletions src/Magento/FunctionalTestingFramework/Suite/SuiteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ public static function getInstance()
*/
public function generateAllSuites($testManifest)
{
$suites = array_keys(SuiteObjectHandler::getInstance()->getAllObjects());
if ($testManifest != null) {
$suites = $testManifest->getSuiteConfig();
}
$suites = $testManifest->getSuiteConfig();

foreach ($suites as $suiteName => $suiteContent) {
$firstElement = array_values($suiteContent)[0];
Expand All @@ -94,37 +91,6 @@ public function generateAllSuites($testManifest)
}
}

/**
* Returns an array of tests contained within suites as keys pointed at the name of their corresponding suite.
*
* @return array
*/
public function getTestsReferencedInSuites()
{
$testsReferencedInSuites = [];
$suites = SuiteObjectHandler::getInstance()->getAllObjects();

// see if we have a specific suite configuration.
if (!empty($this->suiteReferences)) {
$suites = array_intersect_key($suites, $this->suiteReferences);
}

foreach ($suites as $suite) {
/** @var SuiteObject $suite */
$test_keys = array_keys($suite->getTests());

// see if we need to filter which tests we'll be generating.
if (array_key_exists($suite->getName(), $this->suiteReferences)) {
$test_keys = $this->suiteReferences[$suite->getName()] ?? $test_keys;
}

$testToSuiteName = array_fill_keys($test_keys, [$suite->getName()]);
$testsReferencedInSuites = array_merge_recursive($testsReferencedInSuites, $testToSuiteName);
}

return $testsReferencedInSuites;
}

/**
* Function which takes a suite name and generates corresponding dir, test files, group class, and updates
* yml configuration for group run.
Expand Down