MftfTestCase.php 3.2 KB

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