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