XMLValidationTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Yandex\Allure\Adapter;
  3. use Doctrine\Common\Annotations\AnnotationRegistry;
  4. use DOMDocument;
  5. use SebastianBergmann\Exporter\Exception;
  6. use Yandex\Allure\Adapter\Event\AddAttachmentEvent;
  7. use Yandex\Allure\Adapter\Event\StepFinishedEvent;
  8. use Yandex\Allure\Adapter\Event\StepStartedEvent;
  9. use Yandex\Allure\Adapter\Event\TestCaseFailedEvent;
  10. use Yandex\Allure\Adapter\Event\TestCaseFinishedEvent;
  11. use Yandex\Allure\Adapter\Event\TestCaseStartedEvent;
  12. use Yandex\Allure\Adapter\Event\TestSuiteFinishedEvent;
  13. use Yandex\Allure\Adapter\Event\TestSuiteStartedEvent;
  14. use Yandex\Allure\Adapter\Model\Description;
  15. use Yandex\Allure\Adapter\Model\DescriptionType;
  16. use Yandex\Allure\Adapter\Model\Label;
  17. use Yandex\Allure\Adapter\Model\Parameter;
  18. use Yandex\Allure\Adapter\Model\ParameterKind;
  19. use Yandex\Allure\Adapter\Model\SeverityLevel;
  20. const TEST_CASE_NAME = 'test-case-name';
  21. const TEST_CASE_TITLE = 'test-case-title';
  22. const TEST_SUITE_UUID = 'test-suite-uuid';
  23. const TEST_SUITE_NAME = 'test-suite-name';
  24. const TEST_SUITE_TITLE = 'test-suite-title';
  25. const DESCRIPTION = 'test-suite-description';
  26. const FEATURE_NAME = 'test-feature';
  27. const STORY_NAME = 'test-story';
  28. const PARAMETER_NAME = 'test-parameter-name';
  29. const PARAMETER_VALUE = 'test-parameter-value';
  30. const FAILURE_MESSAGE = 'failure-message';
  31. const STEP_NAME = 'test-step-name';
  32. const STEP_TITLE = 'test-step-title';
  33. const STEP_ATTACHMENT_TITLE = 'step-attachment-caption';
  34. const STEP_ATTACHMENT_SOURCE = 'step-attachment-source';
  35. class XMLValidationTest extends \PHPUnit_Framework_TestCase
  36. {
  37. public function testGeneratedXMLIsValid()
  38. {
  39. $tmpDir = $this->prepareDirForXML();
  40. $uuid = $this->generateXML($tmpDir);
  41. $fileName = $tmpDir . DIRECTORY_SEPARATOR . $uuid . '-testsuite.xml';
  42. $this->assertTrue(file_exists($fileName));
  43. $this->validateFileXML($fileName);
  44. }
  45. private function prepareDirForXML()
  46. {
  47. $tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('allure-xml-test');
  48. mkdir($tmpDir);
  49. $this->assertTrue(
  50. file_exists($tmpDir) &&
  51. is_writable($tmpDir)
  52. );
  53. return $tmpDir;
  54. }
  55. private function generateXML($tmpDir)
  56. {
  57. AnnotationRegistry::registerAutoloadNamespace(
  58. 'JMS\Serializer\Annotation',
  59. __DIR__ . "/../../../../vendor/jms/serializer/src"
  60. );
  61. Model\Provider::setOutputDirectory($tmpDir);
  62. Allure::setDefaultLifecycle();
  63. $testSuiteStartedEvent = new TestSuiteStartedEvent(TEST_SUITE_NAME);
  64. $uuid = $testSuiteStartedEvent->getUuid();
  65. $testSuiteStartedEvent->setTitle(TEST_SUITE_TITLE);
  66. $testSuiteStartedEvent->setDescription(new Description(DescriptionType::HTML, DESCRIPTION));
  67. $testSuiteStartedEvent->setLabels([
  68. Label::feature(FEATURE_NAME),
  69. Label::story(STORY_NAME)
  70. ]);
  71. Allure::lifecycle()->fire($testSuiteStartedEvent);
  72. $testCaseStartedEvent = new TestCaseStartedEvent($uuid, TEST_CASE_NAME);
  73. $testCaseStartedEvent->setDescription(new Description(DescriptionType::MARKDOWN, DESCRIPTION));
  74. $testCaseStartedEvent->setLabels([
  75. Label::feature(FEATURE_NAME),
  76. Label::story(STORY_NAME),
  77. Label::severity(SeverityLevel::MINOR)
  78. ]);
  79. $testCaseStartedEvent->setTitle(TEST_CASE_TITLE);
  80. $testCaseStartedEvent->setParameters([
  81. new Parameter(PARAMETER_NAME, PARAMETER_VALUE, ParameterKind::SYSTEM_PROPERTY)
  82. ]);
  83. Allure::lifecycle()->fire($testCaseStartedEvent);
  84. $testCaseFailureEvent = new TestCaseFailedEvent();
  85. $testCaseFailureEvent = $testCaseFailureEvent->withMessage(FAILURE_MESSAGE)->withException(new \Exception());
  86. Allure::lifecycle()->fire($testCaseFailureEvent);
  87. $stepStartedEvent = new StepStartedEvent(STEP_NAME);
  88. $stepStartedEvent = $stepStartedEvent->withTitle(STEP_TITLE);
  89. Allure::lifecycle()->fire($stepStartedEvent);
  90. Allure::lifecycle()->fire(
  91. new AddAttachmentEvent(STEP_ATTACHMENT_SOURCE, STEP_ATTACHMENT_TITLE, 'text/plain')
  92. );
  93. Allure::lifecycle()->fire(new StepFinishedEvent());
  94. Allure::lifecycle()->fire(new TestCaseFinishedEvent());
  95. Allure::lifecycle()->fire(new TestSuiteFinishedEvent($uuid));
  96. return $uuid;
  97. }
  98. private function validateFileXML($fileName)
  99. {
  100. libxml_use_internal_errors(true); //Comment this line to see DOMDocument XML validation errors
  101. $dom = new DOMDocument();
  102. $dom->load($fileName);
  103. $schemaFilename = __DIR__ . DIRECTORY_SEPARATOR . 'allure-1.4.0.xsd';
  104. $isSchemaValid = $dom->schemaValidate($schemaFilename);
  105. $this->assertTrue($isSchemaValid, 'XML file should be valid');
  106. }
  107. }