AllureTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace Yandex\Allure\Adapter;
  3. use Doctrine\Common\Annotations\AnnotationRegistry;
  4. use Yandex\Allure\Adapter\Event\ClearStepStorageEvent;
  5. use Yandex\Allure\Adapter\Event\ClearTestCaseStorageEvent;
  6. use Yandex\Allure\Adapter\Event\StepFinishedEvent;
  7. use Yandex\Allure\Adapter\Event\StepStartedEvent;
  8. use Yandex\Allure\Adapter\Event\TestCaseFinishedEvent;
  9. use Yandex\Allure\Adapter\Event\TestCaseStartedEvent;
  10. use Yandex\Allure\Adapter\Event\TestSuiteFinishedEvent;
  11. use Yandex\Allure\Adapter\Model\Attachment;
  12. use Yandex\Allure\Adapter\Model\Provider;
  13. use Yandex\Allure\Adapter\Model\Step;
  14. use Yandex\Allure\Adapter\Model\TestCase;
  15. use Yandex\Allure\Adapter\Fixtures\GenericStepEvent;
  16. use Yandex\Allure\Adapter\Fixtures\GenericTestCaseEvent;
  17. use Yandex\Allure\Adapter\Fixtures\GenericTestSuiteEvent;
  18. class AllureTest extends \PHPUnit_Framework_TestCase
  19. {
  20. const STEP_NAME = 'step-name';
  21. const TEST_CASE_NAME = 'test-case-name';
  22. const TEST_SUITE_NAME = 'test-suite-name';
  23. const TEST_SUITE_UUID = 'test-suite-uuid';
  24. const STEP_ATTACHMENT_TITLE = 'step-attachment-caption';
  25. const STEP_ATTACHMENT_SOURCE = 'step-attachment-source';
  26. const STEP_ATTACHMENT_TYPE = 'text/plain';
  27. public function testStepStorageClearEvent()
  28. {
  29. Allure::lifecycle()->getStepStorage()->clear();
  30. Allure::lifecycle()->getStepStorage()->put(new Step());
  31. Allure::lifecycle()->fire(new ClearStepStorageEvent());
  32. $this->assertTrue(Allure::lifecycle()->getStepStorage()->isEmpty());
  33. }
  34. public function testTestCaseStorageClear()
  35. {
  36. Allure::lifecycle()->getTestCaseStorage()->clear();
  37. Allure::lifecycle()->getTestCaseStorage()->put(new TestCase());
  38. Allure::lifecycle()->fire(new ClearTestCaseStorageEvent());
  39. $this->assertTrue(Allure::lifecycle()->getTestCaseStorage()->isEmpty());
  40. }
  41. public function testStepStartedEvent()
  42. {
  43. Allure::lifecycle()->getStepStorage()->clear();
  44. $this->assertTrue(Allure::lifecycle()->getStepStorage()->isEmpty());
  45. Allure::lifecycle()->fire(new StepStartedEvent(self::STEP_NAME));
  46. $this->assertEquals(1, Allure::lifecycle()->getStepStorage()->size());
  47. $step = Allure::lifecycle()->getStepStorage()->getLast();
  48. $this->assertEquals(self::STEP_NAME, $step->getName());
  49. }
  50. public function testStepFinishedEvent()
  51. {
  52. $step = new Step();
  53. $step->setName(self::STEP_NAME);
  54. Allure::lifecycle()->getStepStorage()->put($step);
  55. Allure::lifecycle()->fire(new StepFinishedEvent());
  56. $step = Allure::lifecycle()->getStepStorage()->getLast();
  57. $this->assertEquals(self::STEP_NAME, $step->getName());
  58. }
  59. public function testGenericStepEvent()
  60. {
  61. $step = new Step();
  62. Allure::lifecycle()->getStepStorage()->clear();
  63. Allure::lifecycle()->getStepStorage()->put($step);
  64. Allure::lifecycle()->fire(new GenericStepEvent(self::STEP_NAME));
  65. $this->assertEquals(self::STEP_NAME, $step->getName());
  66. }
  67. public function testTestCaseStarted()
  68. {
  69. Allure::lifecycle()->getTestCaseStorage()->clear();
  70. Allure::lifecycle()->getTestSuiteStorage()->clear();
  71. $this->assertTrue(Allure::lifecycle()->getTestCaseStorage()->isEmpty());
  72. Allure::lifecycle()->fire(new TestCaseStartedEvent(self::TEST_SUITE_UUID, self::TEST_CASE_NAME));
  73. $testCase = Allure::lifecycle()
  74. ->getTestSuiteStorage()
  75. ->get(self::TEST_SUITE_UUID)
  76. ->getTestCase(self::TEST_CASE_NAME);
  77. $this->assertNotEmpty($testCase);
  78. $this->assertEquals(self::TEST_CASE_NAME, $testCase->getName());
  79. }
  80. public function testTestCaseFinishedEvent()
  81. {
  82. Allure::lifecycle()->getStepStorage()->clear();
  83. Allure::lifecycle()->getStepStorage()->getLast(); //To initialize root step
  84. Allure::lifecycle()->getTestCaseStorage()->clear();
  85. $step = new Step();
  86. $step->setName(self::STEP_NAME);
  87. $attachment = new Attachment(
  88. self::STEP_ATTACHMENT_TITLE,
  89. self::STEP_ATTACHMENT_SOURCE,
  90. self::STEP_ATTACHMENT_TYPE
  91. );
  92. Allure::lifecycle()->getStepStorage()->getLast()->addStep($step);
  93. Allure::lifecycle()->getStepStorage()->getLast()->addAttachment($attachment);
  94. $testCaseFromStorage = Allure::lifecycle()->getTestCaseStorage()->get();
  95. Allure::lifecycle()->fire(new TestCaseFinishedEvent());
  96. //Checking that attachments were moved
  97. $attachments = $testCaseFromStorage->getAttachments();
  98. $this->assertEquals(1, sizeof($attachments));
  99. $attachment = array_pop($attachments);
  100. $this->assertTrue(
  101. ($attachment instanceof Attachment) &&
  102. ($attachment->getTitle() === self::STEP_ATTACHMENT_TITLE) &&
  103. ($attachment->getSource() === self::STEP_ATTACHMENT_SOURCE) &&
  104. ($attachment->getType() === self::STEP_ATTACHMENT_TYPE)
  105. );
  106. //Checking that steps were moved
  107. $steps = $testCaseFromStorage->getSteps();
  108. $this->assertEquals(1, sizeof($steps));
  109. $stepFromStorage = array_pop($steps);
  110. $this->assertTrue(
  111. ($stepFromStorage instanceof Step) &&
  112. ($stepFromStorage->getName() === self::STEP_NAME)
  113. );
  114. $this->assertTrue(Allure::lifecycle()->getTestCaseStorage()->isEmpty());
  115. }
  116. public function testGenericTestCaseEvent()
  117. {
  118. $testCase = new TestCase();
  119. Allure::lifecycle()->getTestCaseStorage()->clear();
  120. Allure::lifecycle()->getTestCaseStorage()->put($testCase);
  121. Allure::lifecycle()->fire(new GenericTestCaseEvent(self::TEST_CASE_NAME));
  122. $this->assertEquals(self::TEST_CASE_NAME, $testCase->getName());
  123. }
  124. public function testGenericTestSuiteEvent()
  125. {
  126. Allure::lifecycle()->getTestSuiteStorage()->clear();
  127. $event = new GenericTestSuiteEvent(self::TEST_SUITE_NAME);
  128. $testSuite = Allure::lifecycle()->getTestSuiteStorage()->get($event->getUuid());
  129. Allure::lifecycle()->fire($event);
  130. $this->assertEquals(self::TEST_SUITE_NAME, $testSuite->getName());
  131. }
  132. public function testTestSuiteFinishedEvent()
  133. {
  134. Allure::lifecycle()->getTestSuiteStorage()->clear();
  135. $testSuite = Allure::lifecycle()->getTestSuiteStorage()->get(self::TEST_SUITE_UUID);
  136. $testSuite->addTestCase(new TestCase());
  137. $this->assertEquals(1, Allure::lifecycle()->getTestSuiteStorage()->size());
  138. $outputDirectory = sys_get_temp_dir();
  139. AnnotationRegistry::registerAutoloadNamespace(
  140. 'JMS\Serializer\Annotation',
  141. __DIR__ . "/../../../../vendor/jms/serializer/src"
  142. );
  143. Provider::setOutputDirectory($outputDirectory);
  144. $xmlFilePath = $outputDirectory . DIRECTORY_SEPARATOR . self::TEST_SUITE_UUID . '-testsuite.xml';
  145. Allure::lifecycle()->fire(new TestSuiteFinishedEvent(self::TEST_SUITE_UUID));
  146. $this->assertTrue(Allure::lifecycle()->getTestSuiteStorage()->isEmpty());
  147. $this->assertTrue(file_exists($xmlFilePath));
  148. }
  149. }