AllureAdapterTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace Yandex\Allure\Adapter;
  3. use PHPUnit\Framework\AssertionFailedError;
  4. use PHPUnit\Framework\TestCase;
  5. use PHPUnit\Framework\TestSuite;
  6. use Exception;
  7. use org\bovigo\vfs\vfsStream;
  8. use Yandex\Allure\Adapter\Event\TestCaseBrokenEvent;
  9. use Yandex\Allure\Adapter\Event\TestCaseCanceledEvent;
  10. use Yandex\Allure\Adapter\Event\TestCaseFailedEvent;
  11. use Yandex\Allure\Adapter\Event\TestCasePendingEvent;
  12. use Yandex\Allure\Adapter\Support\MockedLifecycle;
  13. const EXCEPTION_MESSAGE = 'test-exception-message';
  14. const ROOT_DIRECTORY = 'test-root-directory';
  15. const TEST_DIRECTORY = 'test-directory';
  16. class AllureAdapterTest extends TestCase
  17. {
  18. /**
  19. * @var MockedLifecycle
  20. */
  21. private $mockedLifecycle;
  22. /**
  23. * @var AllureAdapter
  24. */
  25. private $allureAdapter;
  26. protected function setUp()
  27. {
  28. parent::setUp();
  29. $this->mockedLifecycle = new MockedLifecycle();
  30. Allure::setLifecycle($this->getMockedLifecycle());
  31. $this->allureAdapter = new AllureAdapter('test-output-directory', true);
  32. }
  33. public function testPrepareOutputDirectory()
  34. {
  35. $rootDirectory = vfsStream::setup(ROOT_DIRECTORY);
  36. $this->assertFalse($rootDirectory->hasChild(TEST_DIRECTORY));
  37. $newDirectoryPath = vfsStream::url(ROOT_DIRECTORY) . DIRECTORY_SEPARATOR . TEST_DIRECTORY;
  38. Model\Provider::setOutputDirectory(null);
  39. new AllureAdapter($newDirectoryPath, true);
  40. $this->assertTrue($rootDirectory->hasChild(TEST_DIRECTORY));
  41. $this->assertEquals($newDirectoryPath, Model\Provider::getOutputDirectory());
  42. }
  43. public function testAddError()
  44. {
  45. $exception = $this->getException();
  46. $time = $this->getTime();
  47. $this->getAllureAdapter()->addError($this, $exception, $time);
  48. $events = $this->getMockedLifecycle()->getEvents();
  49. $event = new TestCaseBrokenEvent();
  50. $event->withException($exception)->withMessage(EXCEPTION_MESSAGE);
  51. $this->assertEquals(1, sizeof($events));
  52. $this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestCaseBrokenEvent', $events[0]);
  53. $this->assertEquals($event, $events[0]);
  54. }
  55. public function testAddFailure()
  56. {
  57. $exception = new AssertionFailedError(EXCEPTION_MESSAGE);
  58. $time = $this->getTime();
  59. $this->getAllureAdapter()->addFailure($this, $exception, $time);
  60. $events = $this->getMockedLifecycle()->getEvents();
  61. $event = new TestCaseFailedEvent();
  62. $event->withException($exception)->withMessage(EXCEPTION_MESSAGE);
  63. $this->assertEquals(1, sizeof($events));
  64. $this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestCaseFailedEvent', $events[0]);
  65. $this->assertEquals($event, $events[0]);
  66. }
  67. public function testAddIncompleteTest()
  68. {
  69. $exception = $this->getException();
  70. $time = $this->getTime();
  71. $this->getAllureAdapter()->addIncompleteTest($this, $exception, $time);
  72. $this->pendingTestCaseEventAssertions($exception);
  73. }
  74. private function pendingTestCaseEventAssertions(\Exception $exception)
  75. {
  76. $events = $this->getMockedLifecycle()->getEvents();
  77. $event = new TestCasePendingEvent();
  78. $event->withException($exception);
  79. $this->assertEquals(1, sizeof($events));
  80. $this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestCasePendingEvent', $events[0]);
  81. $this->assertEquals($event, $events[0]);
  82. }
  83. public function testAddRiskyTest()
  84. {
  85. $exception = $this->getException();
  86. $time = $this->getTime();
  87. $this->getAllureAdapter()->addRiskyTest($this, $exception, $time);
  88. $this->pendingTestCaseEventAssertions($exception);
  89. }
  90. public function testAddSkippedTest()
  91. {
  92. $exception = $this->getException();
  93. $time = $this->getTime();
  94. $this->getAllureAdapter()->addSkippedTest($this, $exception, $time);
  95. $events = $this->getMockedLifecycle()->getEvents();
  96. $event = new TestCaseCanceledEvent();
  97. $event->withException($exception)->withMessage(EXCEPTION_MESSAGE);
  98. $this->assertEquals(3, sizeof($events));
  99. $this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestCaseStartedEvent', $events[0]);
  100. $this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestCaseCanceledEvent', $events[1]);
  101. $this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestCaseFinishedEvent', $events[2]);
  102. $this->assertEquals($event, $events[1]);
  103. }
  104. public function testStartTestSuite()
  105. {
  106. $this->getAllureAdapter()->startTestSuite($this->getTestSuite());
  107. $events = $this->getMockedLifecycle()->getEvents();
  108. $this->assertEquals(1, sizeof($events));
  109. $this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestSuiteStartedEvent', $events[0]);
  110. }
  111. public function testEndTestSuite()
  112. {
  113. $this->getAllureAdapter()->endTestSuite($this->getTestSuite());
  114. $events = $this->getMockedLifecycle()->getEvents();
  115. $this->assertEquals(1, sizeof($events));
  116. $this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestSuiteFinishedEvent', $events[0]);
  117. }
  118. public function testStartTest()
  119. {
  120. $this->getAllureAdapter()->startTestSuite($this->getTestSuite()); //Is needed to set $adapter->suiteName field
  121. $this->getAllureAdapter()->startTest($this);
  122. $events = $this->getMockedLifecycle()->getEvents();
  123. $this->assertEquals(2, sizeof($events));
  124. $this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestSuiteStartedEvent', $events[0]);
  125. $this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestCaseStartedEvent', $events[1]);
  126. }
  127. public function testEndTest()
  128. {
  129. $this->getAllureAdapter()->endTest($this, $this->getTime());
  130. $events = $this->getMockedLifecycle()->getEvents();
  131. $this->assertEquals(1, sizeof($events));
  132. $this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestCaseFinishedEvent', $events[0]);
  133. }
  134. private function getMockedLifecycle()
  135. {
  136. return $this->mockedLifecycle;
  137. }
  138. private function getAllureAdapter()
  139. {
  140. return $this->allureAdapter;
  141. }
  142. private function getException()
  143. {
  144. return new Exception(EXCEPTION_MESSAGE);
  145. }
  146. private function getTime()
  147. {
  148. return (float)time();
  149. }
  150. private function getTestSuite()
  151. {
  152. return new TestSuite(__CLASS__);
  153. }
  154. }