Skip to content
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,109 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Tests\unit\Magento\FunctionalTestFramework\Suite\Handlers;

use AspectMock\Test as AspectMock;
use Magento\FunctionalTestingFramework\ObjectManager\ObjectManager;
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
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 PHPUnit\Framework\TestCase;
use tests\unit\Util\SuiteDataArrayBuilder;
use tests\unit\Util\TestDataArrayBuilder;

class SuiteObjectHandlerTest extends TestCase
{
/**
* Tests basic parsing and accesors of suite object and suite object supporting classes
*/
public function testGetSuiteObject()
{
$suiteDataArrayBuilder = new SuiteDataArrayBuilder();
$mockData = $suiteDataArrayBuilder
->withName('basicTestSuite')
->withAfterHook()
->withBeforeHook()
->includeTests(['simpleTest'])
->includeGroups(['group1'])
->excludeTests(['group1Test2'])
->excludeGroups(['group2'])
->build();

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

$mockGroup1Test1 = $testDataArrayBuilder
->withName('group1Test1')
->withAnnotations(['group' => [['value' => 'group1']]])
->withTestActions()
->build();

$mockGroup1Test2 = $testDataArrayBuilder
->withName('group1Test2')
->withAnnotations(['group' => [['value' => 'group1']]])
->withTestActions()
->build();

$mockGroup2Test1 = $testDataArrayBuilder
->withName('group2Test1')
->withAnnotations(['group' => [['value' => 'group2']]])
->withTestActions()
->build();

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

// parse and retrieve suite object with mocked data
$basicTestSuiteObj = SuiteObjectHandler::getInstance()->getObject('basicTestSuite');

// assert on created suite object
$this->assertEquals($basicTestSuiteObj->getName(), 'basicTestSuite');
$this->assertCount(2, $basicTestSuiteObj->getTests());
$this->assertNotEmpty($basicTestSuiteObj->getBeforeHook());
$this->assertNotEmpty($basicTestSuiteObj->getAfterHook());
}

/**
* 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)
{
// 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();
$instance = AspectMock::double(
ObjectManager::class,
['create' => function ($clazz) use ($mockDataParser, $mockSuiteDataParser) {
if ($clazz == TestDataParser::class) {
return $mockDataParser;
}

if ($clazz == SuiteDataParser::class) {
return $mockSuiteDataParser;
}
}]
)->make();
// bypass the private constructor
AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Tests\unit\Magento\FunctionalTestFramework\Util\Sorter;

use AspectMock\Test as AspectMock;
use Magento\FunctionalTestingFramework\Suite\Handlers\SuiteObjectHandler;
use Magento\FunctionalTestingFramework\Suite\Objects\SuiteObject;
use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler;
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
use Magento\FunctionalTestingFramework\Util\Sorter\ParallelGroupSorter;
use PHPUnit\Framework\TestCase;

class ParallelGroupSorterTest extends TestCase
{
/**
* Test a basic sort of available tests based on size
*/
public function testBasicTestGroupSort()
{
$sampleTestArray = [
'test1' => 100,
'test2' => 300,
'test3' => 50,
'test4' => 60,
'test5' => 25,
'test6' => 125,
'test7' => 250,
'test8' => 1,
'test9' => 80,
'test10' => 25
];

$expectedResult = [
1 => ['test2'],
2 => ['test7'],
3 => ['test6', 'test9'],
4 => ['test1', 'test4', 'test3'],
5 => ['test5', 'test10', 'test8']
];

$testSorter = new ParallelGroupSorter();
$actualResult = $testSorter->getTestsGroupedBySize([], $sampleTestArray, 200);

$this->assertCount(5, $actualResult);

foreach ($actualResult as $gropuNumber => $actualTests) {
$expectedTests = $expectedResult[$gropuNumber];
$this->assertEquals($expectedTests, array_keys($actualTests));
}
}

/**
* Test a sort of both tests and a suite which is larger than the given line limitation
*/
public function testSortWithSuites()
{
// mock tests for test object handler.
$numberOfCalls = 0;
$mockTest1 = AspectMock::double(TestObject::class, ['getTestActionCount' => function () use (&$numberOfCalls) {
$actionCount = [200, 275];
$result = $actionCount[$numberOfCalls];
$numberOfCalls++;

return $result;
}])->make();

$mockHandler = AspectMock::double(
TestObjectHandler::class,
['getObject' => function () use ($mockTest1) {
return $mockTest1;
}]
)->make();

AspectMock::double(TestObjectHandler::class, ['getInstance' => $mockHandler])->make();

// mock a suite object
$mockSuite = AspectMock::double(SuiteObject::class, [
'getBeforeHook' => null,
'getAfterHook' => null,
'getName' => 'mockSuite1'
])->make();
$mockSuiteHandler = AspectMock::double(SuiteObjectHandler::class, ['getObject' => $mockSuite])->make();
AspectMock::double(SuiteObjectHandler::class, ['getInstance' => $mockSuiteHandler]);

// create test to size array
$sampleTestArray = [
'test1' => 100,
'test2' => 300,
'test3' => 500,
'test4' => 60,
'test5' => 125
];

// create mock suite references
$sampleSuiteArray = [
'mockTest1' => ['mockSuite1'],
'mockTest2' => ['mockSuite1']
];

// perform sort
$testSorter = new ParallelGroupSorter();
$actualResult = $testSorter->getTestsGroupedBySize($sampleSuiteArray, $sampleTestArray, 200);

// verify the resulting groups
$this->assertCount(5, $actualResult);

$expectedResults = [
1 => ['test3'],
2 => ['test2'],
3 => ['mockSuite1_0'],
4 => ['mockSuite1_1'],
5 => ['test5', 'test4', 'test1']
];

foreach ($actualResult as $groupNum => $group) {
$this->assertEquals($expectedResults[$groupNum], array_keys($group));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
namespace tests\unit\Magento\FunctionalTestFramework\Test\Util;

use Magento\FunctionalTestingFramework\Exceptions\XmlException;
use Magento\FunctionalTestingFramework\Test\Util\TestNameValidationUtil;
use Magento\FunctionalTestingFramework\Util\Validation\NameValidationUtil;
use PHPUnit\Framework\TestCase;

class TestNameValidationUtilTest extends TestCase
class NameValidationUtilTest extends TestCase
{
/**
* Validate name with curly braces throws exception
Expand Down Expand Up @@ -69,6 +69,6 @@ public function testSpacesInTestName()
private function validateBlacklistedTestName($testName)
{
$this->expectException(XmlException::class);
TestNameValidationUtil::validateName($testName);
NameValidationUtil::validateName($testName, "Test");
}
}
Loading