forked from magento/magento2-functional-testing-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMftfTestCase.php
100 lines (90 loc) · 3.16 KB
/
MftfTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace tests\util;
use Magento\FunctionalTestingFramework\ObjectManager;
use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler;
use Magento\FunctionalTestingFramework\Util\TestGenerator;
use PHPUnit\Framework\TestCase;
abstract class MftfTestCase extends TestCase
{
const RESOURCES_PATH = __DIR__ .
DIRECTORY_SEPARATOR .
'..' .
DIRECTORY_SEPARATOR .
'verification' .
DIRECTORY_SEPARATOR .
'Resources';
/**
* Private function which takes a test name, generates the test and compares with a correspondingly named txt file
* with expected contents.
*
* @param string $testName
*/
public function generateAndCompareTest($testName)
{
$testObject = TestObjectHandler::getInstance()->getObject($testName);
$test = TestGenerator::getInstance(null, [$testObject]);
$test->createAllTestFiles();
$cestFile = $test->getExportDir() .
DIRECTORY_SEPARATOR .
$testObject->getCodeceptionName() .
".php";
$this->assertTrue(file_exists($cestFile));
$this->assertFileEquals(
self::RESOURCES_PATH . DIRECTORY_SEPARATOR . $testObject->getName() . ".txt",
$cestFile
);
}
/**
* Private function which attempts to generate tests given an invalid shcema of a various type
*
* @param string[] $fileContents
* @param string $objectType
* @param string $expectedError
* @throws \Exception
*/
public function validateSchemaErrorWithTest($fileContents, $objectType ,$expectedError)
{
$this->clearHandler();
$fullTestModulePath = TESTS_MODULE_PATH .
DIRECTORY_SEPARATOR .
'TestModule' .
DIRECTORY_SEPARATOR .
$objectType .
DIRECTORY_SEPARATOR;
foreach ($fileContents as $fileName => $fileContent) {
$tempFile = $fullTestModulePath . $fileName;
$handle = fopen($tempFile, 'w') or die('Cannot open file: ' . $tempFile);
fwrite($handle, $fileContent);
fclose($handle);
}
try {
$this->expectExceptionMessage($expectedError);
TestObjectHandler::getInstance()->getObject("someTest");
} finally {
foreach (array_keys($fileContents) as $fileName) {
unlink($fullTestModulePath . $fileName);
}
$this->clearHandler();
}
}
/**
* Clears test handler and object manager to force recollection of test data
*
* @throws \Exception
*/
private function clearHandler()
{
// clear test object handler to force recollection of test data
$property = new \ReflectionProperty(TestObjectHandler::class, 'testObjectHandler');
$property->setAccessible(true);
$property->setValue(null);
// clear test object handler to force recollection of test data
$property = new \ReflectionProperty(ObjectManager::class, 'instance');
$property->setAccessible(true);
$property->setValue(null);
}
}